Skip to content

Commit d632ce2

Browse files
committed
Add unit tests for "staying on the same page when switching versions" functionality
Following https://www.innoq.com/en/blog/2020/04/ts-jasmine-karma/#executingtestsonnode.js This is meant to simplify checking correctness of code like squidfunk#7227 and fixing bugs like squidfunk#7226. In fact, I'm hoping to eventually make this code general enough that it runs locally with `mike serve`. I picked `mocha` as the testing library because its Typescript support relies on ts-node, which this project already relies on. I have little experience with `mocha` vs `jest` vs something else beyond this commit. See also https://github.com/mochajs/mocha-examples/tree/main/packages/typescript Instead of using `chai`, I'm using Node's assert package. Its TS types are in `@types/node`. It should be trivial, and might be good, to switch to `chai` or something else. This setup is not perfect, in particular I can't get tests to import the whole `index.ts` file. This is why the function being tested gets its own file. Importing `index.ts` would seem to require writing some DOM shims and be a general headache. For the record, using `tsx` and `jsdom-global` instead of `ts-node` to run `mocha` seemed like the most promising approach, but it still failed since some files imported from this `index.ts` rely on the DOM having some particular structure in their top-level definitions. Here's how to set up `tsx` and `jsdom-global`, for reference: ```diff diff --git a/.mocharc.json b/.mocharc.json index e713305556...33c9adc84d 100644 --- a/.mocharc.json +++ b/.mocharc.json @@ -2,5 +2,5 @@ "$schema": "https://json.schemastore.org/mocharc.json", "extension": ["ts"], "spec": "src/**/**.test.ts", - "require": "ts-node/register" + "require": ["tsx", "jsdom-global/register"] } diff --git a/package.json b/package.json index ecc9f98cf9...10242ca725 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "gitlab": "^14.2.2", "google-fonts-complete": "jonathantneal/google-fonts-complete", "html-minifier": "^4.0.0", + "jsdom-global": "^3.0.2", "material-design-color": "^2.3.2", "material-shadows": "^3.0.1", "mocha": "^10.6.0", @@ -99,6 +100,7 @@ "svgo": "3.0.0", "tiny-glob": "^0.2.9", "ts-node": "^10.9.2", + "tsx": "^4.16.2", "typescript": "^5.5.2" }, "engines": { ``` I can also incorporate the diff into this commit.
1 parent 81093a0 commit d632ce2

File tree

7 files changed

+308
-20
lines changed

7 files changed

+308
-20
lines changed

.mocharc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://json.schemastore.org/mocharc.json",
3+
"extension": ["ts"],
4+
"spec": "src/**/**.test.ts",
5+
"require": "ts-node/register"
6+
}

package-lock.json

Lines changed: 229 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
"check:style": "run-p check:style:*",
3434
"check:style:scss": "stylelint \"src/**/*.scss\"",
3535
"check:style:ts": "eslint --cache \"src/**/*.ts\"",
36+
"check:test": "ts-node ./node_modules/jasmine/bin/jasmine --config=tests/jasmine-config/jasmine.json",
3637
"fix": "run-p fix:*",
3738
"fix:style": "run-p fix:style:*",
3839
"fix:style:ts": "eslint --cache \"src/**/*.ts\" --fix",
3940
"start": "ts-node -T tools/build --verbose --all --dirty --watch",
41+
"test": "run-p check:test",
4042
"upgrade": "run-s upgrade:*",
4143
"upgrade:bump": "ncu --upgrade --filterVersion \"/^\\^/\"",
4244
"upgrade:install": "npm install"
@@ -58,6 +60,7 @@
5860
"@types/escape-html": "^1.0.4",
5961
"@types/fuzzaldrin-plus": "^0.6.5",
6062
"@types/html-minifier": "^4.0.5",
63+
"@types/jasmine": "^5.1.4",
6164
"@types/lunr": "^2.3.7",
6265
"@types/node": "^20.14.10",
6366
"@types/resize-observer-browser": "^0.1.11",
@@ -77,6 +80,8 @@
7780
"gitlab": "^14.2.2",
7881
"google-fonts-complete": "jonathantneal/google-fonts-complete",
7982
"html-minifier": "^4.0.0",
83+
"jasmine": "^5.1.0",
84+
"jasmine-spec-reporter": "^7.0.0",
8085
"material-design-color": "^2.3.2",
8186
"material-shadows": "^3.0.1",
8287
"npm-check-updates": "^16.14.20",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {SpecReporter} from "jasmine-spec-reporter";
2+
3+
jasmine.getEnv().clearReporters();
4+
5+
jasmine.getEnv().addReporter(new SpecReporter({
6+
spec: {
7+
displayPending: true
8+
}
9+
}));

0 commit comments

Comments
 (0)