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
2 answers

Module /node_modules/jest-preset-angular/preprocessor.js in the transform option was not found

When switching from karma-jasmine to jest for my Angular 7 application, I am getting the following error : Validation Error: Module /node_modules/jest-preset-angular/preprocessor.js in the transform option was not found. …
jackfr0st
  • 551
  • 2
  • 8
  • 19
9
votes
5 answers

Jest + Material-UI : Correctly mocking useMediaQuery

I'm using Material-UI's useMediaQuery() function in one of my components to determine the size prop to use for a
Jake
  • 3,865
  • 5
  • 25
  • 58
9
votes
4 answers

How to allow partial TypeScript types when mocking functions - Jest, TypeScript

I have a function which I would like to mock for testing purposes in TypeScript. In my tests, all I care about are the json and the status. However, when using Jest's jest.spyOn the type of my mocked function is set to return a http Response type.…
James Mulholland
  • 1,782
  • 1
  • 14
  • 21
9
votes
1 answer

TypeScript errors doesn't show when running tests through JEST with create-react-app

As the title says, when writing incorrect TypeScript-code in a project set up with create-react-app, I don't get any errors in the terminal when running the tests through npm test. Maybe this is expected behaviour? Would however be nice to get the…
9
votes
2 answers

How to mock or assert whether window.alert has fired in React & Jest with typescript?

I am using jest tests to test my React project written in #typescript created with Create React App. I'm using react-testing-library. I have a form component which shows an alert if the form is submitted empty. I wanted to test this feature (maybe)…
Mayur Dhurpate
  • 1,112
  • 4
  • 16
  • 34
9
votes
1 answer

How do I mock the QueryIterator returned by the DynamoDb Datamapper with Jest?

I'm trying to write unit tests for a TypeScript class that calls DynamoDb. This is what I've got so far. import {DataMapper, QueryIterator} from '@aws/dynamodb-data-mapper'; import DynamoDB =…
StolenKitten
  • 337
  • 1
  • 10
9
votes
2 answers

"ReferenceError: waitForElement is not defined" when testing react.js

I am testing with Jest and react-testing-library a component that is calling an async function. When I run the test I get the error ReferenceError: waitForElement is not defined Following this instructions I have tried: without the…
jamomani
  • 903
  • 1
  • 7
  • 19
9
votes
2 answers

create-react-app jest encountered unexpected token {

I'm developing a React app with a QR-scanner in it with create-react-app. I've added the module react-qr-reader which in turn uses the modules webrtc-adapter. It all works great, until I run yarn test. Then it shows me this error: I've already…
CherryNerd
  • 1,258
  • 1
  • 16
  • 38
9
votes
1 answer

Unit Testing Controllers use Jest, NodeJS

I want to check a case that certain routes are calling the correct controller use Jest specific (mock or spy). It is case specific for unit testing. Somebody can help me how to check it use jest. I don't need verify kind of expect (status code or…
Roman Stefanko
  • 365
  • 2
  • 3
  • 8
9
votes
1 answer

Mock an entire module with Jest in Javascript

I searched for a very long time how to mock any module with jest (like rewire does). I finally manage to do it this way, and it work like a charm : jest.mock('common/js/browser-utils', () => ({ openBrowser: jest.fn() })); const { openBrowser:…
oopinou
  • 143
  • 1
  • 1
  • 9
9
votes
4 answers

Jest doesn't run -- hangs indefinitely

I was originally having this problem with create-react-app, so I did really bare bones setup just of jest: created new directory yarn init in that directory yarn add jest created new file sum.js: function sum(a, b) { return a + b; } …
Coco
  • 1,550
  • 3
  • 24
  • 43
9
votes
3 answers

How to test the args passed into document.body.appendChild with jest

I have a function that creates a script element and adds it to the body. It looks a bit like this: const s = document.createElement('script'); s.type = 'text/javascript'; s.src = 'https://myscript'; s.id = 'abc'; document.body.appendChild(s); I'm…
Christopher Moore
  • 3,071
  • 4
  • 30
  • 46
9
votes
4 answers

How to print the request and response when a test fails in Jest?

I'm using Supertest and Jest to test a Node.js API. A sample test has the following format it('Read a note for a user', (done) => { request(graphqlURL) .post('/graphql') .set(baseHeaders()) .send({ query:…
dsinecos
  • 571
  • 6
  • 13
9
votes
1 answer

Is there a way to generate Jest / Istanbul coverage report in junit.xml format

I'd like to use Gitlab CI for tracking / embedding the coverage analysis in merge requests. The gitlab-ci.yml artifacts:reports:junit config option seems to be suitable for this task. However, this requires the coverage output to be in junit.xml…
lipp
  • 5,586
  • 1
  • 21
  • 32
9
votes
1 answer

jest snapshot breaks with emotion 10 / babel 7 using enzyme

So I had jest snapshots working properly generating css and html in the snapshots with babel 6 / emotion 9 however I need wanted to update to babel 7 and emotion 10 but my snapshot tests with enzyme no longer work. The code compiles and works fine…
ryanzec
  • 27,284
  • 38
  • 112
  • 169