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

How to create a mockable class to connect to mongoDB?

I've tried to create a class to connect to a mongoDB (and get a gridFS connection using (gridfs-stream). But with that I do get two problems: I do get sometimes the mongo Error server instance in invalid state connected It is impossible for me to…
user3142695
  • 15,844
  • 47
  • 176
  • 332
9
votes
2 answers

"unexpected token import" error while running tests

when I run the test, I get the following error: ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react'; …
Iván
  • 401
  • 2
  • 6
  • 17
9
votes
1 answer

How to use doMock in jest?

I find that when using jest.doMock instead of jest.mock to mock a function (I will need to create multiple mock implementations for the same function in different it blocks), I find that the test fails with Error…
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
9
votes
3 answers

Mocking aws-sdk S3#putObject instance method using jest

For the source code containing: const S3 = require('aws-sdk/s3/clients') const s3 = new S3() s3.putObject(params, callback) I have added the following mock based on this article: jest.mock('aws-sdk/s3/clients') const S3 =…
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
9
votes
1 answer

Jest with create-react-app - unexpected token errors

I am working on an application that was recently converted from an old Webpack build to use create-react-app. Most of the transition has went smoothly, but I'm running into some major issues with the previous unit tests. When I run npm test which…
wildlifehexagon
  • 523
  • 6
  • 26
9
votes
3 answers

TypeError: Cannot assign to read only property 'x' of object '#' React/JEST
While I am writing test case for my react component I am getting TypeError: Cannot assign to read only property 'x' of object '#' wherein while the application run it does not throw similiar error The code for it is pretty…
Pragam Shrivastava
  • 1,428
  • 3
  • 13
  • 20
9
votes
4 answers

Invalid Chai property: toMatchSnapshot -- React.js Jest testing

I'm getting an error: Invalid Chai property: toMatchSnapshot when I try to use Jest + Enzyme's snapshot testing. I've updated my React version to 16.2 and I use enzyme-to-json library with Enzyme 3. Code is below: import React from 'react'; import…
Patrick Lu
  • 101
  • 1
  • 5
9
votes
2 answers

Jest - Unexpected token import

I'm trying to learn how to do unit testing with React and jest. I've run into the following error: Unexpected token import Here are my babel presets: { "presets": ["es2015", "stage-0", "react"], "env": { "test": { "plugins":…
red house 87
  • 1,837
  • 9
  • 50
  • 99
9
votes
2 answers

Jest with styled components themeProvider

I'm referring to this github project and I'm trying to write a simple test of KeyPad.js component. I have seen the issues opened on this matter and one suggested solution is to pass the theme as prop to the component. This solution wouldn't work…
Pietro
  • 1,815
  • 2
  • 29
  • 63
9
votes
1 answer

How to execute npm script using grunt-run?

I have a npm task in my package.json file as follows to execute jest testing: "scripts": { "test-jest": "jest", "jest-coverage": "jest --coverage" }, "jest": { "testEnvironment": "jsdom" }, I want to execute this task npm run…
Peter
  • 10,492
  • 21
  • 82
  • 132
9
votes
1 answer

NodeJS - Jest unit test setTimeout in process.on callback

I am trying to unit test a timer using Jest in my process.on('SIGTERM') callback but it never seems to be called. I am using jest.useFakeTimers() and while it does seem to mock the setTimeout call to an extent, it doesn't end up in the…
shiznatix
  • 1,087
  • 1
  • 20
  • 37
9
votes
1 answer

Expected Array but received array in Jest

I created unit (async) test in Jest. But when I get response from server: [ { name: "My name" }, { name: "Another name" } ] and test it: test('Response from server', () => { get('my-url').end(error, response) =>…
Vesmy
  • 1,190
  • 4
  • 15
  • 29
9
votes
2 answers

Jest test .toBeInstanceOf(Error) fails

I have trouble checking whether promise rejects with error in Jest test. (post Jest 20 version:) test("Rejects on error", () => { expect.assertions(1); return expect(promiseHttpsGet(null)).rejects.toBeInstanceOf(Error); }); (pre Jest…
PeterDanis
  • 8,210
  • 2
  • 13
  • 23
9
votes
2 answers

Enzyme cannot find child component in shallow test

Is this not the correct way to render a react/reflux test with enzyme but without the store, ie, "dumb" import React from 'react' import { shallow, render } from 'enzyme' import { Controls } from '../Controls' // named export import LoadingSpinner…
1252748
  • 14,597
  • 32
  • 109
  • 229
9
votes
2 answers

Use scoped packages with Jest

I am developing an app with react-native and typescript and doing the tests with Jest, but I have a problem when I use scoped packages (@assets), jest can not find the path and gives error. The directory structure looks like this: project/ …
Armando
  • 603
  • 1
  • 5
  • 14
1 2 3
99
100