It takes only five easy steps to add unit tests to a Turborepo monorepo. No external testing frameworks, such as Jest or Mocha, are needed.
1. Add tsx
at the monorepo’s root level to run TypeScript code without issues related to different module systems:
npm add --dev tsx
2. Add a test
task to turbo.json
:
{
...
"tasks": {
"test": {
"outputs": []
},
...
},
...
}
3. Add a test
script in every package.json
where necessary:
"scripts": {
...
"test": "tsx --test"
},
4. Add a test
script into the monorepo's root package.json
:
"scripts": {
...
"test": "turbo run build && turbo run test"
}
The build step is optional, but it ensures the project compiles successfully before running tests. Alternatively, add a dependency on the build
step in turbo.json
.
5. That is it. Now add your first test using the built-in Node.js test runner functionality:
add.ts
export function add(a: number, b: number) {
return a + b;
};
add.test.ts
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { add } from './add.ts';
describe('Test addition', () => {
it('should add two positive numbers', () => {
assert.equal(add(2, 2), 4);
});
it('should add two negative numbers', () => {
assert.equal(add(-2, -2), -4);
});
});
Finally, give it a try by running:
npm run test
Top comments (0)