Skip to content

Commit 0de21c7

Browse files
committed
Add tests for assertValidFeatureReference
1 parent 27b90eb commit 0de21c7

File tree

6 files changed

+75
-22
lines changed

6 files changed

+75
-22
lines changed

assertions.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import assert from "node:assert/strict";
2+
import { assertValidFeatureReference } from "./assertions";
3+
4+
describe("assertValidReference()", function () {
5+
it("throws if target ID is a move", function () {
6+
assert.throws(() => {
7+
assertValidFeatureReference("a", "some-moving-feature", {
8+
"some-moving-feature": { kind: "moved" },
9+
});
10+
});
11+
});
12+
13+
it("throws if target ID is a split", function () {
14+
assert.throws(() => {
15+
assertValidFeatureReference("a", "some-split-feature", {
16+
"some-split-feature": { kind: "split" },
17+
});
18+
});
19+
});
20+
21+
it("throws if target ID is not defined", function () {
22+
assert.throws(() => {
23+
assertValidFeatureReference(
24+
"a",
25+
"this-is-a-completely-invalid-feature",
26+
{},
27+
);
28+
});
29+
});
30+
31+
it("does not throw if target ID is a feature", function () {
32+
assert.doesNotThrow(() => {
33+
assertValidFeatureReference("a", "dom", { dom: { kind: "feature" } });
34+
});
35+
});
36+
});

assertions.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { isOrdinaryFeatureData } from "./type-guards";
2+
import { WebFeaturesData } from "./types.quicktype";
3+
4+
/**
5+
* Assert that a reference from one feature to another is an ordinary feature
6+
* reference (i.e., it's defined and not some kind of redirect).
7+
*
8+
* @export
9+
* @param {string} sourceId The feature that is referencing another feature
10+
* @param {string} targetId The feature being referenced
11+
* @param {WebFeaturesData["features"]} features Feature data
12+
*/
13+
export function assertValidFeatureReference(
14+
sourceId: string,
15+
targetId: string,
16+
features: WebFeaturesData["features"],
17+
) {
18+
const target: unknown = features[targetId];
19+
if (target === undefined) {
20+
throw new Error(`${sourceId} references a non-existent feature`);
21+
}
22+
if (!isOrdinaryFeatureData(target)) {
23+
throw new Error(
24+
`${sourceId} references a redirect "${targetId} instead of an ordinary feature ID`,
25+
);
26+
}
27+
}
28+
29+
// TODO: assertValidSnapshotReference

index.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { GroupData, SnapshotData, WebFeaturesData } from './types';
99

1010
import { BASELINE_LOW_TO_HIGH_DURATION, coreBrowserSet, parseRangedDateString } from 'compute-baseline';
1111
import { Compat } from 'compute-baseline/browser-compat-data';
12+
import { assertValidFeatureReference } from './assertions';
1213
import { isMoved, isSplit } from './type-guards';
1314

1415
// The longest name allowed, to allow for compact display.
@@ -191,32 +192,20 @@ for (const [key, data] of yamlEntries('features')) {
191192
features[key] = data;
192193
}
193194

194-
// Assert that feature references are valid
195-
196-
function assertValidReference(sourceID: string, targetID: string): void {
197-
if (targetID in features) {
198-
if (isMoved(features[targetID]) || isSplit(features[targetID])) {
199-
throw new Error(`${sourceID} references a redirect "${targetID}" instead of an ordinary feature ID`);
200-
}
201-
return;
202-
}
203-
throw new Error(`${sourceID}'s reference to "${targetID}" is not a valid feature ID`);
204-
}
205-
206195
for (const [id, feature] of Object.entries(features)) {
207196
const { kind } = feature;
208197
switch (kind) {
209198
case "feature":
210199
for (const alternative of feature.discouraged?.alternatives ?? []) {
211-
assertValidReference(id, alternative);
200+
assertValidFeatureReference(id, alternative, features)
212201
}
213202
break;
214203
case "moved":
215-
assertValidReference(id, feature.redirect_target);
204+
assertValidFeatureReference(id, feature.redirect_target, features);
216205
break;
217206
case "split":
218207
for (const target of feature.redirect_targets) {
219-
assertValidReference(id, target);
208+
assertValidFeatureReference(id, target, features);
220209
}
221210
break;
222211
default:

package-lock.json

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

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
"test:coverage": "npm run --workspaces test:coverage",
3030
"test:dist": "tsx scripts/dist.ts --check",
3131
"test:format": "prettier --check .",
32+
"test:index": "mocha -r tsx './*.test.ts'",
3233
"test:lint": "npx eslint .",
3334
"test:schematypes": "tsx scripts/schema.ts",
3435
"test:specs": "tsx scripts/specs.ts",
3536
"test:types": "npm run --workspaces test:types && tsc",
36-
"test": "npm run test:caniuse -- --quiet && npm run test:schematypes && npm run test:specs && npm run test:types && npm run test:format && npm run test:dist && npm run test --workspaces && npm run test:lint",
37+
"test": "npm run test:caniuse -- --quiet && npm run test:schematypes && npm run test:specs && npm run test:types && npm run test:format && npm run test:index && npm run test:dist && npm test --workspaces && npm run test:lint",
3738
"update-drafts": "tsx scripts/update-drafts.ts",
3839
"remove-tagged-compat-features": "tsx scripts/remove-tagged-compat-features.ts && npm run format"
3940
},
@@ -53,6 +54,7 @@
5354
"fast-json-stable-stringify": "^2.1.0",
5455
"fdir": "^6.5.0",
5556
"hast-util-to-string": "^3.0.1",
57+
"mocha": "^11.7.2",
5658
"prettier": "^3.6.2",
5759
"quicktype": "^23.2.6",
5860
"rehype-parse": "^9.0.1",

packages/compute-baseline/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828
"devDependencies": {
2929
"@types/chai": "^5.2.2",
3030
"@types/chai-jest-snapshot": "^1.3.8",
31-
"@types/mocha": "^10.0.10",
3231
"c8": "^10.1.3",
3332
"chai": "^6.0.1",
34-
"chai-jest-snapshot": "^2.0.0",
35-
"mocha": "^11.7.2"
33+
"chai-jest-snapshot": "^2.0.0"
3634
},
3735
"peerDependencies": {
3836
"@mdn/browser-compat-data": "^7.0.0"

0 commit comments

Comments
 (0)