Skip to content

Commit

Permalink
Re-write tests and drop dependency on Chai (#1505)
Browse files Browse the repository at this point in the history
The Chai testing library is now ECMAScript module only. This could have
provided a good occasion to convert tests from CommonJS modules to ECMAScript
modules, but then, looking into actual tests, many of them used Chai where they
could just have used the basic Node.js `assert` module, and the few tests that
used `expect` did not look more readable thanks to that syntax compared to a
more basic `assert` approach.

Instead of converting test code to ECMAScript modules, this update rather drops
the dependency on Chai, rewriting tests where needed to rather call regular
`assert` functions.
  • Loading branch information
tidoust authored Mar 4, 2024
1 parent 807626c commit f2f6d80
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 253 deletions.
154 changes: 0 additions & 154 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"webidl2": "24.4.1"
},
"devDependencies": {
"chai": "4.3.10",
"mocha": "10.3.0",
"respec": "34.4.0",
"respec-hljs": "2.1.1",
Expand Down
12 changes: 6 additions & 6 deletions tests/crawl.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function runWithAnnotatedCrawlData(path, fn) {
}

if (global.describe && describe instanceof Function) {
const { assert } = require('chai');
const assert = require('assert');

describe("The crawler", function () {
this.slow(20000);
Expand Down Expand Up @@ -109,7 +109,7 @@ if (global.describe && describe instanceof Function) {
fallback
}));
assert.equal(results[0].title, "Change is the only constant");
assert.isUndefined(results[0].error);
assert.ifError(results[0].error);
assert.equal(results[0].refs, "A useful list of refs");
})

Expand All @@ -119,7 +119,7 @@ if (global.describe && describe instanceof Function) {
[{ url, nightly: { url } }],
{ forceLocalFetch: true });
assert.equal(results[0].title, "[Could not be determined, see error]");
assert.include(results[0].error, "Loading https://www.w3.org/TR/idontexist/ triggered HTTP status 404");
assert(results[0].error.includes("Loading https://www.w3.org/TR/idontexist/ triggered HTTP status 404"));
});

it("reports errors and returns fallback data when possible", async () => {
Expand All @@ -132,7 +132,7 @@ if (global.describe && describe instanceof Function) {
fallback
});
assert.equal(results[0].title, "On the Internet, nobody knows you don't exist");
assert.include(results[0].error, "Loading https://www.w3.org/TR/idontexist/ triggered HTTP status 404");
assert(results[0].error.includes("Loading https://www.w3.org/TR/idontexist/ triggered HTTP status 404"));
assert.equal(results[0].refs, "A useful list of refs");
});

Expand All @@ -147,7 +147,7 @@ if (global.describe && describe instanceof Function) {
});
const results = require(path.resolve(output, "index.json"));
assert.equal(results.results[0].url, "https://www.w3.org/TR/idontexist/");
assert.include(results.results[0].error, "Loading https://www.w3.org/TR/idontexist/ triggered HTTP status 404");
assert(results.results[0].error.includes("Loading https://www.w3.org/TR/idontexist/ triggered HTTP status 404"));
assert.equal(results.results[0].refs, "refs/idontexist.json");
const refs = require(path.resolve(output, "refs", "idontexist.json"));
assert.equal(refs.refs, "A useful list of refs");
Expand All @@ -159,7 +159,7 @@ if (global.describe && describe instanceof Function) {
[{ url, nightly: { url } }],
{ forceLocalFetch: true });
assert.equal(results[0].title, "[Could not be determined, see error]");
assert.include(results[0].error, "CSS server issue detected");
assert(results[0].error.includes("CSS server issue detected"));
});

it("crawls the published spec when `--release` is set", async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/create-outline.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { assert } = require('chai');
const assert = require('assert');
const puppeteer = require('puppeteer');
const path = require('path');
const rollup = require('rollup');
Expand Down
4 changes: 2 additions & 2 deletions tests/css-grammar-parser/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const css = require("../../src/lib/css-grammar-parser");
const fs = require("fs");
const expect = require('chai').expect;
const assert = require("assert");

const propDefs = fs.readFileSync("tests/css-grammar-parser/in", "utf-8").split("\n").map(def => def.trim());
const propDefsOut = JSON.parse(fs.readFileSync("tests/css-grammar-parser/out.json", "utf-8"));
Expand All @@ -9,7 +9,7 @@ const results = propDefs.map(css.parsePropDefValue);
describe('Parser correctly parses grammar instances', () => {
for(let i in results) {
it(`parses property definition ${propDefs[i]} as expected`, () => {
expect(results[i]).to.deep.equal(propDefsOut[i], `Parsing ${propDefs[i]} got ${JSON.stringify(results[i], null, 2)} instead of ${JSON.stringify(propDefsOut[i], null, 2)}`);
assert.deepStrictEqual(results[i], propDefsOut[i], `Parsing ${propDefs[i]} got ${JSON.stringify(results[i], null, 2)} instead of ${JSON.stringify(propDefsOut[i], null, 2)}`);
});
}
});
2 changes: 1 addition & 1 deletion tests/extract-dfns.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { assert } = require('chai');
const assert = require('assert');
const puppeteer = require('puppeteer');
const path = require('path');
const rollup = require('rollup');
Expand Down
2 changes: 1 addition & 1 deletion tests/extract-elements.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/extract-events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { assert } = require('chai');
const assert = require('assert');
const puppeteer = require('puppeteer');
const path = require('path');
const rollup = require('rollup');
Expand Down
2 changes: 1 addition & 1 deletion tests/extract-headings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { assert } = require('chai');
const assert = require('assert');
const puppeteer = require('puppeteer');
const path = require('path');
const rollup = require('rollup');
Expand Down
2 changes: 1 addition & 1 deletion tests/extract-ids.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { assert } = require('chai');
const assert = require('assert');
const puppeteer = require('puppeteer');
const path = require('path');
const rollup = require('rollup');
Expand Down
2 changes: 1 addition & 1 deletion tests/extract-references.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { assert } = require('chai');
const assert = require('assert');
const puppeteer = require('puppeteer');
const path = require('path');
const rollup = require('rollup');
Expand Down
2 changes: 1 addition & 1 deletion tests/extract-webidl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { assert } = require('chai');
const assert = require('assert');
const puppeteer = require('puppeteer');
const path = require('path');
const rollup = require('rollup');
Expand Down
Loading

0 comments on commit f2f6d80

Please sign in to comment.