All Questions
23 questions
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 ...
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 ...
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>(...
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 (...
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 ...
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 = ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 = (...
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....
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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: ...