Skip to content

Commit

Permalink
Change testing framework to Jest (dividab#186)
Browse files Browse the repository at this point in the history
* Upgrade to jest

* Fix tests

* Remove llog

* Test not working

* Add explicit extensions (came from running mocha under ts-node)

* All pass

* Coverage
  • Loading branch information
jonaskello committed Nov 20, 2021
1 parent cef69d6 commit 9de71eb
Show file tree
Hide file tree
Showing 21 changed files with 2,336 additions and 1,139 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
- name: Report coverage
uses: codecov/codecov-action@v1
with:
file: coverage/coverage-final.json
file: coverage/lcov.info
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__tests__/
__stories__/
*.test.{js,ts,tsx}
8 changes: 0 additions & 8 deletions .nycrc.json

This file was deleted.

10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["<rootDir>/src/**/*.test.{ts,tsx}"],
collectCoverage: false,
coverageDirectory: "<rootDir>/coverage/",
collectCoverageFrom: ["<rootDir>/src/**/*.{ts,tsx}"],
coveragePathIgnorePatterns: ["/__tests__/"],
};
19 changes: 8 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@
"README.md"
],
"devDependencies": {
"@types/chai": "^4.1.4",
"@types/jest": "^27.0.3",
"@types/minimist": "^1.2.0",
"@types/mocha": "^5.2.3",
"@types/node": "^6.0.54",
"@types/strip-bom": "^3.0.0",
"@types/strip-json-comments": "^0.0.30",
"chai": "^4.1.2",
"husky": "^4.2.5",
"jest": "^27.3.1",
"lint-staged": "^10.2.11",
"mocha": "^5.2.0",
"nyc": "^11.4.1",
"prettier": "^2.0.5",
"rimraf": "^2.6.2",
"shelljs": "^0.7.5",
"ts-jest": "^27.0.7",
"ts-node": "^7.0.0",
"tslint": "^5.8.0",
"typescript": "^2.4.1"
"typescript": "^4.5.2"
},
"dependencies": {
"@types/json5": "^0.0.29",
Expand All @@ -47,12 +45,11 @@
"example:project": "yarn build && ts-node -r ./register.js -P ./example/project/tsconfig.json ./example/project/main.ts",
"example:api": "cd example/api && ts-node main.ts",
"example:perf": "cd example/perf && ts-node main.ts",
"test": "mocha",
"build": "rimraf lib && tsc -p src",
"build:test": "rimraf ./test/js_out && tsc -p test",
"test": "jest",
"test-coverage": "jest --coverage",
"build": "rimraf lib && tsc -p .",
"lint": "tslint './{src,tests}/**/*.ts{,x}'",
"verify": "yarn build && yarn lint && yarn coverage",
"coverage": "rimraf coverage .nyc_output && nyc yarn test",
"verify": "yarn build && yarn lint && yarn test-coverage",
"publish:major": "yarn build && node scripts/publish.js major",
"publish:minor": "yarn build && node scripts/publish.js minor",
"publish:patch": "yarn build && node scripts/publish.js patch",
Expand Down
36 changes: 23 additions & 13 deletions test/config-loader-tests.ts → src/__tests__/config-loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { assert } from "chai";
import {
configLoader,
loadConfig,
ConfigLoaderFailResult,
ConfigLoaderSuccessResult,
} from "../src/config-loader";
} from "../config-loader";
import { join } from "path";

describe("config-loader", (): void => {
Expand All @@ -20,9 +19,12 @@ describe("config-loader", (): void => {
});

const successResult = result as ConfigLoaderSuccessResult;
assert.equal(successResult.resultType, "success");
assert.equal(successResult.absoluteBaseUrl, "/foo/bar");
assert.equal(successResult.paths["asd"][0], "asd");
// assert.equal(successResult.resultType, "success");
// assert.equal(successResult.absoluteBaseUrl, "/foo/bar");
// assert.equal(successResult.paths["asd"][0], "asd");
expect(successResult.resultType).toBe("success");
expect(successResult.absoluteBaseUrl).toBe("/foo/bar");
expect(successResult.paths["asd"][0]).toBe("asd");
});

it("should use explicitParams when set and add cwd when path is relative", () => {
Expand All @@ -37,8 +39,10 @@ describe("config-loader", (): void => {
});

const successResult = result as ConfigLoaderSuccessResult;
assert.equal(successResult.resultType, "success");
assert.equal(successResult.absoluteBaseUrl, join("/baz", "bar/"));
// assert.equal(successResult.resultType, "success");
// assert.equal(successResult.absoluteBaseUrl, join("/baz", "bar/"));
expect(successResult.resultType).toBe("success");
expect(successResult.absoluteBaseUrl).toBe(join("/baz", "bar/"));
});

