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
9
votes
3 answers

React Native - mocking FormData in unit tests

I'm having issues testing my thunks, as many of my API calls are using FormData, and I can't seem to figure out how to mock this in tests. I'm using Jest. My setup file looks like this: import 'isomorphic-fetch'; // Mocking the global.fetch…
jhm
  • 4,379
  • 5
  • 33
  • 49
9
votes
1 answer

Jest can not deal with insertAdjacentElement?

I want to test a quite simple JS function export function displaySpinner() { const loadingOverlayDOM = document.createElement('DIV'); const spinner = document.createElement('IMG'); loadingOverlayDOM.id =…
RSeidelsohn
  • 1,149
  • 18
  • 33
9
votes
3 answers

How to unit test a React component that renders after fetch has finished?

I'm a Jest/React beginner. In jest's it I need to wait until all promises have executed before actually checking. My code is similar to this: export class MyComponent extends Component { constructor(props) { super(props); …
Antonis Christofides
  • 6,990
  • 2
  • 39
  • 57
9
votes
5 answers

How to fix Jest "No Tests Found" on windows 10?

I am trying to use Jest on my windows 10 desktop computer, but it keeps telling me that there are no tests found. On my windows 10 laptop, it works just fine. Here is the output I am getting on my desktop: C:\app> jest No tests found In C:\app …
Andy Hansen
  • 313
  • 3
  • 8
9
votes
4 answers

Jest - Cannot find module 'setupDevtools' from 'setup.js'

I am studying Jest and i am trying to add it to my component => github.com/bolket/react-native-scrollview-smart. When i start my test i have this error: $ jest FAIL lib/ScrollViewSmart.test.js ● Test suite failed to run Cannot find module…
SaroVin
  • 1,583
  • 3
  • 23
  • 46
9
votes
3 answers

How can I test Thunk actions with Jest?

I'm new to unit testing Redux-Thunk async actions using Jest. Here is my code: export const functionA = (a, b) => (dispatch) => { dispatch({ type: CONSTANT_A, payload: a }); dispatch({ type: CONSTANT_B, payload: b }); } How can I test this…
Voi Mập
  • 779
  • 3
  • 7
  • 22
9
votes
2 answers

JestJS -> Invariant Violation: Could not find "store" in either the context or props of "Connect(Portfolio)"

The full error message: Invariant Violation: Could not find "store" in either the context or props of "Connect(Portfolio)". Either wrap the root component in a , or explicitly pass "store" as a prop to "Connect(Portfolio)". Not sure why I'm…
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
9
votes
1 answer

Jest fails to transpile import from npm linked module

I have a project with multiple modules (using Lerna) and I want to use Jest to run tests. However, when I test code that uses a shared module (npm linked module via Lerna) it seems that Babel is not correctly applied and I get the following…
Florian Eckerstorfer
  • 1,526
  • 1
  • 14
  • 21
9
votes
1 answer

.toBeInstanceOf(String) on buffer.toString() in Jest tests?

How can I use expect(str).toBeInstanceOf(String) in a Jest assertion for a string that has been created using Buffer#toString()? Or is the correct thing to do here expect(typeof str).toEqual('string') instead? Details: This test case, using typeof,…
bguiz
  • 27,371
  • 47
  • 154
  • 243
9
votes
2 answers

Run jest with electron instead of node

To make a long story short, I'd like to run my jest tests (using CLI) with electron instead of node. It's relevant when using native module, because you need to build them using electron header while jest run them using plain node. So I must either…
Eric Burel
  • 3,790
  • 2
  • 35
  • 56
9
votes
4 answers

How to unit test Promise catch() method behavior with async/await in Jest?

Say I have this simple React component: class Greeting extends React.Component { constructor() { fetch("https://api.domain.com/getName") .then((response) => { return response.text(); }) …
rfgamaral
  • 16,546
  • 57
  • 163
  • 275
9
votes
1 answer

Manual mock React-Intl with Jest to have snapshot testing

I have been struggling mocking React-Intl library with Jest because I'm having this error when I run tests: Invariant Violation: [React Intl] Could not find required `intl` object. needs to exist in the component ancestry. The…
Albert Olivé Corbella
  • 4,061
  • 7
  • 48
  • 66
9
votes
1 answer

Mock a method of a service called by the tested one when using Jest

I am trying to mock a method's service i export as a module from my test. This is something i use to do with "sinon", but i would like to use jest as much as possible. This is a classic test, i have an "authentication" service and a "mailer"…
Ludo
  • 5,060
  • 15
  • 53
  • 85
9
votes
3 answers

Test suite failed to run. Invariant Violation: _registerComponent(...): Target container is not a DOM element

I am trying to test my react component's render, but getting the error below: Invariant Violation: _registerComponent(...): Target container is not a DOM element. Error doesn't exist if i change the place where follows render my component on…
ventro_art
  • 574
  • 1
  • 4
  • 16
9
votes
4 answers

JEST and ES6 import - root folder based imports does not working

I have a React project based on React Redux Starter Kit. In Jest tests: when I trying to import something with root-based path like "components/Link" - it does not work. Only relative paths are working. Putting { "jest": { "rootDir": "/src"…
Rustam
  • 1,875
  • 2
  • 16
  • 33
1 2 3
99
100