Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
0 votes
1 answer
43 views

Is there a difference in module creation if I use export * instead of export {Component1, Component2} in my index.ts

I recently ran into a failing test where I received the following error message" TypeError: Cannot redefine property: getCurrentRouteSnapshot I read the following StackOverflow and found this ...
IMeyers20's user avatar
  • 191
0 votes
1 answer
755 views

Jest : SyntaxError: Cannot use import statement outside a module

I'm writting tests in a Node/TS project, which is a backend API (no babel, no webpack, no react/angular/vue/...). My project is then a CommonJS project. I'm using Jest as a test framework. Everything ...
Nicolas Espiau's user avatar
4 votes
0 answers
679 views

Testing CommonJS with dynamic imports of ESM with ts-jest

Given a library that uses CommonJS module system, but imports ESM (only) modules with a dynamic import (to prevent TS from replacing import with require): export async function loadEsmModule<T>(...
daniel-sc's user avatar
  • 1,307
0 votes
0 answers
1k views

NPM test script fails with "Error [ERR_REQUIRE_ESM]"

I'm trying to update dependencies for my React Node project, and any update to package.json at all, including deleting yarn.lock and installing the same dependencies fresh with yarn, causes yarn test (...
kenzie's user avatar
  • 13
0 votes
0 answers
42 views

Question next.js jest test run support commonjs compile error

ErrorMessage FAIL src/hooks/test.tsx ● Test suite failed to run Jest encountered an unexpected token Jest failed to parse a file. This happens e.g. when your code or its dependencies use ...
brightbong's user avatar
0 votes
0 answers
122 views

Exporting jest config with next/jest yields issue with async code

I'm facing an issue exporting Jest configurations in a Next project, I'm following the Next 14 docs. This is my jest.config.js: const nextJest = require("next/jest") const createJestConfig = ...
maja's user avatar
  • 779
0 votes
1 answer
318 views

how to test @mdx-js/mdx with jest (transformIgnorePatterns not working on 'develop' module)

My ultimate goal is to be able to write tests in Jest that can import from @mdx-js/mdx, which is an ESM module, and since the library package under development is configured for commonsjs, I am using ...
drkstr101's user avatar
  • 807
5 votes
3 answers
7k views

Jest tests are failing because of an unknown unexpected token "export"

I'm using tRPC with Typescript and Next.js. My tests used to not fail until recently (I don't think I touched them). I get the error Unexpected token 'export'. I've tried changing my jest.config.mjs ...
importTurtle's user avatar
1 vote
1 answer
541 views

How to mock default package in dependency with Jest?

My dependency includes os and fs with require. I didn't notice any issues with my unit tests until I ran in a Jenkins env which gave this error: StorageDirectoryResolutionError: Storage directory ...
Dave Stein's user avatar
  • 9,266
5 votes
0 answers
792 views

Create a hybrid npm module for ESM and CommonJS

Let's say I am writing a npm library called my-awesome-lib using Typescript. Because my-awesome-lib depends on a pure-ESM library, I have to use the ESM syntax, i.e. import to consume that 3rd-party ...
HUIJING HUANG's user avatar
0 votes
1 answer
2k views

cannot override function with jest mock function

I'm writing a test for one of my endpoints that invokes a chaincode transaction on hyperledger fabric. For the test, I want to override the invoke service to mock a transaction so that a new ...
Matias Salimbene's user avatar
1 vote
1 answer
146 views

Jest unable to return value from jQuery

I need jest tests to fail if a jQuery selector returns 0. So if there is a typo, the test should fail. Here's my index.js: const sample = $('.someClass'); module.exports = sample; This is how my ...
Waleed Ahmad's user avatar
1 vote
1 answer
482 views

firebase.database.ServerValue.TIMESTAMP : database is undefined

It's been days and I still can't figure out why firebase.database is undefined. chat.js file content const firebase = require('firebase'); require('firebase/database'); exports.sendMessage = (...
Barbeuk's user avatar
  • 41
1 vote
1 answer
896 views

jest.config.js fails - unexpected token '.' in module.exports

I must be missing something but I'm struggling to understand this error. How can . be unexpected in module.exports={}? Please point me in the right direction. Thanks! \jest.config.js:16 module....
Vok's user avatar
  • 467
7 votes
2 answers
5k views

JavaScript: module/require is not defined

I'd like to have Jest testing for some JavaScript code that I'm using in the frontend of my web app. From what I understand, I need to export and import functions from my module so I can test them in ...
mattu's user avatar
  • 1,004
1 vote
1 answer
1k views

CommonJS and Jest Mocking - Exported Function not being mocked

I'm having trouble mocking specific functions within my modules using commonjs example module db.js function createDefaultProfile(user_id) { return { version: 1, username: user_id }; } function ...
Addison Joseph's user avatar
2 votes
1 answer
3k views

Trying to mock a named commonjs module in Jest

I've searched for days on how to accomplish this and nothing i've tried has worked. I have an authenticate module that I share among other modules, and I'm wanting to create a mock for it using jest, ...
CoryDorning's user avatar
  • 1,914
1 vote
0 answers
136 views

Jest mocking module implementation with .mockImplemetation

Can anyone help here. I am really frustrated with how the mockImplementation works. So, first of all I am using jest for node testing. I am using the commonjs modules. what I wanna do is that I am ...
Sadok Mtir's user avatar
1 vote
1 answer
363 views

Jest fails when imports files from repository using import/export default syntax

I'm using some files from another repository which is written in es6 syntax. The current project also is written with es6 syntax, but Babel is configured so tests don't fail on the imports. but when ...
Andrew Korin's user avatar
32 votes
7 answers
83k views

Jest - Mock a constant property from a module for a specific test

So, I'm attempting to do something which on the surface should be very simple... I have some constants defined in: //constants.js module.exports = { MY_CONSTANT: "TEST" } I have a file ...
A.K's user avatar
  • 383
2 votes
1 answer
553 views

CommonJS modules in

Using Nx for my Angular workspace, and noticed something, which raised my question. What's the purpose to use CommonJS modules in all tsconfig.spec.json files for libs? Checking Nx examples all libs ...
Daniel Danielecki - Ditectrev's user avatar
47 votes
2 answers
20k views

Reset single module with Jest

jest.resetModules() clears the require cache for all modules but is there a way to clear it for just a single module? Can't use require.cache as Jest appears to bypass it. I'm testing a Node module ...
Tamlyn's user avatar
  • 23.5k
0 votes
1 answer
1k views

React Jest - import JS module without using require/export

Is it possible to use Jest to test individual JavaScript files without using require/export? Let's say I want to test abc.js. Here is an example of abc.js: (function(){ return { foo: ...
Leo Teng's user avatar