Skip to content

Commit

Permalink
refactor(test): use Node.js native test runner (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeMRF authored Feb 24, 2023
1 parent 7f4a8a3 commit 52c1010
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 114 deletions.
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"test": "test"
},
"scripts": {
"lint": "cross-env eslint index.js",
"test-only": "cross-env esm-tape-runner 'test/**/*.spec.js' | tap-monkey",
"lint": "eslint index.js",
"test-only": "node --test test/",
"test": "npm run lint && npm run test-only",
"coverage": "c8 -r html npm test"
},
Expand All @@ -35,12 +35,7 @@
"homepage": "https://github.com/NodeSecure/utils#readme",
"devDependencies": {
"@nodesecure/eslint-config": "^1.5.0",
"@slimio/is": "^2.0.0",
"@small-tech/esm-tape-runner": "^2.0.0",
"@small-tech/tap-monkey": "^1.4.0",
"c8": "^7.12.0",
"cross-env": "^7.0.3",
"eslint": "^8.23.0",
"tape": "^5.6.0"
"eslint": "^8.23.0"
}
}
37 changes: 16 additions & 21 deletions test/formatBytes.spec.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
// Import Third-party Dependencies
import test from "tape";
// Import Node.js Dependencies
import { describe, it } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import * as utils from "../index.js";

