Questions tagged [ts-jest]

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.

1505 questions
308
votes
26 answers

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

I have a React application (not using Create React App) built using TypeScript, Jest, Webpack, and Babel. When trying to run yarn jest, I get the following error: I have tried removing all packages and re-adding them. It does not resolve this. I…
Logan Shoemaker
  • 3,377
  • 2
  • 13
  • 11
147
votes
18 answers

Jest won't transform the module - SyntaxError: Cannot use import statement outside a module

I couldn't get rid of this SyntaxError: Cannot use import statement outside a module error no matter what I have tried and it got so frustrating. Is there anybody out here solved this issue? I have read a million stackoverflow and github issue…
dugong
  • 3,690
  • 4
  • 11
  • 27
102
votes
1 answer

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

Has anyone successfully upgraded to latest Jest version 29? I'm receiving an error: Error: Test environment jest-environment-jsdom cannot be found. Make sure the testEnvironment configuration option points to an existing node module.
marko424
  • 3,839
  • 5
  • 17
  • 27
59
votes
12 answers

Cannot find name 'it' in Jest TypeScript

I try to create an intial setup for Jest in React + TypeScript. I have completed the initial setup and try to check whether the test runs. When I run the test using the command npm test, I am getting the following error: Cannot find name 'it'. Do…
Nidhin Kumar
  • 3,278
  • 9
  • 40
  • 72
49
votes
8 answers

TypeError: Jest: a transform must export a `process` function

Recently upgrading my app to Angular 11. Jest has been set up as the default testing framework. Running npm test results in the following error: ● Test suite failed to run TypeError: Jest: a transform must export a `process` function. 20…
user2031428
  • 1,039
  • 2
  • 9
  • 13
44
votes
1 answer

How to get a Jest custom matcher working in typescript?

I regularly have unit tests where I need to compare two moment objects. I'd us moment's built-in function moment.isSame(moment) to compare them. However, this means my assertion will look like this: expect(moment1.isSame(moment2)).toBeTrue(); I…
Martao
  • 795
  • 2
  • 6
  • 12
37
votes
4 answers

Typescript paths not resolving when running jest?

Attempting to convert this project over to jest using these instructions. I have everything working except for the files that use the paths configuration: "paths": { "@fs/*": ["./src/*"], "@test/*": ["./test/*"] } It looks as if…
Ole
  • 41,793
  • 59
  • 191
  • 359
36
votes
4 answers

Configure jest timeout once for all tests

According to the docs one can increase the default async timeout from 5000ms using the jest-object More specifically, by using the jestsettimeouttimeout The issue I am facing is I am running a series of tests against an API that is very slow, 5-15…
user1037355
28
votes
9 answers

Jest test passed but get Error: connect ECONNREFUSED 127.0.0.1:80 at the end

I'm using node with TypeScript on my back end and Jest and Supertest as my test framework on my back end. When I'm trying to test I have the result pass but I get an error at the end. Here's the result: PASS …
aRtoo
  • 1,686
  • 2
  • 18
  • 38
27
votes
7 answers

TypeError: Jest: a transform must export something

After configuring jest, node.js and typescript the script npx jest throws the error in a console TypeError: Jest: a transform must export something. at C:\projects\project-api\node_modules\@jest\transform\build\ScriptTransformer.js:386:19 at…
Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68
26
votes
1 answer

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

I'm using Typescript and Jest. In Jest, if I want to check if my function was called I can run expect(myMockFn).toHaveBeenCalledWith(arrayArgument); I want to check if my function was called with an array argument that contains an object with some…
Dave
  • 15,639
  • 133
  • 442
  • 830
26
votes
3 answers

How to strongly type jest mocks

I would like to strongly type my jest mocks. To a certain extent, I can make it work but when a class has private properties I'm stuck. An additional question, when I use mocks (the way I currently do) the return type is of the original type but…
Michael
  • 1,201
  • 1
  • 8
  • 15
24
votes
1 answer

Jest, ts-jest, typescript with ES Modules import : cannot find module

I have hard time getting jest to work with typescript project that use ES modules with import syntax. My project was initially written for commonjs, jest test run fine. But then I decided to switch to ES Modules(for learning purpose), jest is not…
koonfoon
  • 433
  • 4
  • 12
23
votes
2 answers

Why isn't ts-jest loading my custom tsconfig file?

For some reason, my custom tsconfig file isn't being picked up in jest.config.ts. Here is my jest.config.ts: module.exports = { setupFilesAfterEnv: [`./jest.setup.ts`], testEnvironment: `jsdom`, roots: [ `/test` ], testMatch:…
the8thbit
  • 601
  • 1
  • 5
  • 20
22
votes
3 answers

Nest can't resolve dependencies in the RootTestModule context when I use Bazel Test

I need to run my tests in the bezel. how can I solve this mysterious problem? I have a nestjs project contains multiple apps and libs. When I run the test yarn jest --config ./jest.config.json libs/lib1, it works perfectly. However when I run with…
Hossein Mayboudi
  • 395
  • 1
  • 4
  • 12
1
2 3
99 100