jestjs
NestJS – Test suite failed to run Cannot find module ‘src/article/article.entity’ from ‘comment/comment.entity.ts’
You can tell Jest how to resolve module paths by configuring the moduleNameMapper option, which is useful if you’re using packages like module-alias or if you’re using absolute paths. Add these lines to your Jest configuration: { // … “jest”: { // … “moduleNameMapper”: { “^src/(.*)$”: “<rootDir>/$1” } } } Now modules that start with … Read more
jest config is throwing “type ErrorHandler = (error: mixed, isFatal: boolean) => void” after update to 26.x
Solution found here. Add @react-native to your Jest configuration. Such as: transformIgnorePatterns: [ ‘node_modules/(?!@react-native|react-native)’ ],
How to mock navigator.clipboard.writeText() in Jest?
Jest tests are running in JSdom environment and not all of the properties are defined, but so you should define the function before spying on it. Here is an example: const writeText = jest.fn() Object.assign(navigator, { clipboard: { writeText, }, }); describe(“Clipboard”, () => { describe(“writeText”, () => { beforeAll(() => { navigator.clipboard.writeText.mockResolvedValue(undefined) // or … Read more
How to mock window.navigator.language using jest
window.navigator and its properties are read-only, this is the reason why Object.defineProperty is needed to set window.navigator.language. It’s supposed to work for changing property value multiple times. The problem is that the component is already instantiated in beforeEach, window.navigator.language changes don’t affect it. Using Object.defineProperty for mocking properties manually will require to store original descriptor … Read more
TypeError: require.requireActual is not a function
Jest deprecated require.requireActual a while back and recently removed it in version 26. Downgrade to Jest 25 to fix it, or find what package is using require.requireActual and have them use jest.requireActual instead. EDIT: Storybook v6.0.0-beta.3 includes an update that adds Jest v26 support.
Error is thrown but Jest’s `toThrow()` does not capture the error
expect(fn).toThrow() expects a function fn that, when called, throws an exception. However you are calling CheckFunctionExistenceByStr immediatelly, which causes the function to throw before running the assert. Replace test(` expect(CheckFunctionExistenceByStr( ‘any string’, ‘FunctionThatDoesNotExistsInString’ )).toThrow(); `, () => { expect(CheckFunctionExistenceByStr( ‘any string’, ‘FunctionThatDoesNotExistsInString’ )).toThrow(); } ); with test(` expect(() => { CheckFunctionExistenceByStr( ‘any string’, ‘FunctionThatDoesNotExistsInString’ ) … Read more
React testing library how to use waitFor
If you’re waiting for appearance, you can use it like this: it(‘increments counter after 0.5s’, async() => { const { getByTestId, getByText } = render(<TestAsync />); fireEvent.click(getByTestId(‘button-up’)); await waitFor(() => { expect(getByText(‘1’)).toBeInTheDocument(); }); }); Checking .toHaveTextContent(‘1’) is a bit “weird” when you use getByText(‘1’) to grab that element, so I replaced it with .toBeInTheDocument().
Jest coverage: How can I get a total percentage of coverage?
Thanks to Teneff’s answer, I go with the coverageReporter=”json-summary”. jest –coverage –coverageReporters=”json-summary” This generates a coverage-summary.json file which can easily be parsed. I get the total values directly from the json: “total”: { “lines”: { “total”: 21777, “covered”: 65, “skipped”: 0, “pct”: 0.3 }, “statements”: { “total”: 24163, “covered”: 72, “skipped”: 0, “pct”: 0.3 }, … Read more
Reset single module with Jest
As explained in the question, jest.resetModules() resets the module cache, which is useful your module keeps some local state and you want to get rid of that state between tests. The problem with resetModules is that it resets everything but sometimes you want only some of the modules to be reset. Since Jest 24 you … Read more