test("formatBytes should return '0 B' if bytes argument is equal zero", (tape) => {
tape.equal(utils.formatBytes(0), "0 B");
describe("formatBytes", () => {
it("should return '0 B' if bytes argument is equal zero", () => {
assert.equal(utils.formatBytes(0), "0 B");
});

tape.end();
});

test("formatBytes should format 10 bytes", (tape) => {
tape.equal(utils.formatBytes(10), "10 B");

tape.end();
});

test("formatBytes should format 3000 bytes in KB with two fixed number", (tape) => {
tape.equal(utils.formatBytes(3000), "2.93 KB");

tape.end();
});
it("should format 10 bytes", () => {
assert.equal(utils.formatBytes(10), "10 B");
});

test("formatBytes should format 822_223_900 bytes in MB", (tape) => {
tape.equal(utils.formatBytes(822_223_900), "784.13 MB");
it("should format 3000 bytes in KB with two fixed number", () => {
assert.equal(utils.formatBytes(3000), "2.93 KB");
});

tape.end();
it("should format 822_223_900 bytes in MB", () => {
assert.equal(utils.formatBytes(822_223_900), "784.13 MB");
});
});
25 changes: 12 additions & 13 deletions test/locationToString.spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// Import Third-party Dependencies
import test from "tape";
// Import Node.js Dependencies
import { describe, it } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import * as utils from "../index.js";

test("locationToString should return the location array in string syntax", (tape) => {
const str = utils.locationToString([[1, 2], [2, 4]]);
tape.equal(str, "[1:2] - [2:4]");
describe("locationToString", () => {
it("should return the location array in string syntax", () => {
const str = utils.locationToString([[1, 2], [2, 4]]);
assert.equal(str, "[1:2] - [2:4]");
});

tape.end();
});

test("locationToString should ignore elements after length 1", (tape) => {
const str = utils.locationToString([[1, 2, 3], [2, 4, 10], [50]]);
tape.equal(str, "[1:2] - [2:4]");

tape.end();
it("should ignore elements after length 1", () => {
const str = utils.locationToString([[1, 2, 3], [2, 4, 10], [50]]);
assert.equal(str, "[1:2] - [2:4]");
});
});
102 changes: 45 additions & 57 deletions test/parseManifestAuthor.spec.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,61 @@
// Import Third-party Dependencies
import test from "tape";
import is from "@slimio/is";
// Import Node.js Dependencies
import { describe, it } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import * as utils from "../index.js";

test("manifestAuthorRegex must return a RegExp", (tape) => {
const regex = utils.manifestAuthorRegex();
tape.true(is.regExp(regex));

tape.end();
});

test("parse a name field", (tape) => {
const result = utils.parseManifestAuthor("GENTILHOMME Thomas");
tape.deepEqual(result, {
name: "GENTILHOMME Thomas"
describe("manifestAuthorRegex", () => {
it("must return a RegExp", () => {
const regex = utils.manifestAuthorRegex();
assert.ok(regex instanceof RegExp);
});

tape.end();
});

test("parse a generic name <email> field", (tape) => {
const result = utils.parseManifestAuthor("GENTILHOMME Thomas <gentilhomme.thomas@gmail.com>");
tape.deepEqual(result, {
name: "GENTILHOMME Thomas",
email: "gentilhomme.thomas@gmail.com"
describe("parseManifestAuthor", () => {
it("parse a name field", () => {
const result = utils.parseManifestAuthor("GENTILHOMME Thomas");
assert.deepEqual(result, {
name: "GENTILHOMME Thomas"
});
});

tape.end();
});

test("parse an author field with name, email and URL", (tape) => {
const result = utils.parseManifestAuthor("John-David Dalton <john.david.dalton@gmail.com> (http://allyoucanleet.com/)");
tape.deepEqual(result, {
name: "John-David Dalton",
email: "john.david.dalton@gmail.com",
url: "http://allyoucanleet.com/"
it("parse a generic name <email> field", () => {
const result = utils.parseManifestAuthor("GENTILHOMME Thomas <gentilhomme.thomas@gmail.com>");
assert.deepEqual(result, {
name: "GENTILHOMME Thomas",
email: "gentilhomme.thomas@gmail.com"
});
});

tape.end();
});

test("parse an author field with name and URL", (tape) => {
const result = utils.parseManifestAuthor("John-David Dalton (http://allyoucanleet.com/)");
tape.deepEqual(result, {
name: "John-David Dalton",
url: "http://allyoucanleet.com/"
it("parse an author field with name, email and URL", () => {
const result = utils.parseManifestAuthor("John-David Dalton <john.david.dalton@gmail.com> (http://allyoucanleet.com/)");
assert.deepEqual(result, {
name: "John-David Dalton",
email: "john.david.dalton@gmail.com",
url: "http://allyoucanleet.com/"
});
});

tape.end();
});

test("empty string must return empty object", (tape) => {
const result = utils.parseManifestAuthor("");
tape.deepEqual(result, {});

tape.end();
});

test("parseManifestAuthor must throw an Error if the argument is not a string", (tape) => {
tape.plan(1);
it("parse an author field with name and URL", () => {
const result = utils.parseManifestAuthor("John-David Dalton (http://allyoucanleet.com/)");
assert.deepEqual(result, {
name: "John-David Dalton",
url: "http://allyoucanleet.com/"
});
});

try {
utils.parseManifestAuthor(null);
}
catch (error) {
tape.strictEqual(error.message, "expected manifestAuthorField to be a string");
}
it("empty string must return empty object", () => {
const result = utils.parseManifestAuthor("");
assert.deepEqual(result, {});
});

tape.end();
it("must throw an Error if the argument is not a string", () => {
try {
utils.parseManifestAuthor(null);
}
catch (error) {
assert.strictEqual(error.message, "expected manifestAuthorField to be a string");
}
});
});
29 changes: 14 additions & 15 deletions test/taggedString.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
// Import Third-party Dependencies
import test from "tape";
// Import Node.js Dependencies
import { describe, it } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import * as utils from "../index.js";

test("taggedString with numeric parameter", (tape) => {
const clojureHello = utils.taggedString`Hello ${0}`;
tape.strictEqual(clojureHello(), "Hello ");
tape.strictEqual(clojureHello("world"), "Hello world");
describe("taggedString", () => {
it("with numeric parameter", () => {
const clojureHello = utils.taggedString`Hello ${0}`;
assert.strictEqual(clojureHello(), "Hello ");
assert.strictEqual(clojureHello("world"), "Hello world");
});

tape.end();
});

test("taggedString with nammed parameter", (tape) => {
const clojureFoo = utils.taggedString`Hello ${"word"}`;
tape.strictEqual(clojureFoo({}), "Hello ");
tape.strictEqual(clojureFoo({ word: "bar" }), "Hello bar");

tape.end();
it("with nammed parameter", () => {
const clojureFoo = utils.taggedString`Hello ${"word"}`;
assert.strictEqual(clojureFoo({}), "Hello ");
assert.strictEqual(clojureFoo({ word: "bar" }), "Hello bar");
});
});

0 comments on commit 52c1010

Please sign in to comment.