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
101
votes
18 answers

Cypress causing type errors in jest assertions

I had been using react-testing-library as well as @testing-library/jest-dom/extend-expect. I installed Cypress yesterday, and now I'm getting Typescript errors on all my jest matchers: Property 'toEqual' doesn't exist on type 'Assertion'. Did you…
Jonathan Tuzman
  • 11,568
  • 18
  • 69
  • 129
99
votes
10 answers

How to spy on a default exported function with Jest?

Suppose I have a simple file exporting a default function: // UniqueIdGenerator.js const uniqueIdGenerator = () => Math.random().toString(36).substring(2, 8); export default uniqueIdGenerator; Which I would use like this: import uniqueIdGenerator…
thisismydesign
  • 21,553
  • 9
  • 123
  • 126
98
votes
11 answers

`regeneratorRuntime` is not defined when running Jest test

The title pretty much explains what I'm facing. I'm trying to test a React component that has some state, and I attempt to provide my store to the component in order to get what it needs. When I run the test of the component using Jest, I get the…
rafafan2010
  • 1,559
  • 2
  • 14
  • 23
97
votes
4 answers

How to mock react custom hook returned value?

Here is my custom hook: export function useClientRect() { const [scrollH, setScrollH] = useState(0); const [clientH, setClientH] = useState(0); const ref = useCallback(node => { if (node !== null) { …
Homa
  • 3,417
  • 2
  • 19
  • 24
96
votes
10 answers

How to query by text string which contains html tags using React Testing Library?

Current Working Solution Using this html:

Name: Bob (special guest)

I can use the React Testing Library getByTestId method to find the…
Beau Smith
  • 33,433
  • 13
  • 94
  • 101
96
votes
9 answers

How can I unit test non-exported functions?

In a JavaScript ES6-module, there may be many, small, easy-to-test functions that should be tested, but shouldn't be exported. How do I test functions in a module without exporting them? (without using Rewire).
Jordan
  • 3,813
  • 4
  • 24
  • 33
96
votes
1 answer

How to pass variable from beforeEach hook to tests in jest?

beforeEach(async () => { const sandbox = sinon.sandbox.create() ... }) test('/add', () => { // how can I use sandbox here? }) What I need is something like t.context in ava
wong2
  • 34,358
  • 48
  • 134
  • 179
96
votes
6 answers

How to mock/replace getter function of object with Jest?

In Sinon I can do the following: var myObj = { prop: 'foo' }; sinon.stub(myObj, 'prop').get(function getterFn() { return 'bar'; }); myObj.prop; // 'bar' But how can I do the same with Jest? I can't just overwrite the function with…
I_like_foxes
  • 1,179
  • 1
  • 7
  • 12
95
votes
13 answers

Jest did not exit one second after the test run has completed using express

I'm using JEST for unit testing my express routes. While running the yarn test all my test case are getting passed, but I'm getting an error Jest did not exit one second after the test run has completed. This usually means that there are…
Aw3 Sol
  • 1,063
  • 2
  • 8
  • 9
94
votes
2 answers

How to test an exception was not thrown with Jest?

The Jest docs do not demonstrate a way of asserting that no exception was thrown, only that one was. expect(() => ...error...).toThrow(error) How do I assert if one was not thrown?
user9993
  • 5,833
  • 11
  • 56
  • 117
94
votes
7 answers

Jest setup "SyntaxError: Unexpected token export"

I'm implementing tests into an existing project that currently has no tests. My tests are failing to compile node_modules/ imports. /Users/me/myproject/node_modules/lodash-es/lodash.js:10 export { default as add } from…
Cory Robinson
  • 4,616
  • 4
  • 36
  • 53
93
votes
6 answers

eslint throws `no-undef` errors when linting Jest test files

I'm using Jest to write some specs and ESLint to lint the styling. For my foo.spec.js tests, eslint keeps throwing the following errors. It seems to think that jest, beforeEach, afterEach, etc... are not defined in that file. 11:1 error …
user2490003
  • 10,706
  • 17
  • 79
  • 155
93
votes
10 answers

Using .env files for unit testing with jest

Is it possible to load environment variables from an env file for unit testing purposes in Jest? I'm looking to run a series of tests on it like so: // unit tests for env file describe('env', () => { it('should have a client id', () => { …
Ronan Quigley
  • 1,081
  • 1
  • 8
  • 13
93
votes
8 answers

Jest, Enzyme: Invariant Violation: You should not use or withRouter() outside a

I have a which outputs one component and list of contacts presentated by . The problem is that in the test for when I try to mount it, test outputs an error Invariant Violation:…
Maria Piaredryj
  • 1,584
  • 3
  • 16
  • 35
91
votes
7 answers

How to fix Error: Not implemented: navigation (except hash changes)

I am implementing unit test for a file that contain window.location.href and I need to check it. My jest version is 22.0.4. Everything is fine when I run my test on node version >=10 But I get this error when I run it on v8.9.3 console.error…
Hoang Subin
  • 6,610
  • 6
  • 37
  • 56