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

How to test a GraphQL query providing an auth token with Apollo Server?

Apollo Server Integration testing documentation shows how to test simple queries using createTestClient: const { query } = createTestClient(server); const res = await query({ query: GET_LAUNCH, variables: { id: 1 } }); In the attempt to test a…
Marco Lackovic
  • 6,077
  • 7
  • 55
  • 56
9
votes
1 answer

jest.fn() claims not to have been called, but has

I'm testing a Vue component that calls a certain action in my Vuex store when a certain parameter is present in the route. I'm mocking the action with jest.fn(). Here's the relevant code from the component: await…
mcv
  • 4,217
  • 6
  • 34
  • 40
9
votes
3 answers

How can I make Jest test for a thrown error silently

I’m writing a test to assert that a component throws an error if provided one prop but not another. The test itself passes, but the console still complains about an uncaught error and prints the entire stack trace. Is there a way I can get Jest to…
Jordan Garvey
  • 345
  • 5
  • 16
9
votes
1 answer

How can I mock a library in jest?

I am trying to mock the file-type library in a jest test. In my javascript file, I use this library in the following way: import * as fileType from 'file-type'; .... const uploadedFileType = fileType(intArray); Then, in my jest test, I am…
user3802348
  • 2,502
  • 11
  • 36
  • 49
9
votes
1 answer

Vue-jest can't find babel

I'm trying to test my vue components however I'm always getting the following error: Cannot find module 'babel-core' at Object. (node_modules/vue-jest/lib/compilers/babel-compiler.js:1:15) package.json: "devDependencies": { "@babel/core":…
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
9
votes
3 answers

@types/jest index.d.ts file returning error

I just npm installed @types/jest to my stenciljs starter app and now, when I go to start my project the newly installed node package is returning several errors. Here are the errors it's returning when I go to npm start my project: [ ERROR ] …
cashews
  • 161
  • 1
  • 3
  • 9
9
votes
2 answers

How to read console.log from a mounted component with Enzyme and Jest in Create React App

I have a component I'm mounting with Enzyme. There is an object that is defined inside the constructor of the component. I need to read values inside that object. How do I do this? If I was using the browser, I would use…
9
votes
2 answers

JEST - why mock api calls?

I'm getting familiar with JavaScript testing, and may be missing a point or two related to mocking api calls. Every tutorial I have found mocks api calls when doing unit or integration testing - example: https://jestjs.io/docs/en/tutorial-async I…
9
votes
2 answers

How to call multiple setProps in enzyme?

I have the following test: import React from 'react'; import { configure, shallow } from 'enzyme' import Adapter from 'enzyme-adapter-react-16'; configure({ adapter: new Adapter() }); import ListAdapter from './ListAdapter' import TweetCard from…
Maverick
  • 2,738
  • 24
  • 91
  • 157
9
votes
2 answers

Testing if an array does not contain a value

Is it possible using Jest to validate that userId: 123 should not exist. For example: [ { UserId: 112, Name: "Tom" }, { UserId: 314, Name: "Paul" }, { UserId: 515, Name: "Bill" } ] The test should pass if there's no object with UserId: 123.
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
9
votes
3 answers

Jest test on typescript file syntax error: "interface is a reserved word in strict mode"

My Branch: https://github.com/Futuratum/moonholdings.io/tree/JestTests Current PR: https://github.com/Futuratum/moonholdings.io/pull/29 My Astronaut.tsx component import React from 'react'; import { moonHoldings } from…
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
9
votes
1 answer

Jest how to test express API POST request?

I need to test if my POST request to my endpoint works properly with a Jest test. I had the idea of first getting the count of my Services table (I'm using sequelize orm), then to send a new post request and to finally get the new count and compare…
user9132502
  • 310
  • 1
  • 3
  • 12
9
votes
1 answer

console.log(Wrapper.debug()) not showing the react component

I am using enzyme with jest and trying to print out wrapper.debug but I am not getting output as shown in jest docs. what is wrong here? my test file: import React from 'react'; import Enzyme, {shallow} from 'enzyme'; import EnzymeAdapter from…
sriram hegde
  • 2,301
  • 5
  • 29
  • 43
9
votes
2 answers

Jest - loading text files for string assertions

I am working on a text generator and I would like to compare the generated string with a text stored in a sample files. Files have indentation for some lines and it is very cumbersome to construct these strings in TS/js Is there a simple way to load…
Jar
  • 591
  • 1
  • 5
  • 5
9
votes
1 answer

Unable to test any code using an openlayers' module in Jest

I'm trying to write some tests for some modules that happen to import an openlayers module or two. But as some others have found (here, here, and here), this doesn't work out of the box. Here's what I've tried: renaming .babelrc to babel.config.js…
jktravis
  • 1,427
  • 4
  • 20
  • 36