Skip to content

Commit

Permalink
test: convert tests to use vitest.
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Oct 31, 2022
1 parent edb6542 commit c70ee5e
Show file tree
Hide file tree
Showing 8 changed files with 7,025 additions and 11,890 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Expand Up @@ -7,7 +7,7 @@
"ignorePatterns": [
"lib/__fixtures__/**/*.ts",
"esbuild.config.mjs",
"jest.config.ts",
"vite.config.ts",
"coverage",
"dist",
"smoke-tests"
Expand Down
20 changes: 0 additions & 20 deletions jest.config.ts

This file was deleted.

5 changes: 3 additions & 2 deletions lib/index.spec.ts
@@ -1,5 +1,6 @@
import path from "path";
import path from "node:path";
import { cosmiconfig, cosmiconfigSync } from "cosmiconfig";
import { describe, expect, it } from "vitest";
import { TypeScriptLoader } from ".";

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

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

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

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

return writableTsNode;
});

import * as tsnode from "ts-node";

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

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

let loader: Loader;

Expand All @@ -19,6 +46,10 @@ 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 @@ -40,7 +71,7 @@ describe("TypeScriptLoader", () => {
try {
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts");
loader(filePath, readFixtureContent(filePath));
fail(
throw new Error(
"Error should be thrown upon failing to transpile an invalid TS file."
);
} catch (error: unknown) {
Expand All @@ -51,10 +82,10 @@ describe("TypeScriptLoader", () => {
describe("ts-node", () => {
const unknownError = "Test Error";

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

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

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

0 comments on commit c70ee5e

Please sign in to comment.