Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate to node native test runner #121

Merged
merged 5 commits into from
Apr 2, 2023
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Node.js CI

on:
push:
branches: master
branches: [master]
pull_request:

permissions:
Expand All @@ -29,7 +29,7 @@ jobs:
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test-only
run: npm run test:ci
nsci:
runs-on: ubuntu-latest
strategy:
Expand Down
2 changes: 1 addition & 1 deletion __snapshots__/depWalker.spec.js.snapshot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports['walk @slimio/is 1'] ={
exports['walk @slimio/is 1'] = {
"@slimio/is": {
"versions": {
"1.5.1": {
Expand Down
171 changes: 84 additions & 87 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,87 +1,84 @@
{
"name": "@nodesecure/scanner",
"version": "4.0.0",
"description": "A package API to run a static analysis of your module's dependencies.",
"exports": "./index.js",
"engines": {
"node": ">=16"
},
"scripts": {
"lint": "eslint src test",
"prepublishOnly": "pkg-ok",
"test": "npm run lint && npm run test-only",
"test-only": "cross-env esm-tape-runner 'test/**/*.spec.js' | tap-monkey",
"coverage": "c8 -r html npm run test-only"
},
"files": [
"src",
"types",
"index.js",
"index.d.ts"
],
"repository": {
"type": "git",
"url": "git+https://github.com/NodeSecure/scanner.git"
},
"keywords": [
"node",
"nodejs",
"security",
"cli",
"sast",
"scanner",
"static",
"code",
"analysis",
"node_modules",
"tree",
"npm",
"registry",
"graph",
"visualization",
"dependencies"
],
"author": "NodeSecure",
"license": "MIT",
"bugs": {
"url": "https://github.com/NodeSecure/scanner/issues"
},
"homepage": "https://github.com/NodeSecure/scanner#readme",
"devDependencies": {
"@nodesecure/eslint-config": "^1.6.0",
"@slimio/is": "^2.0.0",
"@small-tech/esm-tape-runner": "^2.0.0",
"@small-tech/tap-monkey": "^1.4.0",
"@types/node": "^18.13.0",
"c8": "^7.12.0",
"cross-env": "^7.0.3",
"dotenv": "^16.0.3",
"eslint": "^8.34.0",
"get-folder-size": "^4.0.0",
"pkg-ok": "^3.0.0",
"sinon": "^15.0.1",
"snap-shot-core": "^10.2.4",
"tape": "^5.6.1"
},
"dependencies": {
"@nodesecure/authors": "^1.0.1",
"@nodesecure/flags": "^2.4.0",
"@nodesecure/fs-walk": "^1.0.0",
"@nodesecure/i18n": "^3.0.0",
"@nodesecure/js-x-ray": "^6.0.1",
"@nodesecure/npm-registry-sdk": "^1.4.1",
"@nodesecure/ntlp": "^2.2.0",
"@nodesecure/utils": "^1.0.0",
"@nodesecure/vuln": "^1.7.0",
"@npm/types": "^1.0.2",
"@npmcli/arborist": "^6.2.2",
"@slimio/lock": "^1.0.0",
"builtins": "^5.0.1",
"combine-async-iterators": "^2.0.1",
"itertools": "^1.7.1",
"lodash.difference": "^4.5.0",
"pacote": "^15.0.8",
"semver": "^7.3.8"
},
"type": "module"
}
{
"name": "@nodesecure/scanner",
"version": "4.0.0",
"description": "A package API to run a static analysis of your module's dependencies.",
"exports": "./index.js",
"engines": {
"node": ">=16"
},
"scripts": {
"lint": "eslint src test",
"prepublishOnly": "pkg-ok",
"test": "npm run lint && npm run test-only",
"test:ci": "node --test test/**.spec.js test/**/*.spec.js",
"test-only": "node --test --test-reporter=spec test/**.spec.js test/**/*.spec.js",
"coverage": "c8 -r html npm run test-only"
},
"files": [
"src",
"types",
"index.js",
"index.d.ts"
],
"repository": {
"type": "git",
"url": "git+https://github.com/NodeSecure/scanner.git"
},
"keywords": [
"node",
"nodejs",
"security",
"cli",
"sast",
"scanner",
"static",
"code",
"analysis",
"node_modules",
"tree",
"npm",
"registry",
"graph",
"visualization",
"dependencies"
],
"author": "NodeSecure",
"license": "MIT",
"bugs": {
"url": "https://github.com/NodeSecure/scanner/issues"
},
"homepage": "https://github.com/NodeSecure/scanner#readme",
"devDependencies": {
"@nodesecure/eslint-config": "^1.7.0",
"@slimio/is": "^2.0.0",
"@types/node": "^18.15.11",
"c8": "^7.13.0",
"dotenv": "^16.0.3",
"eslint": "^8.37.0",
"get-folder-size": "^4.0.0",
"pkg-ok": "^3.0.0",
"sinon": "^15.0.3",
"snap-shot-core": "^10.2.4"
},
"dependencies": {
"@nodesecure/authors": "^1.0.2",
"@nodesecure/flags": "^2.4.0",
"@nodesecure/fs-walk": "^1.0.0",
"@nodesecure/i18n": "^3.2.0",
"@nodesecure/js-x-ray": "^6.0.1",
"@nodesecure/npm-registry-sdk": "^1.5.2",
"@nodesecure/ntlp": "^2.2.1",
"@nodesecure/utils": "^1.1.0",
"@nodesecure/vuln": "^1.7.0",
"@npm/types": "^1.0.2",
"@npmcli/arborist": "^6.2.6",
"@slimio/lock": "^1.0.0",
"builtins": "^5.0.1",
"combine-async-iterators": "^2.0.1",
"itertools": "^1.7.1",
"lodash.difference": "^4.5.0",
"pacote": "^15.1.1",
"semver": "^7.3.8"
},
"type": "module"
}
85 changes: 39 additions & 46 deletions test/class/dependency.spec.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,82 @@
// Import Node.js Dependencies
import { test } from "node:test";
import assert from "node:assert";

// Require Third-party Dependencies
import is from "@slimio/is";
import test from "tape";

// Require Internal Dependencies
import Dependency from "../../src/class/dependency.class.js";

test("Dependency class should act as expected by assertions", (tape) => {
tape.true(is.classObject(Dependency));
test("Dependency class should act as expected by assertions", () => {
assert.ok(is.classObject(Dependency));

const dep = new Dependency("semver", "1.0.0");
tape.deepEqual(dep.parent, {});
tape.strictEqual(dep.name, "semver");
tape.strictEqual(dep.version, "1.0.0");
tape.strictEqual(dep.fullName, "semver 1.0.0");
tape.strictEqual(dep.dev, false);
tape.strictEqual(dep.dependencyCount, 0);
tape.strictEqual(dep.existOnRemoteRegistry, true);
tape.deepEqual(dep.warnings, []);
tape.deepEqual(dep.alias, {});
tape.strictEqual(dep.gitUrl, null);
tape.strictEqual(Reflect.ownKeys(dep).length, 8);
assert.deepEqual(dep.parent, {});
assert.strictEqual(dep.name, "semver");
assert.strictEqual(dep.version, "1.0.0");
assert.strictEqual(dep.fullName, "semver 1.0.0");
assert.strictEqual(dep.dev, false);
assert.strictEqual(dep.dependencyCount, 0);
assert.strictEqual(dep.existOnRemoteRegistry, true);
assert.deepEqual(dep.warnings, []);
assert.deepEqual(dep.alias, {});
assert.strictEqual(dep.gitUrl, null);
assert.strictEqual(Reflect.ownKeys(dep).length, 8);

const flagOne = dep.flags;
const flagTwo = dep.flags;
tape.deepEqual(flagOne, flagTwo);
tape.false(flagOne === flagTwo);

tape.end();
assert.deepEqual(flagOne, flagTwo);
assert.ok(flagOne !== flagTwo);
});

test("Dependency children should write his parent as usedBy when exported", (tape) => {
test("Dependency children should write his parent as usedBy when exported", () => {
const semverDep = new Dependency("semver", "1.0.0");
const testDep = new Dependency("test", "1.0.0", semverDep);

tape.strictEqual(semverDep.dependencyCount, 1);
tape.deepEqual(testDep.parent, {
assert.strictEqual(semverDep.dependencyCount, 1);
assert.deepEqual(testDep.parent, {
[semverDep.name]: semverDep.version
});

const flatDep = testDep.exportAsPlainObject(void 0);
tape.deepEqual(flatDep.versions["1.0.0"].usedBy, {
assert.deepEqual(flatDep.versions["1.0.0"].usedBy, {
[semverDep.name]: semverDep.version
});

tape.end();
});

test("Create a dependency with one warning", (tape) => {
test("Create a dependency with one warning", () => {
const semverDep = new Dependency("semver", "1.0.0");
const fakeWarning = { foo: "bar" };
semverDep.warnings.push(fakeWarning);

const flatDep = semverDep.exportAsPlainObject(void 0);
const version = flatDep.versions["1.0.0"];
tape.deepEqual(version.flags, ["hasWarnings"]);
tape.strictEqual(version.warnings[0], fakeWarning);

tape.end();
assert.deepEqual(version.flags, ["hasWarnings"]);
assert.strictEqual(version.warnings[0], fakeWarning);
});

test("Create a GIT Dependency (flags.isGit must be set to true)", (tape) => {
test("Create a GIT Dependency (flags.isGit must be set to true)", () => {
const semverDep = new Dependency("semver", "1.0.0").isGit();
tape.is(semverDep.gitUrl, null);
assert.deepStrictEqual(semverDep.gitUrl, null);

const flatSemver = semverDep.exportAsPlainObject(void 0);
tape.true(flatSemver.versions["1.0.0"].flags.includes("isGit"));
assert.ok(flatSemver.versions["1.0.0"].flags.includes("isGit"));

const mochaDep = new Dependency("mocha", "1.0.0").isGit("https://github.com/mochajs/mocha");
tape.strictEqual(mochaDep.gitUrl, "https://github.com/mochajs/mocha");
assert.strictEqual(mochaDep.gitUrl, "https://github.com/mochajs/mocha");

const flatMocha = mochaDep.exportAsPlainObject(void 0);
tape.true(flatMocha.versions["1.0.0"].flags.includes("isGit"));

tape.end();
assert.ok(flatMocha.versions["1.0.0"].flags.includes("isGit"));
});

test("Dependency.addFlag should throw a TypeError if flagName is not string", (tape) => {
tape.plan(2);

test("Dependency.addFlag should throw a TypeError if flagName is not string", () => {
const semverDep = new Dependency("semver", "1.0.0");
try {
semverDep.addFlag(10);
}
catch (error) {
tape.strictEqual(error.name, "TypeError");
tape.strictEqual(error.message, "flagName argument must be typeof string");
}
assert.throws(
() => semverDep.addFlag(10),
{
name: "TypeError",
message: "flagName argument must be typeof string"
}
);
});
Loading