$ npm init -y
# I'm only concerned with typescript and jest. The others are from jest's website that are needed
$ npm install --save-dev typescript jest babel-jest @babel/core @babel/preset-env @babel/preset-typescript ts-jest @jest/globals @types/jest
$ npx tsc --init
$ npm init jest@latest # yes yes node no babel no
On my Mac, I don't need to change the type from commonjs to module. On my Windows machine I did. I need to do more digging to see if this is a difference in the nodejs version, or something else.
Change the type to module in package.json We need to do this or the jest.config.js fails to load. Maybe there is an option to specify this when doing the npm init jest
The jest docs show a babel.config.js, but the type is commonjs and not module. So you have to rename to babel.config.cjs to get it to work I found using json works better for my simple use cases (for now)
$ vim babel.config.json
{
"presets": [
["@babel/preset-env", { "targets": {"node": "current"} }],
"@babel/preset-typescript"
]
}
$ mkdir src && mkdir test
$ vim test/sum.test.ts
import {describe, expect, test} from '@jest/globals';
import {sum} from '../src/sum';
describe('sum module', () => {
test('adds 1 + 2 to equal 3', () => {
let result: number = sum(1, 2);
expect(result).toBe(3);
});
});
$ vim src/sum.ts
export function sum(a: number, b: number): number {
return a + b;
}