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.
22,728
questions
0
votes
0
answers
18
views
jest-preset-angular: Adding ANY transformignorepaths results in errors
We've just upgraded to Angular 16 and our testing suite with jasmine and karma no longer works - much like some other users who have posted issues, our testing suite hangs for a really long time after ...
-1
votes
0
answers
15
views
How can I write unit tests for a date time range picker component?
This component has multiple functions inside, including useEffects and other different states. I know the basic of unit testing with Jest (the describe, test, render, expect, etc. conventions) but how ...
-1
votes
0
answers
8
views
How can I perform nesting tests with jest?
I'm writing a module test that it's tests are too large. I can't put them all in a "describe" block. How can I separate them by putting each test inside a file, then place all of them inside ...
-1
votes
1
answer
22
views
Unexpected token on valid typescript file
When starting jest with a pattern from intelliJ (jest --testNamePattern=MySpecialComponent), i get an error on my jest.config.ts stating
Error: Jest: Failed to parse the TypeScript config file /home/...
1
vote
3
answers
65
views
How to avoid TypeScript error when using closest() in React Testing Library?
In my unit test, I am trying to select the following div so that I can click on it.
<div>
<h1>Avocado Toast</h1>
<p>Avocado toast served on country bread, served with a ...
2
votes
0
answers
27
views
Cookies with Fetch polyfill in Jest
I'm setting up e2e tests in jest, and I'd like to have fetch send my cookies when I call it like:
const userinfo = await fetch('/api/auth/me')
I've set up my jest config with
testEnvironmentOptions: {...
0
votes
0
answers
33
views
How to test a class async method in jest and Typescript
I am using spies to test async methods from a class. In the User model I created a async static method that checking an array of objects of the type User and returns the User if is found.
static ...
-1
votes
0
answers
15
views
Jest Unit test is failing at @sendgrid/image import typescript node.js [closed]
I am using node.js with typescript and jest to test my graphQL API. All the test with jest gets executed successfully except one resolver where I am using email functionalities. I am using sendgrid to ...
-1
votes
0
answers
19
views
Why is jest still failing my test even when the expected matches the received?
I have a test that tests the parameters being passed to a function call, but I don't understand why it's still complaining about the anonymous function:
expect(jest.fn()).toBeCalledWith(......
0
votes
0
answers
17
views
Jest mock a class instance originally imported from a barrel file
I am trying to mock a Vuex store imported by an otherwise-vanilla TypeScript class. For the sake of this question, I think it's safe to ignore the Vuex internals and just try to provide an instance of ...
-1
votes
1
answer
19
views
How can you match on wildcard object keys using Jest matchers?
Presume that you have this value, where you can't predict the value of the keys in foo:
{
context: { version: 1.0 },
value: {
foo: {
afh34q2: [1n, 100n],
}
}
}
How can I write a ...
0
votes
0
answers
28
views
Mocking Injected Class in Jest
I have 4 classes, I use 3 of those classes to instantiate one class.
Like so:
const repo = new PaymentMessageRepository();
const gorepo = new GoMessageRepository();
const sqsm = new ...
0
votes
1
answer
27
views
getAllByRole does not find input element in react native testing library
I have a simple component with two inputs and one button. I was able to test the code with getAllByRole func for buttons. But when I want to find input elements, I got below error :
Unable to find an ...
0
votes
0
answers
6
views
Turborepo/Monorepo multiple jest versions
I've got a turbo repo with two folders, apps and libs. Inside libs I have several packages. I'm trying to upgrade the packages in libs to a newer version of jest one at a time, but I'm struggling to ...
0
votes
0
answers
9
views
Jest Tests failing with TypeError: (0 , _auth(...).getReactNativePersistence) is not a function on Expo 49
Similar to the question from here, I have upgraded to Expo 49 and now some of my Jest tests are failing with the following error:
TypeError: (0 , _auth(...).getReactNativePersistence) is not a ...
0
votes
1
answer
15
views
Cannot spy the useWindowSize property because it is not a function; undefined given instead
I am trying to mock a function so that I can cover the branches based on specific values of a variable in a hook.
This is my test file
import React from 'react';
import { render, screen, fireEvent,...
0
votes
0
answers
11
views
How to inject Vue3 SFC styles into the DOM in a test environment using Jest and JSDom?
I want to create a Jest component test for a Vue3 single-file component where I have access to the style tag included in the .vue file. Using the JSDom Jest environment and the vue-jest transformer ...
0
votes
0
answers
9
views
Jest module import only fails in debug
Using VsCode and jest test runner I can successfully "run" tests but when I try to "debug" them they fail because:
Cannot find module 'components/Modal' from 'src/components/...
-2
votes
0
answers
22
views
How to fix eslint jest/require hook error
I am trying to run an eslint --fix to fix my eslint errors but I keep getting this error
1:1 error This should be done within a hook jest/require-hook
✖ 1 problem (1 error, 0 warnings)
This is ...
1
vote
1
answer
20
views
Using Jest with setTimeout and useFakeTimers
I have a TypeScript async function which must perform a sleep of 1 second between two statements.
It is implemented by this code.
async function systemUnderTest(): Promise<void> {
console....
0
votes
0
answers
13
views
Jest unable to parse inline types
I have a very simple react project that is being scaffolded and for some reason, while vite builds and serves the app just fine. Jest seems to be having a strange typescript issue where it does not ...
0
votes
0
answers
6
views
XState mock createActorContext
I have a provider that looks like:
import { ReactNode } from 'react'
import { createActorContext } from '@xstate/react'
import mainMachine from 'machines/transmitSdk'
export const MainMachineService =...
-1
votes
0
answers
23
views
How to mock `createQueryBuilder` in NestJS tests?
I have this NestJS service method which I need to test.
async getAllTests(orgId: string): Promise<Array<TestEntity>> {
try {
const query = this.repo.createQueryBuilder(this.tableName)...
-1
votes
1
answer
26
views
How to test calls to NestJS Logger?
I have a NestJS project I am trying to add tests for a service. I need to test and verify the calls to the Logger to make sure it logs as expected.
My test.service.ts file is set up like this
@...
0
votes
1
answer
10
views
IntelliJ Ultimate Editor Syntax Highlighting for Jest
I am writing javascript unit tests using Jest in IntelliJ Ultimate.
Everything works great, and I can even run the tests right in the editor. There's just one thing I'd like to fix: the jest functions ...
0
votes
0
answers
7
views
Unable to mock document attributes in jsdom when using ts-jest
I'm using Jest with ts-jest in a jsdom environment. I want to mock the object, which is returned by document.adoptedStyleSheets. This property is not yet supported by jsdom.
I'm unable to mock the ...
0
votes
0
answers
47
views
Jest error : SyntaxError: await is only valid in async functions and the top level bodies of modules [duplicate]
export default class TestClass {
static async fetchFile () {
try {
const res = await fetch(
url.to.file
);
if (!res.ok)
throw new Error(
'Something ...
0
votes
0
answers
22
views
Sharding JestJS tests on Azure DevOps pipelines
I've been using JestJS to scan a codebase, but it's proving to be a little slow. I started to look into this, and found this blog post about being able to 'shard' Jest tests.
A lot of web searching ...
-1
votes
0
answers
16
views
How to test component which uses useForm from antd
I don't know what to mock, because 'classnames' is a dependency of dependency of my component. I have tried to use transformIgnorePatterns and it did't help me.
The code:
import { Button, Form, Input }...
-1
votes
0
answers
11
views
Unable to verify leaf signature in Jest even after providing it in environment
I wrote an npm test file that would test an outgoing HTTP call.
I've printed the NODE_EXTRA_CA_CERTS, it's showing the path to ca.crt file(which is correct, and absolute.
I even tried using process....
-2
votes
0
answers
14
views
Unable to npm install cobertura-merge and istanbul-merge
Trying to install cobertura-merge and istanbul-merge using below commands in my Angular project -
npm install cobertura-merge
npm install istanbul-merge --save-dev
But getting below error-
npm ERR! ...
-1
votes
0
answers
16
views
How to define a generic test with interfaces in Jest then run it for each implementation
I'd like to define abstract tests then use their definitions on each concrete implementation
For example, this is a test on one of my interfaces IPayment
// payment.test.ts
export function ...
1
vote
0
answers
13
views
Why is assigning a listener to process.stdout "error" event produce a memory leak?
As the title says, Jest is recognizing the following pattern as a memory leak:
export const createNodeWriter = () => {
process.stdout.on('error', () => {});
return () => {};
};
Here is ...
-1
votes
0
answers
11
views
How do I make sure that I'm mocking correctly in Jest unit tests in my app?
So I'm working on unit tests for the api endpoints in my app right now. Let's say the endpoint I'm testing is get-user.ts. It relies on certain other modules within my app, the most notable of which ...
0
votes
0
answers
6
views
VTU not reading props object passed to the mount() function
When mounting a component with props object, VTU does not read props from the options object passed to the mount() function and therefore the test always fails.
Otp.vue
<template>
<div ...
0
votes
0
answers
25
views
Jest test Error Receiving 404 but expected 200
I'm writing tests using Jest & Supertest for some API's I've created in Node and I've written the test below but I keep receiving a 404 code instead of the expected 200 and I'm not sure why. I ...
0
votes
0
answers
14
views
Getting error while trying to mock middleware
I wish to mock cookie-session session middle ware. cookie-session is a npm package. I wish to mock this cookie session because I want to mock the session values which route gets after it is passed ...
0
votes
0
answers
14
views
Cannot run unit tests in next 13 with supabase due to: SyntaxError: Unexpected token 'export'
Trying to run this in my test file and I get this error
render(
<RouterContext.Provider value={createMockRouter({}) as NextRouter}>
<SignUpPage />
</RouterContext....
-1
votes
0
answers
11
views
not able to mock navigator.credentials.create in jest
I am writing unit test specs for a method which uses navigator.credentials.create, hence wanted to mock it but I am not able to do it
sample code which is to be mocked
const publicKey = {challenge: ...
0
votes
0
answers
13
views
How can I mock StorageAccessFramework from expo-file-system using Jest
I am importing as per the docs as follows:
import * as FileSystem from 'expo-file-system';
const { StorageAccessFramework } = FileSystem;
On a user request the following code is executed:
const ...
-1
votes
1
answer
29
views
Mocking nested promises with jest
The function that I am creating a test on uses a nested promises and cannot be converted to async await right now
exports.createCampaign = (req, res) => {
const { name } = req.body;
User....
-1
votes
0
answers
15
views
How can I mock third party React hooks when rendering a component with Storybook?
I have a very simple React component that just renders an object returned by the Microsoft Authentication Library (MSAL). I'm just trying to render a user's name.
import { useMsal } from '@azure/msal-...
0
votes
0
answers
19
views
Jest: module not found
I'm using a WASM module which is referenced in package.json as a tgz file:
"ic-vetkd-utils": "file:ic-vetkd-utils-0.1.0.tgz",
After npm install the module is available in ...
0
votes
0
answers
14
views
Mocking one exported function inside one module
Filename : Module.ts
export const functionName(...args){
...some code
functionName1(...params)
...some code
}
export const functionName1(...args){
...some code
}
So these both the ...
0
votes
0
answers
17
views
jest-codemods Errors Out Due to Experimental Syntax
I'm attempting to replace a legacy projects AVA unit tests with Jest. When I run npx jest-codemods on my existing express / @apollo/server project I'm getting this error:
Transformation error (This ...
0
votes
0
answers
15
views
Starting the MongoMemoryServer Instance failed error
I am trying to test an api using mongodb-memory-server, mocha, chai and supertest, Below is my code:
const { MongoMemoryServer } = require("mongodb-memory-server");
const mongoose = require(&...
0
votes
0
answers
15
views
Running Jest/Babel and getting the error Cannot find module
I recently started configuring Jest with Babel so we can run tests for our Angular code. I'm running into an error I can quite find a configuration for.
src/app/admin/metamodel/icon-viewer.component....
0
votes
0
answers
23
views
shelf/[email protected] peer dependency error
I am able to test my api locally with no issues using shelf/jest-mongodb and mongo-memory-server to mock my db. However, when I push to my repo, I get a "conflicting peer dependency" error. ...
0
votes
0
answers
19
views
ReferenceError: setImmediate is not defined with JEST
I have to test next.js app with jest using version 29.
When I try to test some components, I have this error:
ReferenceError: setImmediate is not defined
Note that setImmediate is not used explicitly ...
-1
votes
0
answers
32
views
Unable to perform pointer interaction as the element inherits `pointer-events: none` in jest [closed]
I updated the project libraries. Now all my pointer-events are none. How can I set them all to auto again?
This is the error message I get:
Unable to perform pointer interaction as the element ...