Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

test: migrate tests to node test_runner #183

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,350 changes: 292 additions & 3,058 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"scripts": {
"build": "tsc",
"prepublishOnly": "npm run build",
"test": "mocha --parallel && npm run test:tsd",
"test-only": "glob -c \"node --loader ts-node/esm --no-warnings --test\" \"test/**/*.spec.ts\"",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other project we use tsx (I guess we can replace ts-node)

glob -c \"tsx --test\" \"./test/**/*.spec.ts\"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with using tsx to run tests, is that it make them fail because it fail to do some imports. The problem is due to the fact that we use "type": "module" inside our package.json, removing this fix the tests. The tsx project is aware of the problem, an issue is open here.

Copy link
Member

@PierreDemailly PierreDemailly Mar 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It (can) works with tsx.

npm i -D tsx

package.json:
"test-only": "glob -c \"tsx --test\" \"test/**/*.spec.ts\"",

src/index.ts

export { type RC, type Author, JSONSchema, homedir } from "./rc.js";

instead of

export { RC, Author, JSONSchema, homedir } from "./rc.js";

src/rc.ts

import { generateCIConfiguration, type CiConfiguration, type CiWarnings } from "./projects/ci.js";
import { generateReportConfiguration, type ReportConfiguration, type ReportChart } from "./projects/report.js";
import { generateScannerConfiguration, type ScannerConfiguration, type Author } from "./projects/scanner.js";

instead of

import { generateCIConfiguration, CiConfiguration, CiWarnings } from "./projects/ci.js";
import { generateReportConfiguration, ReportConfiguration, ReportChart } from "./projects/report.js";
import { generateScannerConfiguration, ScannerConfiguration, Author } from "./projects/scanner.js";

Tested with Node v18, v20, v21

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please migrate, we use tsx in all our projects

"test": "npm run test-only && npm run test:tsd",
"test:tsd": "npm run build && tsd",
"coverage": "c8 -r html npm test",
"lint": "cross-env eslint src/*.ts"
Expand All @@ -36,17 +37,13 @@
"homepage": "https://github.com/NodeSecure/rc#readme",
"devDependencies": {
"@nodesecure/eslint-config": "^1.8.0",
"@types/chai": "^4.3.5",
"@types/lodash.merge": "^4.6.7",
"@types/mocha": "^10.0.1",
"@types/node": "^20.5.8",
"@types/zen-observable": "^0.8.4",
"ajv": "^8.12.0",
"c8": "^9.1.0",
"chai": "^5.1.0",
"eslint": "^8.48.0",
"mocha": "^10.2.0",
"tape": "^5.6.6",
"glob": "^10.3.10",
"ts-node": "^10.9.1",
"tsd": "^0.30.7",
"typescript": "^5.2.2"
Expand Down
17 changes: 9 additions & 8 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import path from "node:path";
import { readdirSync } from "node:fs";
import { fileURLToPath } from "node:url";
import assert from "node:assert";
import { describe, it } from "node:test";

// Import Third-party Dependencies
import { expect } from "chai";
import Ajv from "ajv";
import merge from "lodash.merge";

Expand All @@ -18,8 +19,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));

describe("CONSTANTS", () => {
it("should export a CONSTANTS variable", () => {
expect("CONSTANTS" in RC).equal(true);
expect(RC.CONSTANTS.CONFIGURATION_NAME).equal(".nodesecurerc");
assert("CONSTANTS" in RC);
assert.equal(RC.CONSTANTS.CONFIGURATION_NAME, ".nodesecurerc");
});
});

Expand All @@ -35,15 +36,15 @@ describe("JSON Schema", () => {
const ajv = new Ajv();
const validate = ajv.compile(RC.JSONSchema);

expect(validate(generateDefaultRC())).equal(true);
assert(validate(generateDefaultRC()));

const completeRC = merge(
generateDefaultRC("complete"),
kDummyPartialMandatoryRC
);
expect(validate(completeRC)).equal(true);
assert(validate(completeRC));

expect(validate({ foo: "bar" })).equal(false);
assert(!validate({ foo: "bar" }));
});

it("should validate all fixtures configuration", async() => {
Expand All @@ -57,10 +58,10 @@ describe("JSON Schema", () => {

for (const fileLocation of configurationFiles) {
const json = readJSONSync(fileLocation);
expect(
assert(
validate(json),
`Should be able to validate RC '${fileLocation}'`
).equal(true);
);
}
});
});
27 changes: 14 additions & 13 deletions test/memoize.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Import Third-party Dependencies
import { expect } from "chai";
// Import Node.js Dependencies
import assert from "node:assert";
import { describe, beforeEach, it } from "node:test";

