web: Make tests in core use typescript

This commit is contained in:
Nathan Adams 2020-11-13 00:47:40 +01:00 committed by Mike Welsh
parent 5ff7168fe9
commit 7701ef9c26
5 changed files with 56 additions and 41 deletions

View File

@ -7,13 +7,18 @@
"scripts": { "scripts": {
"build": "webpack", "build": "webpack",
"prepare": "webpack", "prepare": "webpack",
"test": "mocha" "test": "mocha -r esm -r ts-node/register test/**.ts"
}, },
"devDependencies": { "devDependencies": {
"@types/chai": "^4.2.14",
"@types/mocha": "^8.0.4",
"@typescript-eslint/eslint-plugin": "^4.7.0", "@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0", "@typescript-eslint/parser": "^4.7.0",
"chai": "^4.2.0",
"eslint": "^7.13.0", "eslint": "^7.13.0",
"esm": "^3.2.25",
"mocha": "^8.0.1", "mocha": "^8.0.1",
"ts-node": "^9.0.0",
"typedoc": "^0.19.2", "typedoc": "^0.19.2",
"typescript": "^4.0.5", "typescript": "^4.0.5",
"webpack": "^5.1.3", "webpack": "^5.1.3",

View File

@ -9,7 +9,7 @@ interface Requirement {
* Represents a set of version requirements. * Represents a set of version requirements.
*/ */
export class VersionRange { export class VersionRange {
private readonly requirements: Requirement[][]; readonly requirements: Requirement[][];
constructor(requirements: Requirement[][]) { constructor(requirements: Requirement[][]) {
this.requirements = requirements; this.requirements = requirements;

View File

@ -1,5 +1,13 @@
{ {
"env": { "env": {
"mocha": true "mocha": true
} },
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
]
} }

View File

@ -1,13 +1,13 @@
const { assert } = require("chai"); import { assert } from "chai";
const { VersionRange } = require("../src/version-range"); import { VersionRange } from "../src/version-range";
const { Version } = require("../src/version"); import { Version } from "../src/version";
describe("VersionRange", function () { describe("VersionRange", function () {
describe("#from_requirement_string()", function () { describe("#from_requirement_string()", function () {
it("should accept a specific version without an equals sign", function () { it("should accept a specific version without an equals sign", function () {
const range = VersionRange.from_requirement_string("1.2.3"); const range = VersionRange.from_requirement_string("1.2.3");
assert.deepEqual(range.requirements, [ assert.deepEqual(range.requirements, [
[["", Version.from_semver("1.2.3")]], [{ comparator: "", version: Version.from_semver("1.2.3") }],
]); ]);
}); });
@ -16,15 +16,15 @@ describe("VersionRange", function () {
"1.2.3 || 1.2.4" "1.2.3 || 1.2.4"
); );
assert.deepEqual(range.requirements, [ assert.deepEqual(range.requirements, [
[["", Version.from_semver("1.2.3")]], [{ comparator: "", version: Version.from_semver("1.2.3") }],
[["", Version.from_semver("1.2.4")]], [{ comparator: "", version: Version.from_semver("1.2.4") }],
]); ]);
}); });
it("should accept a specific version with an equals sign", function () { it("should accept a specific version with an equals sign", function () {
const range = VersionRange.from_requirement_string("=1.2.3"); const range = VersionRange.from_requirement_string("=1.2.3");
assert.deepEqual(range.requirements, [ assert.deepEqual(range.requirements, [
[["=", Version.from_semver("1.2.3")]], [{ comparator: "=", version: Version.from_semver("1.2.3") }],
]); ]);
}); });
@ -33,8 +33,8 @@ describe("VersionRange", function () {
"=1.2.3 || =1.2.4" "=1.2.3 || =1.2.4"
); );
assert.deepEqual(range.requirements, [ assert.deepEqual(range.requirements, [
[["=", Version.from_semver("1.2.3")]], [{ comparator: "=", version: Version.from_semver("1.2.3") }],
[["=", Version.from_semver("1.2.4")]], [{ comparator: "=", version: Version.from_semver("1.2.4") }],
]); ]);
}); });
@ -42,8 +42,8 @@ describe("VersionRange", function () {
const range = VersionRange.from_requirement_string(">1.2.3 <1.2.5"); const range = VersionRange.from_requirement_string(">1.2.3 <1.2.5");
assert.deepEqual(range.requirements, [ assert.deepEqual(range.requirements, [
[ [
[">", Version.from_semver("1.2.3")], { comparator: ">", version: Version.from_semver("1.2.3") },
["<", Version.from_semver("1.2.5")], { comparator: "<", version: Version.from_semver("1.2.5") },
], ],
]); ]);
}); });
@ -54,8 +54,14 @@ describe("VersionRange", function () {
); );
assert.deepEqual(range.requirements, [ assert.deepEqual(range.requirements, [
[ [
[">=", Version.from_semver("1-test")], {
["<=", Version.from_semver("2-test")], comparator: ">=",
version: Version.from_semver("1-test"),
},
{
comparator: "<=",
version: Version.from_semver("2-test"),
},
], ],
]); ]);
}); });
@ -64,8 +70,8 @@ describe("VersionRange", function () {
const range = VersionRange.from_requirement_string("^1.2 <1.3"); const range = VersionRange.from_requirement_string("^1.2 <1.3");
assert.deepEqual(range.requirements, [ assert.deepEqual(range.requirements, [
[ [
["^", Version.from_semver("1.2")], { comparator: "^", version: Version.from_semver("1.2") },
["<", Version.from_semver("1.3")], { comparator: "<", version: Version.from_semver("1.3") },
], ],
]); ]);
}); });
@ -75,8 +81,8 @@ describe("VersionRange", function () {
"|| || 1.2.4 || || 1.2.5 ||" "|| || 1.2.4 || || 1.2.5 ||"
); );
assert.deepEqual(range.requirements, [ assert.deepEqual(range.requirements, [
[["", Version.from_semver("1.2.4")]], [{ comparator: "", version: Version.from_semver("1.2.4") }],
[["", Version.from_semver("1.2.5")]], [{ comparator: "", version: Version.from_semver("1.2.5") }],
]); ]);
}); });
}); });

