jestjs

React & Jest: Cannot find module from test file

半腔热情 提交于 2021-02-18 10:12:30
问题 Setting up a Jest test ('App-test.js') for a Redux action ('App.js') in a directory app/__tests__ : Here's the header of App.js: jest.unmock('../../modules/actions/App.js') import React from 'react' import ReactDOM from 'react-dom' import TestUtils from 'react-addons-test-utils' import * as App from '../../modules/actions/App.js' In app/ there is a module config.js . This is being imported where it is needed. The problem is, when I run my Jest tests, such as App-test.js, it is looking for

How to test a React component with RouteComponentProps?

天涯浪子 提交于 2021-02-17 18:46:46
问题 I have a component that has props which extend RouteComponentProps that looks like this: export interface RouteComponentProps<P> { match: match<P>; location: H.Location; history: H.History; staticContext?: any; } Now, when I use my component in the app, I pass these props to it: <MyComponent match={this.props.match} location={this.props.location} history={this.props.history} /> The props are available already because it's running inside react router. Now, how do I test this component without

How to test a React component with RouteComponentProps?

和自甴很熟 提交于 2021-02-17 18:44:07
问题 I have a component that has props which extend RouteComponentProps that looks like this: export interface RouteComponentProps<P> { match: match<P>; location: H.Location; history: H.History; staticContext?: any; } Now, when I use my component in the app, I pass these props to it: <MyComponent match={this.props.match} location={this.props.location} history={this.props.history} /> The props are available already because it's running inside react router. Now, how do I test this component without

How to test a React component with RouteComponentProps?

跟風遠走 提交于 2021-02-17 18:43:55
问题 I have a component that has props which extend RouteComponentProps that looks like this: export interface RouteComponentProps<P> { match: match<P>; location: H.Location; history: H.History; staticContext?: any; } Now, when I use my component in the app, I pass these props to it: <MyComponent match={this.props.match} location={this.props.location} history={this.props.history} /> The props are available already because it's running inside react router. Now, how do I test this component without

NestJS Jest cannot find module with absolute path

余生长醉 提交于 2021-02-17 01:56:28
问题 I have a quite new NestJS application. I'm trying to run unit tests, but they keep failing due to 'cannot find module..' when using absolute paths ("src/users/..."), but works when using relative paths ("./users/.."). Is there anything wrong with my configuration here? Jest setup in package.json: "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".spec.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, "coverageDirectory": "../coverage", "testEnvironment

Mocking dayjs extend

你说的曾经没有我的故事 提交于 2021-02-15 07:47:43
问题 In my code that needs testing I use import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; dayjs.extend(utc); dayjs().add(15, 'minute') In my test I need to mock dayjs in order to always have the same date when comparing snapshots in jest so I did jest.mock('dayjs', () => jest.fn((...args) => jest.requireActual('dayjs')( args.filter((arg) => arg).length > 0 ? args : '2020-08-12' ) ) ); It fails with TypeError: _dayjs.default.extend is not a function Unfortunately similar questions on

Mocking dayjs extend

巧了我就是萌 提交于 2021-02-15 07:47:11
问题 In my code that needs testing I use import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; dayjs.extend(utc); dayjs().add(15, 'minute') In my test I need to mock dayjs in order to always have the same date when comparing snapshots in jest so I did jest.mock('dayjs', () => jest.fn((...args) => jest.requireActual('dayjs')( args.filter((arg) => arg).length > 0 ? args : '2020-08-12' ) ) ); It fails with TypeError: _dayjs.default.extend is not a function Unfortunately similar questions on

Jest initialize and shared objects once per test suite and across test cases

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-11 18:19:22
问题 I want to use shared resources between jest test suites. I read in the internet and found that this could be the solution. But the setup is invoked per each test file. I have two test files links.test.js and 'subscritpions.test.js'. I usually call them with one command jest and that all. The problem is that the setup function of my custom environment custom-environment.js : const NodeEnvironment = require('jest-environment-node'); const MySql = require('../../lib/databases/myslq/db'); class

Jest initialize and shared objects once per test suite and across test cases

…衆ロ難τιáo~ 提交于 2021-02-11 18:18:17
问题 I want to use shared resources between jest test suites. I read in the internet and found that this could be the solution. But the setup is invoked per each test file. I have two test files links.test.js and 'subscritpions.test.js'. I usually call them with one command jest and that all. The problem is that the setup function of my custom environment custom-environment.js : const NodeEnvironment = require('jest-environment-node'); const MySql = require('../../lib/databases/myslq/db'); class

Testing component created using styled component with jest and enzyme

微笑、不失礼 提交于 2021-02-11 18:16:10
问题 I have a few tags created using styled components lib: component.js .... const NoJobs = styled.h1` text-align: center; `; .... <div> <NoJobs> No Jobs Found!!! </NoJobs> </div> I want to test this component, below is my test case: component.test.js describe('<JobList />', () => { let wrapper; beforeEach(() => { wrapper = shallow(<JobList />); }); it('should show "no data found" when there is no data', () => { expect(wrapper.contains(<NoJobs>No Jobs Found!!!</NoJobs>)).toEqual(true); }); });