Skip to content

Commit

Permalink
test: rever to jest to test cjs/require things
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Mar 1, 2023
1 parent 893579d commit 26e9bf4
Show file tree
Hide file tree
Showing 8 changed files with 12,521 additions and 8,131 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"ignorePatterns": [
"lib/__fixtures__/**/*.ts",
"esbuild.config.mjs",
"vite.config.ts",
"jest.config.ts",
"coverage",
"dist",
"smoke-tests"
Expand Down
20 changes: 20 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Config } from "@jest/types";
const config: Config.InitialOptions = {
collectCoverageFrom: [
"<rootDir>/lib/**/*",
"!<rootDir>/lib/__fixtures__/**/*",
],
globals: {
"ts-jest": {
tsconfig: "tsconfig.json",
},
},
moduleFileExtensions: ["ts", "js"],
transform: {
"^.+\\.ts$": "ts-jest",
},
testEnvironment: "node",
testRegex: "\\.spec\\.[jt]s$",
};

export default config;
5 changes: 2 additions & 3 deletions lib/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from "node:path";
import path from "path";
import { cosmiconfig, cosmiconfigSync } from "cosmiconfig";
import { describe, expect, it } from "vitest";
import { TypeScriptLoader } from ".";

describe("TypeScriptLoader", () => {
Expand Down Expand Up @@ -37,7 +36,7 @@ describe("TypeScriptLoader", () => {

try {
await cfg.load(path.resolve(fixturesPath, "invalid.fixture.ts"));
throw new Error("Should fail to load invalid TS");
fail("Should fail to load invalid TS");
} catch (error: any) {
expect(error?.name).toStrictEqual("TypeScriptCompileError");
}
Expand Down
46 changes: 7 additions & 39 deletions lib/loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
import fs from "node:fs";
import path from "node:path";
import fs from "fs";
import path from "path";
import { Loader } from "cosmiconfig";
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
SpyInstance,
vi,
} from "vitest";
import * as tsnode from "ts-node";

import { TypeScriptLoader } from "./loader";
import { TypeScriptCompileError } from "./typescript-compile-error";

vi.mock("ts-node", async () => {
const actualTsnode = await vi.importActual<typeof import("ts-node")>(
"ts-node"
);

const writableTsNode: any = {};
Object.keys(actualTsnode).forEach((key) =>
Object.defineProperty(writableTsNode, key, {
value: (actualTsnode as any)[key],
writable: true,
})
);

return writableTsNode;
});

describe("TypeScriptLoader", () => {
const fixturesPath = path.resolve(__dirname, "__fixtures__");
const tsNodeSpy = vi.spyOn(tsnode, "register");
const tsNodeSpy = jest.spyOn(tsnode, "register");

let loader: Loader;

Expand All @@ -47,10 +19,6 @@ describe("TypeScriptLoader", () => {
loader = TypeScriptLoader();
});

afterAll(() => {
vi.restoreAllMocks();
});

it("should parse a valid TS file", () => {
const filePath = path.resolve(fixturesPath, "valid.fixture.ts");
loader(filePath, readFixtureContent(filePath));
Expand All @@ -72,7 +40,7 @@ describe("TypeScriptLoader", () => {
try {
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts");
loader(filePath, readFixtureContent(filePath));
throw new Error(
fail(
"Error should be thrown upon failing to transpile an invalid TS file."
);
} catch (error: unknown) {
Expand All @@ -83,10 +51,10 @@ describe("TypeScriptLoader", () => {
describe("ts-node", () => {
const unknownError = "Test Error";

let stub: SpyInstance<[service: tsnode.Service], tsnode.Service>;
let stub: jest.SpyInstance<tsnode.Service, [service: tsnode.Service]>;

beforeEach(() => {
stub = vi.spyOn(tsnode, "register").mockImplementation(
stub = jest.spyOn(tsnode, "register").mockImplementation(
() =>
({
compile: (): string => {
Expand All @@ -105,7 +73,7 @@ describe("TypeScriptLoader", () => {
it("rethrows an error if it is not an instance of Error", () => {
try {
loader("filePath", "readFixtureContent(filePath)");
throw new Error(
fail(
"Error should be thrown upon failing to transpile an invalid TS file."
);
} catch (error: unknown) {
Expand Down
1 change: 0 additions & 1 deletion lib/typescript-compile-error.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe, expect, it } from "vitest";
import { TypeScriptCompileError } from "./typescript-compile-error";

describe("TypeScriptCompileError", () => {
Expand Down

0 comments on commit 26e9bf4

Please sign in to comment.