Jest typescript tests runs twice, one for ts files, and one for js files

Is this normal in ts-jest or am I missing some extra configuration You should set roots to /src only. Here is a good config: module.exports = { “roots”: [ “<rootDir>/src” ], “transform”: { “^.+\\.tsx?$”: “ts-jest” }, “testRegex”: “(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$”, “moduleFileExtensions”: [ “ts”, “tsx”, “json” ], } I also only test .tsx? files (no .jsx?) šŸ˜‰

Jest fake timers with promises

The current best alternative is to use the async versions of fake-timers. So you would do await clock.tickAsync(1000); // doesn’t wait 1000ms but is async Instead of calling clock.tick. Please see the answer below for more details. At the moment, it’s not supported You’re not doing anything wrong – it doesn’t work at the moment … Read more

Jest+React Native Testing Library: How to test an image src?

If you’re using @testing-library/react rather than @testing-library/react-native, and you have an alt attribute on your image, you can avoid using getByDataTestId and instead use getByAltText. it(‘uses correct src’, async () => { const { getByAltText } = await render(<MyComponent />); const image = getByAltText(‘the_alt_text’); expect(image.src).toContain(‘the_url’); // or expect(image).toHaveAttribute(‘src’, ‘the_url’) }); Documentation. Unfortunately, it appears that … Read more

Jest: Testing window.location.reload

Updated Answer (November 2021) Package: “jest”: “^26.6.0” “@testing-library/jest-dom”: “^5.11.4” Build: create-react-app 4 describe(“test window location’s reload function”, () => { const original = window.location; const reloadFn = () => { window.location.reload(); }; beforeAll(() => { Object.defineProperty(window, ‘location’, { configurable: true, value: { reload: jest.fn() }, }); }); afterAll(() => { Object.defineProperty(window, ‘location’, { configurable: true, … Read more

How to publish Jest Unit Test Results in VSTS tests?

I’ve used a different approach, b/c after some research I found that the Jest testResultsProcessor property is deprecated. I’m using the jest-junit package for test reports (which has been worked on more recently than the jest-trx-results-processor, fwiw): Add jest-junit to package.json Eg yarn add -D jest-junit or npm add –save-dev jest-junit Add a VSTS task … Read more

Typescript paths not resolving when running jest?

I wanted to resolve modules paths starting with ~/ to my <baseUrl>/<moduleName>. Thank to OJ Kwon link I solved it with (give him point). tsconfig.json see module-resolution path-mapping doc { “compilerOptions”: { “baseUrl”: “src”, “paths”: { “~/*”: [“*”] } }, } jest config Then we need to tell jest to resolve the paths too. It’s … Read more

“Navbar refers to a value, but is being used as a type here” when trying to render a shallow copy of my component when testing

Have you tried to change the name of the file? MyComponent.test.tsx Also, did you install the types of jest and stuff npm i -D @types/jest. I mean I’m saying this because if you look at the jest config where it says testRegex. You have it like this __tests__/*.(test|spec).txs the test must be inside a tests … Read more

TypeError: Cannot read property ‘contextTypes’ of undefined

This would be the same error TypeError: Cannot read property ‘contextTypes’ of undefined when you are importing something that does not exist. Here is an example: AccountFormComponent.jsx (incorrect class name): export class FoeFormComponent extends React.Component { …. } AccountFormComponent.test.jsx: import { shallow } from ‘enzyme’ import { expect, assert } from ‘chai’ import { AccountFormComponent … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)