Questions tagged [jestjs]

Jest is a JavaScript unit testing framework made by Facebook based on Jasmine and provides automated mock creation and a jsdom environment. It's often used for testing React components.

It has a couple of advantages compared to vanilla

  • Automatically finds tests to execute in your source code
  • Automatically mocks dependencies when running your tests
  • Allows you to test asynchronous code synchronously
  • Runs your tests with a fake DOM implementation (via ) so that your tests can be run on the command line
  • Runs tests in parallel processes so that they finish sooner

Resources

22701 questions
827
votes
26 answers

How do I run a single test using Jest?

I have a test 'works with nested children' within the file fix-order-test.js. Running the below runs all the tests in the file. jest fix-order-test How do I run only a single test? The below does not work as it searches for a file of the regex…
vijayst
  • 20,359
  • 18
  • 69
  • 113
817
votes
34 answers

How do I test a single file using Jest?

I am able to test multiple files using Jest, but I cannot figure out how to test a single file. I have: Run npm install jest-cli --save-dev Updated package.json: `{ ... "scripts": { "test": "jest" } ... } Written a number of tests. Running npm…
Musket
  • 8,585
  • 2
  • 16
  • 9
670
votes
10 answers

What is the difference between 'it' and 'test' in Jest?

I have two tests in my test group. One of the tests use it and the other one uses test. Both of them seem to be working very similarly. What is the difference between them? describe('updateAll', () => { it('no force', () => { return…
C.Lee
  • 10,451
  • 9
  • 31
  • 46
510
votes
23 answers

How to test the type of a thrown exception in Jest

I'm working with some code where I need to test the type of an exception thrown by a function (is it TypeError, ReferenceError, etc.?). My current testing framework is AVA and I can test it as a second argument t.throws method, like here: it('should…
bartsmykla
  • 5,271
  • 2
  • 10
  • 6
490
votes
25 answers

Message "Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout"

I'm using Puppeteer and Jest to run some front end tests. My tests look as follows: describe("Profile Tab Exists and Clickable: /settings/user", () => { test(`Assert that you can click the profile tab`, async () => { await…
Asool
  • 13,031
  • 7
  • 35
  • 49
469
votes
10 answers

How can I mock an ES6 module import using Jest?

I want to test that one of my ES6 modules calls another ES6 module in a particular way. With Jasmine this is super easy -- The application code: // myModule.js import dependency from './dependency'; export default (x) => { …
Cam Jackson
  • 11,860
  • 8
  • 45
  • 78
424
votes
10 answers

Can you write async tests that expect toThrow?

I'm writing an async test that expects the async function to throw like this: it("expects to have failed", async () => { let getBadResults = async () => { await failingAsyncTest() } expect(await getBadResults()).toThrow() }) But jest is…
Sean
  • 4,524
  • 2
  • 11
  • 12
371
votes
13 answers

How do you test for the non-existence of an element using jest and react-testing-library?

I have a component library that I'm writing unit tests for using Jest and react-testing-library. Based on certain props or events I want to verify that certain elements aren't being rendered. getByText, getByTestId, etc throw and error in…
SomethingOn
  • 9,813
  • 17
  • 68
  • 107
364
votes
13 answers

How to use ESLint with Jest

I'm attempting to use the ESLint linter with the Jest testing framework. Jest tests run with some globals like jest, which I'll need to tell the linter about; but the tricky thing is the directory structure, with Jest the tests are embedded with…
Retsam
  • 30,909
  • 11
  • 68
  • 90
319
votes
29 answers

How do I set a mock date in Jest?

I'm using moment.js to do most of my date logic in a helper file for my React components but I haven't been able to figure out how to mock a date in Jest a la sinon.useFakeTimers(). The Jest docs only speak about timer functions like setTimeout,…
alengel
  • 4,368
  • 3
  • 21
  • 28
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
308
votes
22 answers

Console.log statements output nothing at all in Jest

console.log statements output nothing at all in Jest. This was working for me yesterday, and all of sudden, it's not working today. I have made zero changes to my config and haven't installed any updates. I'm not using the --forceExit option. Still…
Hina Dawood
  • 3,201
  • 2
  • 10
  • 8
300
votes
17 answers

Test process.env with Jest

I have an application that depends on environmental variables like: const APP_PORT = process.env.APP_PORT || 8080; And I would like to test that for example: APP_PORT can be set by a Node.js environment variable. or that an Express.js application…
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
288
votes
12 answers

How to run Jest tests sequentially?

I'm running Jest tests via npm test. Jest runs tests in parallel by default. Is there any way to make the tests run sequentially? I have some tests calling third-party code that relies on changing the current working directory.
Martin Konicek
  • 39,126
  • 20
  • 90
  • 98
285
votes
9 answers

How to get the code coverage report using Jest?

Is there a way to have code coverage in the JavaScript Jest testing framework, which is built on top of Jasmine? The internal framework does not print out the code coverage it gets. I've also tried using Istanbul, blanket, and JSCover, but none of…
Alex Palcuie
  • 4,434
  • 3
  • 22
  • 27
1
2 3
99 100