-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jest.spyOn does not work when compared ts-jest #3843
Comments
Hello, Any news or workaround to be able to use jest.spy on with swc ? It sadly blocks to use this amazing work 😢 |
Stuck on this now. |
I was able to get this working by using this pattern:
In test:
I only needed to use this when using the import * pattern like |
I think I've found the problem. we need to look at the transformed code output by you can see the transformed code by putting a conditional breakpoint in Jest, here: recall that our original export const child = () => {
console.log('Hello World!');
};
export const callChild = () => {
child();
};
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.child = exports.callChild = void 0;
const child = () => {
console.log('Hello World!');
};
exports.child = child;
const callChild = () => {
// the `exports.` qualification is critically important for making this reassignable by external integrators!
exports.child();
};
exports.callChild = callChild; whereas 'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.child = exports.callChild = void 0;
const child = () => {
console.log('Hello World!');
};
exports.child = child;
const callChild = () => {
// note the lack of `exports.` qualification -- this means it will always invoke the real implementation
child();
};
exports.callChild = callChild; this means that I think you could get around this by spinning the but yes this is a fixable bug, rooted in how swc transforms ESM to CommonJS. |
to be clear, my recommended workaround (until the bug is fixed) is to split child.ts export const child = () => {
console.log('Hello World!');
}; index.ts import { child } from './child';
export const callChild = () => {
child();
}; and change import { callChild } from '.';
import * as childModule from './child';
describe('index', () => {
it('called', () => {
const spiedChild = jest.spyOn(childModule, 'child');
callChild();
expect(spiedChild).toHaveBeenCalled();
});
}); this ensures that both |
Reopening as the patch is reverted, and the patch was wrong. |
I have runned a local test on my machine and can confirm ts-jest could work fine. However, if I use babel and So I suggest not to rely on specific implmentation of compiler. |
If native esm mode fails, |
Okay yeah, it does seem like const y = () => {
(0, exports.x)();
}; const y = () => {
x();
}; This quirk of But yeah, if the goal of "converting ESM to CommonJS" is "preserve the guarantees of ESM", then the exported module is supposed to be considered immutable anyway. you wouldn't be able to do this type of spying on a real ESM module. So, the "workaround" (extract into separate modules functions upon which you wish to spy) is probably the only solution left. |
Kind of confused what the resolution to this issue is exactly. Tried what @develra mentioned like so:
But then I get this error:
Previously I've had this and it working, but something must have recently changed in a dependency...
|
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Problem
index.ts
index.spec.ts
ts-jest
jest.config.js
Result
@swc/jest
jest.swc.config.js
Result
The text was updated successfully, but these errors were encountered: