Robert Gardner
06/04/2022, 4:20 PM// foo.test.ts
import { create } from './service';
jest.mock('./service');
console.log(create);
// logs [AsyncFunction: create] instead of [Function: mockConstructor]
We are using ts-jest and esbuild-runner. Here is jest.config.js:
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/*.test.(ts|tsx|js)'],
testPathIgnorePatterns: ['node_modules', 'build'],
transform: {
'\\.ts$': 'esbuild-runner/jest',
},
}
There is a jest issue that says you can’t mock modules without using Babel, but doesn’t esbuild-runner/jest handle that?
Mocking modules is so common in jest that I’m surprised this isn’t working. There must be something I’m missing in our setup??Robert Gardner
06/04/2022, 5:11 PMmodule: "esnext"
. Switched it to module: "commonjs"
and now mocking works. I’m not sure how I ended up with “nextjs”.
I also had to remove “transform” from my jest.config.js, and remove ts-jest type checking with isolatedModules: true
since it now no longer recognizes my moduleNameMapper
settings (that were working before all these changes).Frank