In jest, how do I use “toHaveBeenCalledWith” and only match part of an object in an array argument?

You can use a combination of arrayContaining and objectContaining to make this work. Reference: https://jestjs.io/docs/expect#expectarraycontainingarray https://jestjs.io/docs/expect#expectobjectcontainingobject Here is some sample code for you: function something(a, b, somefn) { somefn([{ x: a, y: b, id: ‘some-guid’ }]); } test(‘Testing something’, () => { const mockSomeFn = jest.fn(); something(2, 3, mockSomeFn); expect(mockSomeFn).toHaveBeenCalledWith( expect.arrayContaining([ expect.objectContaining({ x: 2, y: … 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

How to get a Jest custom matcher working in typescript?

The other question and answer you linked to were correct, and you can also find a very succinct example for how to extend jest in this github comment on react-testing-library. To implement their solution for your code, just change: declare global { namespace jest { interface MomentMatchers extends Matchers<moment.Moment> { toBeSameMoment: (expected: moment.Moment) => CustomMatcherResult; … Read more

Upgrading Jest to v29 – Error Test environment jest-environment-jsdom cannot be found

Jest team added more descriptive error message in version 28.0.1 Error: … As of Jest 28 “jsdom” is no longer shipped by default, make sure to install it separately. Installing jsdom package resolves the issue: # npm npm install -D jest-environment-jsdom # yarn yarn add -D jest-environment-jsdom

How to resolve “Cannot use import statement outside a module” from Jest when running tests?

I was having the same failure (also using Babel, Typescript and Jest), it was driving me crazy for hours! Ended up creating a new babel.config.js file specifically for the tests. I had a large .babelrc that wasn’t getting picked up by jest no matter what I did to it. The main app still uses the … Read more

Cannot find name ‘it’ in Jest TypeScript

Install npm install -D jest @types/jest ts-jest jest.config.js — at root module.exports = { roots: [‘<rootDir>/src’], transform: { ‘^.+\\.tsx?$’: ‘ts-jest’, }, testRegex: ‘(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$’, moduleFileExtensions: [‘ts’, ‘tsx’, ‘js’, ‘jsx’, ‘json’, ‘node’], } tsconfig.json { “compilerOptions”: { … “types”: [“reflect-metadata”, “jest”], “typeRoots”: [“./types”, “./node_modules/@types”] … }, “exclude”: [“node_modules”, “**/*.spec.ts”, “**/*.test.ts”], “include”: [“./src/**/*.tsx”, “./src/**/*.ts”] }

Jest won’t transform the module – SyntaxError: Cannot use import statement outside a module

Even though I have tried them separately, I haven’t tried them together (transform and transformIgnorePatterns). So this jest configuration solved my issue: “jest”: { “preset”: “ts-jest”, “testEnvironment”: “node”, “transform”: { “node_modules/variables/.+\\.(j|t)sx?$”: “ts-jest” }, “transformIgnorePatterns”: [ “node_modules/(?!variables/.*)” ] }, My mistakes were: Not using transform and transformIgnorePatterns together. And defining babel-jest as the transformer instead of … Read more

How to resolve “Cannot use import statement outside a module” in jest

Also using Babel, Typescript and Jest. Had the same failure, driving me crazy for hours. Ended up creating a new babel.config.js file specifically for the tests. Had a large .babelrc that wasn’t getting picked up by jest no matter what i did to it. Main app still uses the .babelrc as this overrides babel.config.js files. … Read more