Currently, I am developing a project in Typescript that relies on an external package (written in JavaScript) through npm. During testing, I only want to mock one specific JS file from that package.
Is there a way to achieve this using Jest?
The package.js file includes:
"dependencies": {
...
"@company/product": "1.8.0"
...
Within someFile.ts:
import * as sdk from "@company/product";
I attempted to create a mock like this:
In someFile.spec.ts:
...
const prefs: IPrefs = {
some_property: -1
};
jest.mock(
"@company/product/sdk/api/prefs.js",
(): { Prefs: IPrefs } => {
return {
Prefs: prefs
};
}
);
This is the original "prefs.js" file from the external package:
import ... // I can see in the failed test stack trace
export let Prefs = {
...
};
Despite my efforts, it does not effectively mock the file since I am still seeing the stack trace from the original "prefs.js" provided by the external package.