Questions tagged [ts-jest]

451 questions
59
votes
8 answers

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

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. …
Logan Shoemaker
  • 787
  • 1
  • 5
  • 10
30
votes
3 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…
Ege
  • 1,033
  • 1
  • 6
  • 22
23
votes
6 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
  • 2,198
  • 5
  • 18
  • 44
15
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
  • 505
  • 1
  • 4
  • 12
15
votes
2 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,081
  • 6
  • 14
12
votes
1 answer

Jest: TypeError: replaceAll is not a function

String.prototype.replaceAll() is a useful method and while building and executing everything works fine. However, all Jest-tests fail with the following error: TypeError: replaceAll is not a function These are my dependencies: "dependencies": { …
leonheess
  • 5,825
  • 6
  • 42
  • 67
10
votes
1 answer

Visual Studio Code won't properly step on TypeScript code

I am trying to configure Visual Studio Code to use TypeScript (in a Node package), I'm also using ts-jest/Jest to create tests, additionally, I have the Jest extension installed. I have been able to figure out how to make breakpoints work, but now I…
Trevör
  • 2,531
  • 1
  • 21
  • 55
10
votes
4 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
  • 891
  • 1
  • 9
  • 26
10
votes
2 answers

Jest passing tests but --covering option not picking up files

Problem description: I have written two tests for a typescript class. Those two tests pass so jest successfully retrieves the test files. I then use the --coverage option but it appears jest is not picking the covered files here. Here is the output…
Aria Groult
  • 945
  • 1
  • 14
  • 31
9
votes
2 answers

Using Jest's todo feature

The release notes for Jest 24 highlighted a new feature that I want to make use of: test.todo. However, for the life of my I am unable to use it. For example, I want to sketch out tests in my subscriptions.test.ts file so I create the…
Mark
  • 1,743
  • 1
  • 23
  • 34
8
votes
1 answer

ts-jest fails to run a tsx test file because of "Import" from a module's js file

I'm trying to use ts-jest to run a tsx test file form.spec.tsx. The form.spec.tsx imports React Quill editor and some of the plugins. How can I bypass SyntaxError: Unexpected identifier error coming from a plugin called quill-mention that import…
RedGiant
  • 4,036
  • 6
  • 44
  • 117
8
votes
1 answer

Where should I put the index.d.ts file?

I am writing a nodeJS service that uses a bunch of npm modules without @types. The tsc error message tells me that I need to add index.d.ts file, but it doesn't tell me where to put it. My helper.spec.ts file which also imports the same modules also…
Jake
  • 10,633
  • 20
  • 79
  • 134
7
votes
1 answer

Jest & TypeScript: VS Code can't find names

I am using Jest with TypeScript. Despite the fact that my code works and I can build my project, Visual Studio Code throws me that error for all Jest methods (describe(), test()...): Cannot find name 'describe'. Do you need to install type…
Alpha60
  • 161
  • 7
7
votes
1 answer

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…
7
votes
1 answer

React Jest test fails to run with ts-jest - Encountered an unexpected token on imported file

I'm new to testing React/Typescript apps. I want to unit test my React components, because I'm not satisfied to develop apps without tests at all. The app itself works fine, it's just failing to run in test mode. command (alias for react-scripts…
naXa
  • 26,677
  • 15
  • 154
  • 213
1
2 3
29 30