Skip to content

Commit c70ee5e

Browse files
committed
test: convert tests to use vitest.
1 parent edb6542 commit c70ee5e

File tree

8 files changed

+7025
-11890
lines changed

8 files changed

+7025
-11890
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"ignorePatterns": [
88
"lib/__fixtures__/**/*.ts",
99
"esbuild.config.mjs",
10-
"jest.config.ts",
10+
"vite.config.ts",
1111
"coverage",
1212
"dist",
1313
"smoke-tests"

jest.config.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

lib/index.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import path from "path";
1+
import path from "node:path";
22
import { cosmiconfig, cosmiconfigSync } from "cosmiconfig";
3+
import { describe, expect, it } from "vitest";
34
import { TypeScriptLoader } from ".";
45

56
describe("TypeScriptLoader", () => {
@@ -36,7 +37,7 @@ describe("TypeScriptLoader", () => {
3637

3738
try {
3839
await cfg.load(path.resolve(fixturesPath, "invalid.fixture.ts"));
39-
fail("Should fail to load invalid TS");
40+
throw new Error("Should fail to load invalid TS");
4041
} catch (error: any) {
4142
expect(error?.name).toStrictEqual("TypeScriptCompileError");
4243
}

lib/loader.spec.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
1-
import fs from "fs";
2-
import path from "path";
1+
import fs from "node:fs";
2+
import path from "node:path";
33
import { Loader } from "cosmiconfig";
4+
import {
5+
afterAll,
6+
afterEach,
7+
beforeAll,
8+
beforeEach,
9+
describe,
10+
expect,
11+
it,
12+
SpyInstance,
13+
vi,
14+
} from "vitest";
15+
16+
vi.mock("ts-node", async () => {
17+
const tsnode = await vi.importActual<typeof import("ts-node")>("ts-node");
18+
19+
let writableTsNode: any = {};
20+
Object.keys(tsnode).forEach((key) =>
21+
Object.defineProperty(writableTsNode, key, {
22+
value: (tsnode as any)[key],
23+
writable: true,
24+
})
25+
);
26+
27+
return writableTsNode;
28+
});
29+
430
import * as tsnode from "ts-node";
31+
532
import { TypeScriptLoader } from "./loader";
633
import { TypeScriptCompileError } from "./typescript-compile-error";
734

835
describe("TypeScriptLoader", () => {
936
const fixturesPath = path.resolve(__dirname, "__fixtures__");
10-
const tsNodeSpy = jest.spyOn(tsnode, "register");
37+
const tsNodeSpy = vi.spyOn(tsnode, "register");
1138

1239
let loader: Loader;
1340

@@ -19,6 +46,10 @@ describe("TypeScriptLoader", () => {
1946
loader = TypeScriptLoader();
2047
});
2148

49+
afterAll(() => {
50+
vi.restoreAllMocks();
51+
});
52+
2253
it("should parse a valid TS file", () => {
2354
const filePath = path.resolve(fixturesPath, "valid.fixture.ts");
2455
loader(filePath, readFixtureContent(filePath));
@@ -40,7 +71,7 @@ describe("TypeScriptLoader", () => {
4071
try {
4172
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts");
4273
loader(filePath, readFixtureContent(filePath));
43-
fail(
74+
throw new Error(
4475
"Error should be thrown upon failing to transpile an invalid TS file."
4576
);
4677
} catch (error: unknown) {
@@ -51,10 +82,10 @@ describe("TypeScriptLoader", () => {
5182
describe("ts-node", () => {
5283
const unknownError = "Test Error";
5384

54-
let stub: jest.SpyInstance<tsnode.Service, [service: tsnode.Service]>;
85+
let stub: SpyInstance<[service: tsnode.Service], tsnode.Service>;
5586

5687
beforeEach(() => {
57-
stub = jest.spyOn(tsnode, "register").mockImplementation(
88+
stub = vi.spyOn(tsnode, "register").mockImplementation(
5889
() =>
5990
({
6091
compile: (): string => {
@@ -73,7 +104,7 @@ describe("TypeScriptLoader", () => {
73104
it("rethrows an error if it is not an instance of Error", () => {
74105
try {
75106
loader("filePath", "readFixtureContent(filePath)");
76-
fail(
107+
throw new Error(
77108
"Error should be thrown upon failing to transpile an invalid TS file."
78109
);
79110
} catch (error: unknown) {

lib/typescript-compile-error.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, expect, it } from "vitest";
12
import { TypeScriptCompileError } from "./typescript-compile-error";
23

34
describe("TypeScriptCompileError", () => {

0 commit comments

Comments
 (0)