// Import Internal Dependencies
import { generateDefaultRC, RC } from "../src/rc.js";
Expand All @@ -14,7 +15,7 @@ describe("memoize", () => {
const payload = generateDefaultRC();
memoize(payload);

expect(memoized()).to.deep.equal(payload);
assert.deepEqual(memoized(), payload);
});

it("should overwrite the previous payload if the overwrite option is true", () => {
Expand All @@ -26,7 +27,7 @@ describe("memoize", () => {
};
memoize(payload, { overwrite: true });

expect(memoized()).to.deep.equal(payload);
assert.deepEqual(memoized(), payload);
});

it("should merge with the previous memoized payload if overwrite option is set to false", () => {
Expand All @@ -40,7 +41,7 @@ describe("memoize", () => {
};
memoize(payload, { overwrite: false });

expect(memoized()).to.deep.equal({ ...rc, ...payload });
assert.deepEqual(memoized(), { ...rc, ...payload });
});
});

Expand All @@ -50,14 +51,14 @@ describe("memoized", () => {
});

it("should return null when there is no memoized value", () => {
expect(memoized()).to.eq(null);
assert.equal(memoized(), null);
});

it("should return previously memoized RC", () => {
const rc = generateDefaultRC();
memoize(rc);

expect(memoized()).to.deep.equal(rc);
assert.deepEqual(memoized(), rc);
});
});

Expand All @@ -68,17 +69,17 @@ describe("maybeMemoized", () => {

it("should return None when there is no memoized value", () => {
const option = maybeMemoized();
expect(option.none).to.eq(true);
expect(option.unwrapOr(null)).to.eq(null);
assert(option.none);
assert.equal(option.unwrapOr(null), null);
});

it("should unwrap previously memoized RC", () => {
const rc = generateDefaultRC();
memoize(rc);

const option = maybeMemoized();
expect(option.some).to.eq(true);
expect(option.unwrap()).to.deep.equal(rc);
assert(option.some);
assert.deepEqual(option.unwrap(), rc);
});
});

Expand All @@ -87,8 +88,8 @@ describe("clearMemoized", () => {
const rc = generateDefaultRC();
memoize(rc);

expect(memoized()).to.not.equal(null);
assert.notEqual(memoized(), null);
clearMemoized();
expect(memoized()).to.equal(null);
assert.equal(memoized(), null);
});
});
13 changes: 7 additions & 6 deletions test/rc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Import Third-party Dependencies
import { expect } from "chai";
// Import Node.js Dependencies
import { describe, it } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import {
Expand All @@ -18,7 +19,7 @@ describe("generateDefaultRC (internals)", () => {
generateCIConfiguration()
);

expect(rc).deep.equal(expectedResult);
assert.deepEqual(rc, expectedResult);
});

it(`should generate a RC with argument 'mode' equal 'report' and
Expand All @@ -29,7 +30,7 @@ describe("generateDefaultRC (internals)", () => {
generateReportConfiguration()
);

expect(rc).deep.equal(expectedResult);
assert.deepEqual(rc, expectedResult);
});

it(`should generate a RC with argument 'mode' equal 'scanner' and
Expand All @@ -40,7 +41,7 @@ describe("generateDefaultRC (internals)", () => {
generateScannerConfiguration()
);

expect(rc).deep.equal(expectedResult);
assert.deepEqual(rc, expectedResult);
});

