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

15677 questions
500
votes
16 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
  • 14,909
  • 15
  • 55
  • 99
488
votes
25 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
  • 5,295
  • 2
  • 14
  • 9
395
votes
7 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
  • 7,173
  • 6
  • 27
  • 42
355
votes
16 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
  • 7,423
  • 4
  • 25
  • 41
340
votes
7 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
  • 9,746
  • 6
  • 39
  • 71
300
votes
9 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
  • 20,299
  • 9
  • 62
  • 82
228
votes
13 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
  • 2,441
  • 2
  • 7
  • 6
186
votes
7 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
  • 33,336
  • 20
  • 82
  • 92
170
votes
18 answers

How do I deal with localStorage in jest tests?

I keep getting "localStorage is not defined" in Jest tests which makes sense but what are my options? Hitting brick walls.
Chiedo
  • 6,022
  • 3
  • 24
  • 21
161
votes
10 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
  • 27,156
  • 17
  • 99
  • 146
158
votes
4 answers

How can I clear the Jest cache?

Jest is picking up an old version of a package and thus my tests fail unless I use --no-cache. I can even delete the package folder from folder node_modules and Jest is happy to run the tests (almost all are passing). So how do I clear the Jest…
Manuel
  • 8,676
  • 12
  • 47
  • 79
158
votes
11 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
  • 3,098
  • 2
  • 19
  • 27
157
votes
18 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
  • 2,679
  • 3
  • 18
  • 26
147
votes
12 answers

Jest SecurityError: localStorage is not available for opaque origins

When I want to run my project with the command npm run test, I get the error below. What is causing this? FAIL ● Test suite failed to run SecurityError: localStorage is not available for opaque origins at Window.get localStorage [as localStorage]…
amirdehghan
  • 1,391
  • 2
  • 5
  • 4
139
votes
8 answers

How to mock imported named function in Jest when module is unmocked

I have the following module I'm trying to test in Jest: // myModule.js export function otherFn() { console.log('do something'); } export function testFn() { otherFn(); // do other things } As shown above, it exports some named functions…
Jon Rubins
  • 3,573
  • 9
  • 29
  • 48
1
2 3
99 100