pnpm add -D jest @jest/globals @types/jest @swc/jest
/** @type {import('jest').Config} */
const config = {
testMatch: [
'<rootDir>/src/**/*.test.ts',
],
moduleFileExtensions: [
'js', 'jsx', 'ts', 'tsx'
],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.jsx?$': '$1',
},
transform: {
'^.+\\.tsx?$': ['@swc/jest', {
// Your SWC config here
}],
},
extensionsToTreatAsEsm: [
'.ts', '.tsx'
],
};
export default config;
import { describe, it, expect } from '@jest/globals';
import { greet } from './greet.js';
describe('encodeTlData()', () => {
it('works', () => {
expect(
greet('John')
).toEqual('Hello John!');
});
});
-
SWC doesn't support
tsconfig.json
, therefore it's lacking from the configuration. -
Why not
jest.config.ts
? Jest requirests-node
to read config in TS format, however, thets-node
is no longer maintained and doesn't support various new features like multiple extends intsconfig.json
. We also like to keep our dependencies minimal. -
SWC is very fast but doesn't type-check! So make sure to check your types inside your IDE or using TypeScript compiler. Always check your types as part of CI/CD (before running tests)!
Please add a star. Also, join my Telegram channel for more useful guides and insights on JavaScript/TypeScript development.
Be my guest to comment in the section below or under posts in my Telegram channel.
Need professional consultancy? You can contact me directly.