it(`should generate a RC with argument 'mode' equal an Array ['complete'] and
Expand All @@ -53,6 +54,6 @@ describe("generateDefaultRC (internals)", () => {
generateScannerConfiguration()
);

expect(rc).deep.equal(expectedResult);
assert.deepEqual(rc, expectedResult);
});
});
30 changes: 14 additions & 16 deletions test/read.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import * as fs from "node:fs/promises";
import path from "node:path";
import os from "node:os";
import { fileURLToPath } from "node:url";
import assert from "node:assert";
import { describe, before, beforeEach, it, after } from "node:test";

// Import Third-party Dependencies
import { expect } from "chai";

// Import Internal Dependencies
import { read, CONSTANTS, memoized, memoize, clearMemoized } from "../src/index.js";
import { generateDefaultRC } from "../src/rc.js";

Expand All @@ -33,33 +31,33 @@ describe("read .nodesecurerc", () => {
it("should return a Node.js ENOENT Error because there is no .nodesecurerc file at the given location", async() => {
const result = await read(location);

expect(result.ok).equal(false);
assert(!result.ok);

const nodejsError = result.val as NodeJS.ErrnoException;
expect(nodejsError instanceof Error).equal(true);
expect(nodejsError.code).equal("ENOENT");
assert(nodejsError instanceof Error);
assert.equal(nodejsError.code, "ENOENT");
});

it("should read and create a new .nodesecurerc file because there is none at the given location", async() => {
const result = await read(location, { createIfDoesNotExist: true });

expect(result.ok).equal(true);
expect(result.val).deep.equal(generateDefaultRC());
assert(result.ok);
assert.deepEqual(result.val, generateDefaultRC());
});

it("should read and create a new .nodesecurerc with createMode equal to 'ci'", async() => {
const result = await read(location, { createMode: "ci" });

expect(result.ok).equal(true);
expect(result.val).deep.equal(generateDefaultRC("ci"));
assert(result.ok);
assert.deepEqual(result.val, generateDefaultRC("ci"));
});

it("should read fixtures/.nodesecurerc file", async() => {
await read(location, { createIfDoesNotExist: true });
const result = await read(fixtures, { createIfDoesNotExist: false });

expect(result.ok).equal(true);
expect(result.val).deep.equal({
assert(result.ok);
assert.deepEqual(result.val, {
version: "2.1.0",
i18n: "french",
strategy: "none",
Expand All @@ -86,19 +84,19 @@ describe("read | memoize option", () => {

it("should return the default value 'null' when the memoize option is not declared ", async() => {
await read(location, { createIfDoesNotExist: true });
expect(memoized()).to.eq(null);
assert.equal(memoized(), null);
});

it("should return the configuration passed in parameter", async() => {
await read(location, { createIfDoesNotExist: true, memoize: true });
expect(memoized()).to.deep.equal(generateDefaultRC());
assert.deepEqual(memoized(), generateDefaultRC());
});

it("must overwrite the previously stored payload", async() => {
await read(location, { createIfDoesNotExist: true, memoize: true });

memoize(generateDefaultRC("report"));

expect(memoized()).to.deep.equal(generateDefaultRC("report"));
assert.deepEqual(memoized(), generateDefaultRC("report"));
});
});
33 changes: 16 additions & 17 deletions test/write.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import * as fs from "node:fs/promises";
import path from "node:path";
import os from "node:os";

// Import Third-party Dependencies
import { expect } from "chai";
import assert from "node:assert";
import { describe, it, before, beforeEach, after } from "node:test"

// Import Internal Dependencies
import { read, write, CONSTANTS } from "../src/index.js";
Expand Down Expand Up @@ -32,46 +31,46 @@ describe("write and/or update .nodesecurerc", () => {
const payload = { ...generateDefaultRC(), version: "4.5.2" };
const result = await write(location, { payload });

expect(result.ok).equal(false);
assert(!result.ok);

const nodejsError = result.val as NodeJS.ErrnoException;
expect(nodejsError instanceof Error).equal(true);
expect(nodejsError.code).equal("ENOENT");
assert(nodejsError instanceof Error);
assert.equal(nodejsError.code, "ENOENT");
});

it("should fail to write because the payload is not matching the JSON Schema", async() => {
const payload = { foo: "bar" } as any;
const result = await write(location, { payload });

expect(result.ok).equal(false);
assert(!result.ok);

const value = result.val as Error;
expect(value instanceof Error).equal(true);
expect(value.message.includes("must have required property 'version'")).equal(true);
assert(value instanceof Error);
assert(value.message.includes("must have required property 'version'"));
});

it("should rewrite a complete payload (content of .nodesecurerc)", async() => {
const payload = { ...generateDefaultRC(), version: "4.5.2" };

const writeResult = await write(location, { payload });
expect(writeResult.ok).equal(true);
expect(writeResult.val).equal(void 0);
assert(writeResult.ok);
assert.equal(writeResult.val, void 0);

const readResult = await read(location, { createIfDoesNotExist: false });
expect(readResult.ok).equal(true);
expect(readResult.val).deep.equal(payload);
assert(readResult.ok);
assert.deepEqual(readResult.val, payload);
});

it("should partially update payload (content of .nodesecurerc)", async() => {
const defaultRC = generateDefaultRC();
const payload = { i18n: "french" as const };

const writeResult = await write(location, { payload, partialUpdate: true });
expect(writeResult.ok).equal(true);
expect(writeResult.val).equal(void 0);
assert(writeResult.ok);
assert.equal(writeResult.val, void 0);

const readResult = await read(location, { createIfDoesNotExist: false });
expect(readResult.ok).equal(true);
expect(readResult.val).deep.equal({ ...defaultRC, ...payload });
assert(readResult.ok);
assert.deepEqual(readResult.val, { ...defaultRC, ...payload });
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"forceConsistentCasingInFileNames": true,
"sourceMap": true,
"rootDir": "./src",
"types": ["node", "mocha"]
"types": ["node"]
},
"include": ["src", "src/schema/**/*.json"],
"exclude": ["node_modules", "dist"]
Expand Down