jestjs

How can I mock a service within a React component to isolate unit tests in jest?

孤街浪徒 提交于 2021-01-28 13:08:55
问题 I'm trying to refactor a unit test to isolate a service that calls an API using axios from the component calling the service. The service is for the moment really simple: import axios from 'axios' export default class SomeService { getObjects() { return axios.get('/api/objects/').then(response => response.data); } } Here's a snippet of the component that calls the service: const someService = new SomeService(); class ObjectList extends Component { state = { data: [], } componentDidMount() {

How can I mock a service within a React component to isolate unit tests in jest?

柔情痞子 提交于 2021-01-28 13:00:13
问题 I'm trying to refactor a unit test to isolate a service that calls an API using axios from the component calling the service. The service is for the moment really simple: import axios from 'axios' export default class SomeService { getObjects() { return axios.get('/api/objects/').then(response => response.data); } } Here's a snippet of the component that calls the service: const someService = new SomeService(); class ObjectList extends Component { state = { data: [], } componentDidMount() {

Jest snapshot testing error: You should not use <Link> outside a <Router>

谁都会走 提交于 2021-01-28 12:13:23
问题 I want to write snapshot test for my Footer component, but it throws error: You should not use <Link> outside a <Router> . Here is my code: import React from 'react' import renderer from 'react-test-renderer' import Footer from '../footer' it('Footer renders correctly', () => { const tree = renderer .create(<Footer />) .toJSON() expect(tree).toMatchSnapshot() }) I know this happens because Footer component uses Link from react-router-dom . In order to solve this problem I wrapped Footer

Jest test fails for async function that calls another async function

守給你的承諾、 提交于 2021-01-28 12:10:25
问题 I am trying to test an async function that uses data returned by another async function. Here is the code to explain my problem: StudentInfo.js export async function getData() { //studentData imported from another file return new Promise(resolve => { setTimeout(() => { resolve(studentData); }, 5000); }); } export async function filterStudents() { const studentData = await StudentInfo.getData(); //computations return filteredData; //object } StudentInfo.test.js import * as studentInfo from

How to use Jest to mock and spy a `mongoose.connect` in a provider of NestJS

試著忘記壹切 提交于 2021-01-28 12:03:06
问题 I defined a custom database-specific Module to connect a MongoDB via Mongoose APIs. The complete code is here. { provide: DATABASE_CONNECTION, useFactory: (dbConfig: ConfigType<typeof mongodbConfig>): Connection => createConnection(dbConfig.uri, { useNewUrlParser: true, useUnifiedTopology: true, //see: https://mongoosejs.com/docs/deprecations.html#findandmodify useFindAndModify: false }), inject: [mongodbConfig.KEY], }, When writing tests for this provider, I want to confirm the database

How can I unit test a custom element

爱⌒轻易说出口 提交于 2021-01-28 10:10:11
问题 I have a class that extends HTMLElement . From reading this thread https://github.com/Microsoft/TypeScript/issues/574#issuecomment-231683089 I see that I can't instantiate a custom element with out getting a Illegal constructor . Additionally, the registerElement looked promising as it returned the constructor for the custom element but that is now deprecated for using CustomElementRegistry.define() that 1. exists on the window object and 2. returns void. Any solutions will be greatly

How can I unit test a custom element

旧巷老猫 提交于 2021-01-28 10:02:48
问题 I have a class that extends HTMLElement . From reading this thread https://github.com/Microsoft/TypeScript/issues/574#issuecomment-231683089 I see that I can't instantiate a custom element with out getting a Illegal constructor . Additionally, the registerElement looked promising as it returned the constructor for the custom element but that is now deprecated for using CustomElementRegistry.define() that 1. exists on the window object and 2. returns void. Any solutions will be greatly

Run Jest test in IntelliJ IDEA

杀马特。学长 韩版系。学妹 提交于 2021-01-28 06:41:08
问题 I created a React app with create-react-app version 1.4.0 and opened the resulting project in IntelliJ. Now I am attempting to run the generated test in IntelliJ as well. I get the following output when I do so: /usr/bin/node /home/l/src/hello-react/node_modules/jest/bin/jest --config {\"rootDir\":\"/home/l/src/hello-react\",\"transformIgnorePatterns\":[\"/node_modules/\",\"^/home/l/bin/idea-IU-172.3544.35/plugins/JavaScriptLanguage/helpers\"],\"unmockedModulePathPatterns\":[\"^/home/l/bin

How can we import/include MathQuill in JavaScript?

£可爱£侵袭症+ 提交于 2021-01-28 06:37:07
问题 I'm trying to test my application using Jestjs. Since some of the buttons in my application are MathQuill math fields, I need to be able to somehow include/import MathQuill for testing the button presses using Jestjs. I've looked up a lot of resources online and I can't seem to find a way to do this. If you've come across a similar dead end and happen to have resolved it, could you please give me some pointers on possible solutions/workarounds which I can try out? Thank you! Edit: I'm doing

Detox tests are assigned to Undefined, and all e2e are skipped

痴心易碎 提交于 2021-01-28 05:45:43
问题 I installed jest and detox on a fresh react-native init project. Install jest and jest-circus as per detox docs Setup iOS build and test configuration Get the following error consistently on new builds my .detoxrc.json file: { "testRunner": "jest", "runnerConfig": "e2e/config.json", "configurations": { "ios": { "type": "ios.simulator", "build": "xcodebuild -workspace ios/rndetox.xcworkspace -scheme rndetox -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build", "binaryPath":