In an earlier post we explained how we moved from ts-node to tsx.
For testing we were using Jest. Unfortunatly Jest relies on ts-node for reading typescript configuration files. We used ts-jest for the configuration and even then we needed quite some configuration in jest.config.ts to work with ESM:
import type { JestConfigWithTsJest } from 'ts-jest'
const jestConfig: JestConfigWithTsJest = {
// [...]
preset: 'ts-jest/presets/js-with-ts-esm', // or other ESM presets
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
},
],
},
}
export default jestConfig
- vitest
- node builtin testing
vitest
Vitest is built by the Vue community, who also made the vite build tool. As the name suggest, vitest's natural habitat is vite (which we are not currently using), but it runs without vite.
Like tsx it is a modern tool, built with support for typescript and ES modules builtin, and setup was a breeze.
Just like tsx, it has a builtin watch mode. This mode is even the default: you run your test, then you correct our code and the test automatically reruns.
We adapted the test scripts to our package.json:
"scripts": {
"start": "tsx watch server.ts",
"debug": "tsx watch --inspect-brk server.ts",
"build": "tsc",
"test": "vitest",
"coverage" : "vitest run --coverage "
}
Vitest's testing API is heavily inspired on, and compatible with Jest, easing migration. The only things we had to change, were some imports.
No comments:
Post a Comment