Open joquijada opened 2 years ago
+1
Can confirm, using esbuild-runner
with tests that need to jest.mock
imported functions will break things. If you want to quickly reproduce this problem you can setup any test that required mocking imported functions
// something.test.ts
import { importedFunction } from './otherFile'
jest.mock('./otherFile', () => ({
importedFunction: jest.fn().mockResolvedValue(true),
}))
any solution for this?
I replaced @swc/jest transformer with esbuild-runner/jest and jest.mock no longer works correctly
some.test.ts
import { myModule } from "@/someDir";
import { myFunction } from "@/myFunction";
jest.mock("@/someDir")
test("test1", () => {
(myModule as Jest.MockedFunction).mockImplementationOnce(() => Promise.resolve(true));
myFunction();
expect(myModule).toHaveBeenCalledTimes(1)
});
@/myFunction.ts
import { myModule } from "@/someDir";
export function myFunction() {
// other code...
myModule();
}
I created an issue/question for this in SST at https://github.com/serverless-stack/serverless-stack/issues/1065. I'm wondering if I'm doing something wrong or if this just a shortcoming of
esbuild-runner
. How come thejest.mock()
calls are not hoisted to the top in the generated JavaScript files of Jest tests written in TypeScript? Below is snippet of the resulting JavaScript, notice thejest.mock()
calls appear towards the middle, nowhere near the top as Jestbabel-jest
transformer for example would do,