it("should fallback to tsConfigLoader when explicitParams is not set", () => {
Expand All @@ -54,8 +58,10 @@ describe("config-loader", (): void => {
});

const successResult = result as ConfigLoaderSuccessResult;
assert.equal(successResult.resultType, "success");
assert.equal(successResult.absoluteBaseUrl, join("/baz", "src"));
// assert.equal(successResult.resultType, "success");
// assert.equal(successResult.absoluteBaseUrl, join("/baz", "src"));
expect(successResult.resultType).toBe("success");
expect(successResult.absoluteBaseUrl).toBe(join("/baz", "src"));
});

it("should show an error message when baseUrl is missing", () => {
Expand All @@ -71,8 +77,10 @@ describe("config-loader", (): void => {
});

const failResult = result as ConfigLoaderFailResult;
assert.equal(failResult.resultType, "failed");
assert.isTrue(failResult.message.indexOf("baseUrl") > -1);
// assert.equal(failResult.resultType, "failed");
// assert.isTrue(failResult.message.indexOf("baseUrl") > -1);
expect(failResult.resultType).toBe("failed");
expect(failResult.message.indexOf("baseUrl") > -1).toBeTruthy();
});

it("should presume cwd to be a tsconfig file when loadConfig is called with absolute path to tsconfig.json", () => {
Expand All @@ -84,7 +92,9 @@ describe("config-loader", (): void => {
const result = loadConfig(configFile);

const successResult = result as ConfigLoaderSuccessResult;
assert.equal(successResult.resultType, "success");
assert.equal(successResult.configFileAbsolutePath, configFile);
// assert.equal(successResult.resultType, "success");
// assert.equal(successResult.configFileAbsolutePath, configFile);
expect(successResult.resultType).toBe("success");
expect(successResult.configFileAbsolutePath).toBe(configFile);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join, dirname } from "path";
import { removeExtension } from "../../src/filesystem";
import { removeExtension } from "../../filesystem";

export interface OneTest {
readonly name: string;
Expand All @@ -11,11 +11,19 @@ export interface OneTest {
readonly addMatchAll?: boolean;
readonly existingFiles: ReadonlyArray<string>;
readonly requestedModule: string;
readonly extensions?: ReadonlyArray<string>;
readonly extensions: ReadonlyArray<string>;
readonly packageJson?: {};
readonly expectedPath: string | undefined;
}

const defaultExtensionsWhenRunningInTsNode = [
".js",
".json",
".node",
".ts",
".tsx",
];

export const tests: ReadonlyArray<OneTest> = [
{
name: "should locate path that matches with star and exists",
Expand All @@ -26,6 +34,7 @@ export const tests: ReadonlyArray<OneTest> = [
existingFiles: [join("/root", "location", "mylib", "index.ts")],
requestedModule: "lib/mylib",
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve to correct path when many are specified",
Expand All @@ -52,6 +61,7 @@ export const tests: ReadonlyArray<OneTest> = [
],
requestedModule: "lib/mylib",
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should locate path that matches with star and exists with extension",
Expand All @@ -69,6 +79,7 @@ export const tests: ReadonlyArray<OneTest> = [
existingFiles: [join("/root", "location", "test.jpg")],
requestedModule: "lib/test.jpg",
expectedPath: join("/root", "location", "test.jpg"),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should locate path that matches without star and exists",
Expand All @@ -79,6 +90,7 @@ export const tests: ReadonlyArray<OneTest> = [
existingFiles: [join("/root", "location", "foo.ts")],
requestedModule: "lib/foo",
expectedPath: removeExtension(join("/root", "location", "foo.ts")),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve to parent folder when filename is in subfolder",
Expand All @@ -87,6 +99,7 @@ export const tests: ReadonlyArray<OneTest> = [
existingFiles: [join("/root", "location", "mylib", "index.ts")],
requestedModule: "lib/mylib",
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve from main field in package.json",
Expand All @@ -98,6 +111,7 @@ export const tests: ReadonlyArray<OneTest> = [
expectedPath: removeExtension(
join("/root", "location", "mylib", "kalle.ts")
),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve from main field in package.json (js)",
Expand Down Expand Up @@ -181,6 +195,7 @@ export const tests: ReadonlyArray<OneTest> = [
existingFiles: [join("/root", "mylib", "index.ts")],
requestedModule: "mylib",
expectedPath: dirname(join("/root", "mylib", "index.ts")),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should not resolve with the help of baseUrl when asked not to",
Expand All @@ -190,6 +205,7 @@ export const tests: ReadonlyArray<OneTest> = [
existingFiles: [join("/root", "mylib", "index.ts")],
requestedModule: "mylib",
expectedPath: undefined,
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should not locate path that does not match",
Expand All @@ -198,6 +214,7 @@ export const tests: ReadonlyArray<OneTest> = [
existingFiles: [join("root", "location", "mylib")],
requestedModule: "mylib",
expectedPath: undefined,
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should not resolve typings file (index.d.ts)",
Expand All @@ -208,5 +225,6 @@ export const tests: ReadonlyArray<OneTest> = [
existingFiles: [join("/root", "location", "mylib", "index.d.ts")],
requestedModule: "lib/mylib",
expectedPath: undefined,
extensions: defaultExtensionsWhenRunningInTsNode,
},
];
65 changes: 65 additions & 0 deletions src/__tests__/filesystem.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as Filesystem from "../filesystem";
import * as path from "path";

describe("filesystem", () => {
const fileThatExists = path.join(__dirname, "../../package.json");
const fileThatNotExists = path.join(__dirname, "../../package2.json");

it("should find file that exists, sync", () => {
const result = Filesystem.fileExistsSync(fileThatExists);
// assert.equal(result, true);
expect(result).toBe(true);
});

it("should not find file that not exists, sync", () => {
const result = Filesystem.fileExistsSync(fileThatNotExists);
// assert.equal(result, false);
expect(result).toBe(false);
});

it("should find file that exists, async", (done) => {
Filesystem.fileExistsAsync(fileThatExists, (_err, result) => {
try {
// assert.equal(result, true);
expect(result).toBe(true);
done();
} catch (error) {
done(error);
}
});
});

it("should not find file that not exists, async", (done) => {
Filesystem.fileExistsAsync(fileThatNotExists, (_err, result) => {
try {
// assert.equal(result, false);
expect(result).toBe(false);
done();
} catch (error) {
done(error);
}
});
});

it("should load json, sync", () => {
const result = Filesystem.readJsonFromDiskSync(fileThatExists);
// assert.isOk(result);
expect(result);
// assert.equal(result.main, "lib/index.js");
expect(result.main).toBe("lib/index.js");
});

it("should load json, async", (done) => {
Filesystem.readJsonFromDiskAsync(fileThatExists, (_err, result) => {
try {
// assert.isOk(result); // Asserts that object is truthy.
expect(result).toBeTruthy();
// assert.equal(result.main, "lib/index.js");
expect(result.main).toBe("lib/index.js");
done();
} catch (error) {
done(error);
}
});
});
});
33 changes: 28 additions & 5 deletions test/mapping-entry-test.ts → src/__tests__/mapping-entry.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { assert } from "chai";
import { getAbsoluteMappingEntries } from "../src/mapping-entry";
import { getAbsoluteMappingEntries } from "../mapping-entry";
import { join } from "path";

describe("mapping-entry", () => {
Expand All @@ -13,7 +12,24 @@ describe("mapping-entry", () => {
},
true
);
assert.deepEqual(result, [
// assert.deepEqual(result, [
// {
// pattern: "longest/pre/fix/*",
// paths: [join("/absolute", "base", "url", "foo2", "bar")],
// },
// {
// pattern: "pre/fix/*",
// paths: [join("/absolute", "base", "url", "foo3")],
// },
// {
// pattern: "*",
// paths: [
// join("/absolute", "base", "url", "foo1"),
// join("/absolute", "base", "url", "foo2"),
// ],
// },
// ]);
expect(result).toEqual([
{
pattern: "longest/pre/fix/*",
paths: [join("/absolute", "base", "url", "foo2", "bar")],
Expand All @@ -34,14 +50,21 @@ describe("mapping-entry", () => {

it("should should add a match-all pattern when requested", () => {
let result = getAbsoluteMappingEntries("/absolute/base/url", {}, true);
assert.deepEqual(result, [
// assert.deepEqual(result, [
// {
// pattern: "*",
// paths: [join("/absolute", "base", "url", "*")],
// },
// ]);
expect(result).toEqual([
{
pattern: "*",
paths: [join("/absolute", "base", "url", "*")],
},
]);

result = getAbsoluteMappingEntries("/absolute/base/url", {}, false);
assert.deepEqual(result, []);
// assert.deepEqual(result, []);
expect(result).toEqual([]);
});
});

0 comments on commit 9de71eb

Please sign in to comment.