Skip to content

Commit e01ba10

Browse files
authored
use jest for testing (#108)
use jest instead of nodejs test runner since jest just has way better coverage reporting and is the common tool to test nodejs apps. Perhaps nodejs test runner will be added back again when things change and it becomes majorly adopted.
1 parent 05d6adb commit e01ba10

File tree

10 files changed

+3590
-313
lines changed

10 files changed

+3590
-313
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jobs:
2020
- name: Install Dependencies
2121
run: yarn --immutable
2222
- name: Test
23-
run: npm test
23+
run: yarn test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,4 @@ bundle
137137
dist
138138
node_modules
139139
TODO
140+
.envrc

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ running with a new project in no time. It provides:
2020
- compile the codebase from ts to js
2121
- check for formatting issues
2222
- check for linting issues
23-
- Testing via the new Node.js [test
24-
runner](https://nodejs.org/api/test.html#test-runner) instead of something
25-
like mocha or jest.
23+
- Testing via [jest](https://www.npmjs.com/package/jest)
2624
- Formatting via [prettier](https://prettier.io/).
2725
- Linting via [eslint](https://eslint.org/) and
2826
[typescript-eslint](https://typescript-eslint.io/)
2927
- Bundling via [esbuild](https://esbuild.github.io/), a fast bundler that "just
30-
works" and is nowadays even used in the typescript codebase.
28+
works".
3129
- Debugging set up with examples for vscode and vim.
3230
- Automated dependency updates via
3331
[renovate](https://github.com/renovatebot/renovate).
@@ -105,8 +103,10 @@ attribute.
105103
only invoked by the CI/CD pipeline.
106104
- `yarn start` -> Runs the code. This only works if the code was bundled before ;).
107105
- `yarn test` -> Tests your codebase. Basic tests are created for both major
108-
approaches of putting tests beside the source code as well as putting tests in
109-
a seperate folder.
106+
common approaches of putting tests beside the source code as well as putting
107+
tests in a separate folder.
108+
- You can inspect the code coverage in depth by running `npx http-server
109+
./coverage/lcov-report` and then browsing http://localhost:8080.
110110

111111
## Debugging
112112

babel.config.cjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-typescript",
5+
],
6+
// required for easily mocking module exports.
7+
// see: https://stackoverflow.com/questions/67872622/jest-spyon-not-working-on-index-file-cannot-redefine-property
8+
assumptions: {
9+
constantReexports: true,
10+
},
11+
plugins: [],
12+
};

jest.config.cjs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//@ts-check
2+
/**
3+
* For a detailed explanation regarding each configuration property, visit:
4+
* https://jestjs.io/docs/configuration
5+
*/
6+
7+
/** @type {import('jest').Config} */
8+
const config = {
9+
// Automatically clear mock calls, instances, contexts and results before every test
10+
clearMocks: true,
11+
// Indicates whether the coverage information should be collected while executing the test
12+
collectCoverage: true,
13+
coverageReporters: ["lcov", "text"],
14+
collectCoverageFrom: [
15+
"**/*.ts",
16+
"!**/node_modules/**",
17+
"!**/dist/**",
18+
"!**/*.d.ts",
19+
],
20+
// The directory where Jest should output its coverage files
21+
coverageDirectory: "coverage",
22+
// Indicates which provider should be used to instrument code for coverage
23+
coverageProvider: "v8",
24+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
25+
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
26+
// Indicates whether each individual test should be reported during the run
27+
verbose: true,
28+
testMatch: [
29+
"**/test/**/*.[jt]s?(x)",
30+
"!**/test/util/**/*.[jt]s?(x)",
31+
"!**/test/fixture/**/*.[jt]s?(x)",
32+
"**/?(*.)+(spec|test).[tj]s?(x)",
33+
],
34+
};
35+
36+
module.exports = config;

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,26 @@
3838
"lint": "eslint . --fix",
3939
"lint-check": "eslint .",
4040
"start": "node --enable-source-maps ./bundle/index.js",
41-
"test": "glob \"src/**/*.test.ts\" \"test/**\" -c \"tsx --test\""
41+
"test": "jest --runInBand ."
4242
},
4343
"devDependencies": {
44+
"@babel/core": "7.24.5",
45+
"@babel/preset-env": "7.24.5",
46+
"@babel/preset-typescript": "7.24.1",
47+
"@jest/globals": "29.7.0",
4448
"@tsconfig/node20": "20.1.4",
49+
"@types/jest": "29.5.12",
4550
"@types/node": "20.12.11",
46-
"@types/nodemon": "1.19.6",
4751
"@typescript-eslint/eslint-plugin": "6.21.0",
4852
"@typescript-eslint/parser": "6.21.0",
4953
"esbuild": "0.19.12",
5054
"eslint": "8.57.0",
5155
"eslint-config-prettier": "9.1.0",
5256
"glob": "10.3.15",
57+
"jest": "29.7.0",
5358
"nodemon": "3.1.0",
5459
"prettier": "3.2.5",
5560
"rimraf": "5.0.7",
56-
"tsx": "4.10.2",
5761
"typescript": "5.4.5"
5862
},
5963
"packageManager": "yarn@4.0.2"

src/hello.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { describe, test } from "node:test";
2-
import assert from "node:assert/strict";
1+
import { describe, expect, it } from "@jest/globals";
32
import { returnHelloWorld } from "./hello";
43

54
describe("unit tests and code side by side", () => {
6-
test("returnHelloWorld() - returns 'hello world'", () => {
7-
console.log("testing returnHelloWorld()");
8-
assert.equal(returnHelloWorld(), "hello world");
9-
console.log("Done");
5+
describe("when using the returnHelloWorld function", () => {
6+
it("returns 'hello world'", () => {
7+
console.log("testing returnHelloWorld()");
8+
expect(returnHelloWorld()).toBe("hello world");
9+
console.log("Done");
10+
});
1011
});
1112
});

test/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { describe, it } from "node:test";
2-
import assert from "node:assert/strict";
1+
import { describe, it } from "@jest/globals";
32

4-
describe("tests in seperate folder", () => {
3+
describe("when having tests in seperate folder", () => {
54
it("works", () => {
6-
assert.equal(true, true);
5+
expect(true).toBe(true);
76
});
87
});

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"forceConsistentCasingInFileNames": true,
1313
"declaration": true,
1414
"sourceMap": true,
15-
"resolveJsonModule": true,
15+
"resolveJsonModule": true
1616
},
1717
"include": ["src", "test"],
18-
"exclude": ["dist", "bundle", "node_modules"],
18+
"exclude": ["dist", "bundle", "node_modules"]
1919
}

0 commit comments

Comments
 (0)