View File

@ -1,5 +1,5 @@
const { assert } = require("chai"); import { assert } from "chai";
const { Version } = require("../src/version"); import { Version } from "../src/version";
// Each row should be a list of compatible versions. // Each row should be a list of compatible versions.
// Earlier entries in a row should be "greater than" later entries in the same row. // Earlier entries in a row should be "greater than" later entries in the same row.
@ -24,12 +24,8 @@ const testMatrix = [
["0.0.1", "0.0.1-dev", "0.0.1-5", "0.0.1-2"], ["0.0.1", "0.0.1-dev", "0.0.1-5", "0.0.1-2"],
]; ];
function flatten(arr) { function flatten<T>(arr: T[][]): T[] {
return arr.reduce(function (flat, toFlatten) { return arr.reduce((accumulator, value) => accumulator.concat(value), []);
return flat.concat(
Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten
);
}, []);
} }
describe("Version", function () { describe("Version", function () {
@ -72,9 +68,9 @@ describe("Version", function () {
describe("#is_compatible_with()", function () { describe("#is_compatible_with()", function () {
it("is compatible with similar versions", function () { it("is compatible with similar versions", function () {
for (let test of testMatrix) { for (const test of testMatrix) {
for (let a of test) { for (const a of test) {
for (let b of test) { for (const b of test) {
assert.isOk( assert.isOk(
Version.from_semver(a).is_compatible_with( Version.from_semver(a).is_compatible_with(
Version.from_semver(b) Version.from_semver(b)
@ -86,11 +82,11 @@ describe("Version", function () {
} }
}); });
it("is not compatible with other versions", function () { it("is not compatible with other versions", function () {
for (let test of testMatrix) { for (const test of testMatrix) {
for (let a of test) { for (const a of test) {
for (let otherTest of testMatrix) { for (const otherTest of testMatrix) {
if (test === otherTest) continue; if (test === otherTest) continue;
for (let b of otherTest) { for (const b of otherTest) {
assert.isNotOk( assert.isNotOk(
Version.from_semver(a).is_compatible_with( Version.from_semver(a).is_compatible_with(
Version.from_semver(b) Version.from_semver(b)
@ -150,7 +146,7 @@ describe("Version", function () {
describe("#is_equal()", function () { describe("#is_equal()", function () {
it("returns true when it should", function () { it("returns true when it should", function () {
const tests = flatten(testMatrix); const tests = flatten(testMatrix);
for (let version of tests) { for (const version of tests) {
assert.isOk( assert.isOk(
Version.from_semver(version).is_equal( Version.from_semver(version).is_equal(
Version.from_semver(version) Version.from_semver(version)
@ -186,7 +182,7 @@ describe("Version", function () {
describe("#is_stable_or_compatible_prerelease()", function () { describe("#is_stable_or_compatible_prerelease()", function () {
it("returns true for own versions", function () { it("returns true for own versions", function () {
const tests = flatten(testMatrix); const tests = flatten(testMatrix);
for (let version of tests) { for (const version of tests) {
assert.isOk( assert.isOk(
Version.from_semver( Version.from_semver(
version version
@ -199,8 +195,8 @@ describe("Version", function () {
}); });
it("returns true for compatible pre-releases", function () { it("returns true for compatible pre-releases", function () {
const tests = ["1.2.3", "1.2.3-alpha", "1.2.3-beta1.build2"]; const tests = ["1.2.3", "1.2.3-alpha", "1.2.3-beta1.build2"];
for (let a of tests) { for (const a of tests) {
for (let b of tests) { for (const b of tests) {
assert.isOk( assert.isOk(
Version.from_semver( Version.from_semver(
a a
@ -214,8 +210,8 @@ describe("Version", function () {
}); });
it("returns false for incompatible pre-releases", function () { it("returns false for incompatible pre-releases", function () {
const tests = ["1-dev", "1.2-alpha", "1.2.3-beta1.build2"]; const tests = ["1-dev", "1.2-alpha", "1.2.3-beta1.build2"];
for (let a of tests) { for (const a of tests) {
for (let b of tests) { for (const b of tests) {
if (a === b) continue; if (a === b) continue;
assert.isNotOk( assert.isNotOk(
Version.from_semver( Version.from_semver(