admin管理员组文章数量:1293511
Hi I am trying to mock a function from a module within my node_modules. The current way I have tried is
import { getPriceImpactForPosition } from "@gmx-io/sdk/utils/fees/priceImpact.js";
import { getBasisPoints } from "@gmx-io/sdk/utils/numbers.js";
// Move these mocks before any imports
jest.mock("@gmx-io/sdk/utils/fees/priceImpact.js", () => ({
getPriceImpactForPosition: jest.fn()
}));
jest.mock("@gmx-io/sdk/utils/numbers.js", () => ({
getBasisPoints: jest.fn()
}));
but when I call .mockReturnValue on the mock it says it is not a function. I assume that it is not being mocked properly, and is still calling the actual implementation.
Below is my jest.config.js file
/** @type {import('ts-jest').JestConfigWithTsJest} **/
export default {
preset: "ts-jest/presets/js-with-ts-esm",
testEnvironment: "node",
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
transformIgnorePatterns: ["node_modules/(?!(@gmx-io/sdk))"],
extensionsToTreatAsEsm: [".ts", ".jsx"],
transform: {
"^.+\\.(ts|js|mjs)$": ["ts-jest", {
useESM: true,
tsconfig: "tsconfig.json",
}],
},
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1"
}
};
I had a look into the package as well to find the functions that I am trying to mock, it seems that the module itself is using package exports and I am not sure if this will affect mocking in anyway
"exports": {
".": {
"import": "./build/src/index.js",
"require": "./build/src/index.js"
},
"./abis/*": "./build/abis/*",
"./prebuilt/*": "./build/prebuilt/*",
"./utils/*": "./build/src/utils/*",
"./types/*": "./build/src/types/*",
"./configs/*": "./build/src/configs/*"
},
This is currently how I am running my tests
NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests
Hi I am trying to mock a function from a module within my node_modules. The current way I have tried is
import { getPriceImpactForPosition } from "@gmx-io/sdk/utils/fees/priceImpact.js";
import { getBasisPoints } from "@gmx-io/sdk/utils/numbers.js";
// Move these mocks before any imports
jest.mock("@gmx-io/sdk/utils/fees/priceImpact.js", () => ({
getPriceImpactForPosition: jest.fn()
}));
jest.mock("@gmx-io/sdk/utils/numbers.js", () => ({
getBasisPoints: jest.fn()
}));
but when I call .mockReturnValue on the mock it says it is not a function. I assume that it is not being mocked properly, and is still calling the actual implementation.
Below is my jest.config.js file
/** @type {import('ts-jest').JestConfigWithTsJest} **/
export default {
preset: "ts-jest/presets/js-with-ts-esm",
testEnvironment: "node",
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
transformIgnorePatterns: ["node_modules/(?!(@gmx-io/sdk))"],
extensionsToTreatAsEsm: [".ts", ".jsx"],
transform: {
"^.+\\.(ts|js|mjs)$": ["ts-jest", {
useESM: true,
tsconfig: "tsconfig.json",
}],
},
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1"
}
};
I had a look into the package as well to find the functions that I am trying to mock, it seems that the module itself is using package exports and I am not sure if this will affect mocking in anyway
"exports": {
".": {
"import": "./build/src/index.js",
"require": "./build/src/index.js"
},
"./abis/*": "./build/abis/*",
"./prebuilt/*": "./build/prebuilt/*",
"./utils/*": "./build/src/utils/*",
"./types/*": "./build/src/types/*",
"./configs/*": "./build/src/configs/*"
},
This is currently how I am running my tests
NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests
Share
Improve this question
asked Feb 12 at 17:08
DorkMonstuhDorkMonstuh
8532 gold badges19 silver badges39 bronze badges
1
- It won't work the way you do this with native esm. See jestjs.io/docs/ecmascript-modules – Estus Flask Commented Feb 12 at 17:49
1 Answer
Reset to default 1Use jest.unstable_mockModule()
instead of jest.mock()
for ESM compatibility.
Ensure mocks are set up before importing the module using await import()
.
本文标签: javascriptJest mock mockReturnValue not a functionStack Overflow
版权声明:本文标题:javascript - Jest mock mockReturnValue not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741580217a2386526.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论