Skip to content

Commit

Permalink
refactor: migrate tests to jest (#162)
Browse files Browse the repository at this point in the history
* refactor: remove explicit mocha imports

* deps: add ts-jest

* refactor: rename test files

By default uses the convention of test-files being names *.test.ts. Renamed test files accordingly

* conf: create jest config

Uses ts-jest

* refactor: use body syntax in tests

* refactor: error assertion

We cannot use `instanceOf Error` in jest because of jestjs/jest#2549

* fix: console log issue

Jest automatically consumed and cleared calls to `console.log`. Fixed according to
https://stackoverflow.com/questions/54190596/no-console-log-to-stdout-when-running-npm-test-jest

* refactor: transition to jest

* deps: remove mocha
  • Loading branch information
ComradeVanti committed Mar 10, 2024
1 parent d18e534 commit 45230bd
Show file tree
Hide file tree
Showing 25 changed files with 4,487 additions and 1,236 deletions.
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
verbose: true,
};
5,612 changes: 4,437 additions & 1,175 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"node": ">=16"
},
"scripts": {
"test:quick": "cross-env NODE_ENV=test ts-mocha test/**/test-*.ts",
"test:full": "cross-env NODE_ENV=test ts-mocha test/**/test-*.ts --type-check",
"test:full": "cross-env NODE_ENV=test jest --runInBand",
"clean": "rimraf lib",
"build": "npm run clean && tsc -p tsconfig.build.json",
"build:watch": "tsc -w -p tsconfig.build.json",
Expand Down Expand Up @@ -41,8 +40,8 @@
"@semantic-release/git": "^10.0.1",
"@types/cli-table": "^0.3.2",
"@types/fs-extra": "^11.0.3",
"@types/jest": "^29.5.12",
"@types/libnpmsearch": "^2.0.4",
"@types/mocha": "^10.0.3",
"@types/node": "^16.18.68",
"@types/node-fetch": "^2.6.9",
"@types/npmlog": "^4.1.4",
Expand All @@ -57,14 +56,14 @@
"eslint": "^8.51.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jsdoc": "^47.0.2",
"mocha": "^10.1.0",
"jest": "^29.7.0",
"nock": "^13.2.4",
"prettier": "^2.8.8",
"rimraf": "^5.0.5",
"semantic-release": "^19.0.3",
"should": "^13.2.3",
"test-console": "^2.0.0",
"ts-mocha": "^10.0.0",
"ts-jest": "^29.1.2",
"typescript": "^5.2.2"
},
"dependencies": {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/error-type-guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import ErrnoException = NodeJS.ErrnoException;
* @throws {AssertionError} The given parameter is not an error.
*/
export function assertIsError(x: unknown): asserts x is ErrnoException {
if (!(x instanceof Error))
if (x === null || typeof x !== "object" || !("name" in x && "message" in x))
throw new AssertionError({
message: "Argument was not an error!",
actual: x,
});
}

Expand Down
5 changes: 2 additions & 3 deletions test/test-cmd-add.ts → test/cmd-add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { PackageUrl } from "../src/types/package-url";
import { semanticVersion } from "../src/types/semantic-version";
import { packageReference } from "../src/types/package-reference";
import { MockUnityProject, setupUnityProject } from "./setup/unity-project";
import { after } from "mocha";

describe("cmd-add.ts", function () {
const packageMissing = domainName("pkg-not-exist");
Expand Down Expand Up @@ -132,7 +131,7 @@ describe("cmd-add.ts", function () {
manifest.addDependency(packageA, "1.0.0", true, true)
);

before(async function () {
beforeAll(async function () {
mockProject = await setupUnityProject({ version: "2019.2.13f1" });
});

Expand All @@ -156,7 +155,7 @@ describe("cmd-add.ts", function () {
mockConsole.detach();
});

after(async function () {
afterAll(async function () {
await mockProject.restore();
});

Expand Down
5 changes: 2 additions & 3 deletions test/test-cmd-deps.ts → test/cmd-deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { buildPackument } from "./data-packument";
import { domainName } from "../src/types/domain-name";
import { packageReference } from "../src/types/package-reference";
import { MockUnityProject, setupUnityProject } from "./setup/unity-project";
import { before } from "mocha";

describe("cmd-deps.ts", function () {
const options: DepsOptions = {
Expand Down Expand Up @@ -44,7 +43,7 @@ describe("cmd-deps.ts", function () {
(packument) => packument.addVersion("1.0.0")
);

before(async function () {
beforeAll(async function () {
mockProject = await setupUnityProject({});
});

Expand All @@ -62,7 +61,7 @@ describe("cmd-deps.ts", function () {
stopMockRegistry();
mockConsole.detach();
});
after(async function () {
afterAll(async function () {
await mockProject.restore();
});

Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions test/test-cmd-remove.ts → test/cmd-remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { domainName } from "../src/types/domain-name";
import { semanticVersion } from "../src/types/semantic-version";
import { packageReference } from "../src/types/package-reference";
import { MockUnityProject, setupUnityProject } from "./setup/unity-project";
import { after } from "mocha";
import should from "should";

const packageA = domainName("com.example.package-a");
Expand All @@ -29,7 +28,7 @@ describe("cmd-remove.ts", function () {
let mockConsole: MockConsole = null!;
let mockProject: MockUnityProject = null!;

before(async function () {
beforeAll(async function () {
mockProject = await setupUnityProject({ manifest: defaultManifest });
});

Expand All @@ -42,7 +41,7 @@ describe("cmd-remove.ts", function () {
mockConsole.detach();
});

after(async function () {
afterAll(async function () {
await mockProject.restore();
});

Expand Down
5 changes: 2 additions & 3 deletions test/test-cmd-search.ts → test/cmd-search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { attachMockConsole, MockConsole } from "./mock-console";
import { domainName } from "../src/types/domain-name";
import { semanticVersion } from "../src/types/semantic-version";
import { MockUnityProject, setupUnityProject } from "./setup/unity-project";
import { before } from "mocha";

describe("cmd-search.ts", function () {
let mockConsole: MockConsole = null!;
Expand All @@ -25,7 +24,7 @@ describe("cmd-search.ts", function () {
},
};

before(async function () {
beforeAll(async function () {
mockProject = await setupUnityProject({});
});

Expand All @@ -38,7 +37,7 @@ describe("cmd-search.ts", function () {
mockConsole.detach();
});

after(async function () {
afterAll(async function () {
await mockProject.restore();
});

Expand Down
5 changes: 2 additions & 3 deletions test/test-cmd-view.ts → test/cmd-view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { domainName } from "../src/types/domain-name";
import { semanticVersion } from "../src/types/semantic-version";
import { packageReference } from "../src/types/package-reference";
import { MockUnityProject, setupUnityProject } from "./setup/unity-project";
import { after, before } from "mocha";

const packageA = domainName("com.example.package-a");
const packageUp = domainName("com.example.package-up");
Expand Down Expand Up @@ -104,7 +103,7 @@ describe("cmd-view.ts", function () {
)
);

before(async function () {
beforeAll(async function () {
mockProject = await setupUnityProject({});
});

Expand All @@ -121,7 +120,7 @@ describe("cmd-view.ts", function () {
mockConsole.detach();
});

after(async function () {
afterAll(async function () {
await mockProject.restore();
});

Expand Down
11 changes: 6 additions & 5 deletions test/test-domain-name.ts → test/domain-name.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from "mocha";
import {
domainName,
isDomainName,
Expand Down Expand Up @@ -31,8 +30,9 @@ describe("domain-name", function () {
"at.ac.my-school",
"dev.comradevanti123",
].forEach((s) =>
it(`"${s}" should be domain-name`, () =>
should(isDomainName(s)).be.true())
it(`"${s}" should be domain-name`, () => {
should(isDomainName(s)).be.true();
})
);
[
"",
Expand All @@ -46,8 +46,9 @@ describe("domain-name", function () {
// No trailing hyphens
"com.unity-",
].forEach((s) =>
it(`"${s}" should not be domain-name`, () =>
should(isDomainName(s)).be.false())
it(`"${s}" should not be domain-name`, () => {
should(isDomainName(s)).be.false();
})
);
});
describe("internal package", function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from "mocha";
import {
tryCompareEditorVersion,
tryParseEditorVersion,
Expand Down
5 changes: 2 additions & 3 deletions test/test-env.ts → test/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { registryUrl } from "../src/types/registry-url";
import { TokenAuth, UPMConfig } from "../src/types/upm-config";
import { NpmAuth } from "another-npm-registry-client";
import { MockUnityProject, setupUnityProject } from "./setup/unity-project";
import { afterEach, before } from "mocha";
import { manifestPathFor } from "../src/types/project-manifest";
import fse from "fs-extra";

Expand All @@ -29,7 +28,7 @@ describe("env", function () {
let mockConsole: MockConsole = null!;
let mockProject: MockUnityProject = null!;

before(async function () {
beforeAll(async function () {
mockProject = await setupUnityProject({
version: "2019.2.13f1",
upmConfig: testUpmConfig,
Expand All @@ -45,7 +44,7 @@ describe("env", function () {
await mockProject.reset();
});

after(async function () {
afterAll(async function () {
await mockProject.restore();
});

Expand Down
File renamed without changes.
10 changes: 6 additions & 4 deletions test/test-package-id.ts → test/package-id.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { describe } from "mocha";
import should from "should";
import { isPackageId } from "../src/types/package-id";

describe("package-id", function () {
describe("validate", function () {
["[email protected]"].forEach((s) =>
it(`"${s}" should be package-id`, () => should(isPackageId(s)).be.true())
it(`"${s}" should be package-id`, () => {
should(isPackageId(s)).be.true();
})
);

[
Expand All @@ -17,8 +18,9 @@ describe("package-id", function () {
"com.my-package@1",
"[email protected]",
].forEach((s) =>
it(`"${s}" should not be package-id`, () =>
should(isPackageId(s)).be.false())
it(`"${s}" should not be package-id`, () => {
should(isPackageId(s)).be.false();
})
);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from "mocha";
import should from "should";
import {
isPackageReference,
Expand Down
1 change: 0 additions & 1 deletion test/test-package-url.ts → test/package-url.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from "mocha";
import should from "should";
import { isPackageUrl } from "../src/types/package-url";

Expand Down
1 change: 0 additions & 1 deletion test/test-packument.ts → test/packument.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { tryGetLatestVersion } from "../src/types/packument";
import "should";
import { describe } from "mocha";
import should from "should";
import { semanticVersion } from "../src/types/semantic-version";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
loadProjectManifest,
saveProjectManifest,
} from "../src/utils/project-manifest-io";
import { describe } from "mocha";
import { shouldNotHaveAnyDependencies } from "./project-manifest-assertions";
import { DomainName, domainName } from "../src/types/domain-name";
import { semanticVersion } from "../src/types/semantic-version";
Expand All @@ -21,45 +20,45 @@ import { buildProjectManifest } from "./data-project-manifest";
import { removeScope } from "../src/types/scoped-registry";
import { exampleRegistryUrl } from "./mock-registry";

describe("project-manifest io", function () {
describe("project-manifest io", () => {
let mockConsole: MockConsole = null!;
let mockProject: MockUnityProject = null!;

before(async function () {
beforeAll(async () => {
mockProject = await setupUnityProject({});
});

beforeEach(function () {
beforeEach(() => {
mockConsole = attachMockConsole();
});

afterEach(async function () {
afterEach(async () => {
await mockProject.reset();
mockConsole.detach();
});

after(async function () {
afterAll(async () => {
await mockProject.restore();
});

it("loadManifest", async function () {
it("loadManifest", async () => {
const manifest = await loadProjectManifest(mockProject.projectPath);
should(manifest).be.deepEqual({ dependencies: {} });
});
it("no manifest file", async function () {
it("no manifest file", async () => {
const manifest = await loadProjectManifest("/invalid-path");
should(manifest).be.null();
mockConsole.hasLineIncluding("out", "does not exist").should.be.ok();
});
it("wrong json content", async function () {
it("wrong json content", async () => {
const manifestPath = manifestPathFor(mockProject.projectPath);
fse.writeFileSync(manifestPath, "invalid data");

const manifest = await loadProjectManifest(mockProject.projectPath);
should(manifest).be.null();
mockConsole.hasLineIncluding("out", "failed to parse").should.be.ok();
});
it("saveManifest", async function () {
it("saveManifest", async () => {
let manifest = (await loadProjectManifest(mockProject.projectPath))!;
shouldNotHaveAnyDependencies(manifest);
manifest = addDependency(
Expand All @@ -73,7 +72,7 @@ describe("project-manifest io", function () {
const manifest2 = await loadProjectManifest(mockProject.projectPath);
should(manifest2).be.deepEqual(manifest);
});
it("manifest-path is correct", function () {
it("manifest-path is correct", () => {
const manifestPath = manifestPathFor("test-openupm-cli");
const expected = path.join("test-openupm-cli", "Packages", "manifest.json");
should(manifestPath).be.equal(expected);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from "mocha";
import {
addDependency,
addScopedRegistry,
Expand Down
13 changes: 6 additions & 7 deletions test/test-registry-url.ts → test/registry-url.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from "mocha";
import { coerceRegistryUrl, isRegistryUrl } from "../src/types/registry-url";
import should from "should";

Expand All @@ -22,11 +21,11 @@ describe("registry-url", function () {
);
});
describe("coerce", function () {
it("should coerce urls without protocol", () =>
should(coerceRegistryUrl("test.com")).be.equal("http://test.com"));
it("should remove trailing slash", () =>
should(coerceRegistryUrl("http://test.com/")).be.equal(
"http://test.com"
));
it("should coerce urls without protocol", () => {
should(coerceRegistryUrl("test.com")).be.equal("http://test.com");
});
it("should remove trailing slash", () => {
should(coerceRegistryUrl("http://test.com/")).be.equal("http://test.com");
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from "mocha";
import {
addScope,
hasScope,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe } from "mocha";
import should from "should";
import { isSemanticVersion } from "../src/types/semantic-version";

Expand Down
Loading

0 comments on commit 45230bd

Please sign in to comment.