|
1 | 1 | import fs from "node:fs";
|
2 | 2 | import path from "node:path";
|
3 | 3 |
|
4 |
| -import { Loader } from "cosmiconfig"; |
| 4 | +import type { LoaderResult, LoaderSync } from "cosmiconfig"; |
5 | 5 | import * as jiti from "jiti";
|
6 | 6 |
|
7 |
| -import { TypeScriptLoader } from "./loader"; |
| 7 | +import { TypeScriptLoader, TypeScriptLoaderSync } from "./loader"; |
8 | 8 | import { TypeScriptCompileError } from "./typescript-compile-error";
|
9 | 9 |
|
10 | 10 | // Handle jiti using `export default`
|
11 | 11 | jest.mock("jiti", () => {
|
12 | 12 | const actual = jest.requireActual("jiti");
|
13 | 13 | return {
|
14 | 14 | __esModule: true,
|
15 |
| - default: jest.fn(actual), |
| 15 | + createJiti: actual.createJiti, |
16 | 16 | };
|
17 | 17 | });
|
18 | 18 |
|
19 | 19 | describe("TypeScriptLoader", () => {
|
20 | 20 | const fixturesPath = path.resolve(__dirname, "__fixtures__");
|
21 |
| - const jitiSpy = jest.spyOn(jiti, "default"); |
22 |
| - |
23 |
| - let loader: Loader; |
| 21 | + let jitiCreateJitiSpy: jest.SpyInstance<typeof jiti.createJiti>; |
24 | 22 |
|
25 | 23 | function readFixtureContent(file: string): string {
|
26 | 24 | return fs.readFileSync(file).toString();
|
27 | 25 | }
|
28 | 26 |
|
29 |
| - beforeAll(() => { |
30 |
| - loader = TypeScriptLoader(); |
| 27 | + beforeEach(() => { |
| 28 | + jitiCreateJitiSpy = jest.spyOn(jiti, "createJiti"); |
31 | 29 | });
|
32 | 30 |
|
33 |
| - it("should parse a valid TS file", () => { |
34 |
| - const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); |
35 |
| - loader(filePath, readFixtureContent(filePath)); |
| 31 | + afterEach(() => { |
| 32 | + jest.restoreAllMocks(); |
36 | 33 | });
|
37 | 34 |
|
38 |
| - it("should fail on parsing an invalid TS file", () => { |
39 |
| - const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); |
40 |
| - expect((): unknown => |
41 |
| - loader(filePath, readFixtureContent(filePath)), |
42 |
| - ).toThrow(); |
43 |
| - }); |
| 35 | + describe("asynchronous", () => { |
| 36 | + let loader: (filepath: string, content: string) => Promise<LoaderResult>; |
44 | 37 |
|
45 |
| - it("should use the same instance of jiti across multiple calls", () => { |
46 |
| - const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); |
47 |
| - loader(filePath, readFixtureContent(filePath)); |
48 |
| - loader(filePath, readFixtureContent(filePath)); |
49 |
| - expect(jitiSpy).toHaveBeenCalledTimes(1); |
50 |
| - }); |
| 38 | + beforeEach(() => { |
| 39 | + loader = TypeScriptLoader(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should parse a valid TS file", async () => { |
| 43 | + const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); |
| 44 | + await loader(filePath, readFixtureContent(filePath)); |
| 45 | + }); |
51 | 46 |
|
52 |
| - it("should throw a TypeScriptCompileError on error", () => { |
53 |
| - try { |
| 47 | + it("should fail on parsing an invalid TS file", async () => { |
54 | 48 | const filePath = path.resolve(fixturesPath, "invalid.fixture.ts");
|
55 |
| - loader(filePath, readFixtureContent(filePath)); |
56 |
| - fail( |
57 |
| - "Error should be thrown upon failing to transpile an invalid TS file.", |
58 |
| - ); |
59 |
| - } catch (error: unknown) { |
60 |
| - expect(error).toBeInstanceOf(TypeScriptCompileError); |
61 |
| - } |
62 |
| - }); |
| 49 | + await expect( |
| 50 | + loader(filePath, readFixtureContent(filePath)), |
| 51 | + ).rejects.toThrow(); |
| 52 | + }); |
63 | 53 |
|
64 |
| - describe("jiti", () => { |
65 |
| - const unknownError = "Test Error"; |
| 54 | + it("should use the same instance of jiti across multiple calls", async () => { |
| 55 | + const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); |
| 56 | + await loader(filePath, readFixtureContent(filePath)); |
| 57 | + await loader(filePath, readFixtureContent(filePath)); |
| 58 | + expect(jitiCreateJitiSpy).toHaveBeenCalledTimes(1); |
| 59 | + }); |
66 | 60 |
|
67 |
| - let stub: jest.SpyInstance; |
| 61 | + it("should throw a TypeScriptCompileError on error", async () => { |
| 62 | + try { |
| 63 | + const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); |
| 64 | + await loader(filePath, readFixtureContent(filePath)); |
| 65 | + fail( |
| 66 | + "Error should be thrown upon failing to transpile an invalid TS file.", |
| 67 | + ); |
| 68 | + } catch (error: unknown) { |
| 69 | + expect(error).toBeInstanceOf(TypeScriptCompileError); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + describe("jiti", () => { |
| 74 | + const unknownError = "Test Error"; |
| 75 | + |
| 76 | + beforeEach(() => { |
| 77 | + jitiCreateJitiSpy.mockImplementation((() => ({ |
| 78 | + import: () => { |
| 79 | + // eslint-disable-next-line @typescript-eslint/only-throw-error |
| 80 | + throw unknownError; |
| 81 | + }, |
| 82 | + })) as never); |
| 83 | + |
| 84 | + loader = TypeScriptLoader(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("rethrows an error if it is not an instance of Error", async () => { |
| 88 | + try { |
| 89 | + await loader("filePath", "readFixtureContent(filePath)"); |
| 90 | + fail( |
| 91 | + "Error should be thrown upon failing to transpile an invalid TS file.", |
| 92 | + ); |
| 93 | + } catch (error: unknown) { |
| 94 | + expect(error).not.toBeInstanceOf(TypeScriptCompileError); |
| 95 | + expect(error).toStrictEqual(unknownError); |
| 96 | + } |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe("synchronous", () => { |
| 102 | + let loader: LoaderSync; |
68 | 103 |
|
69 | 104 | beforeEach(() => {
|
70 |
| - stub = jest.spyOn(jiti, "default").mockImplementation((() => () => { |
71 |
| - // eslint-disable-next-line @typescript-eslint/only-throw-error |
72 |
| - throw unknownError; |
73 |
| - }) as never); |
| 105 | + // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 106 | + loader = TypeScriptLoaderSync(); |
| 107 | + }); |
74 | 108 |
|
75 |
| - loader = TypeScriptLoader(); |
| 109 | + it("should parse a valid TS file", () => { |
| 110 | + const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); |
| 111 | + loader(filePath, readFixtureContent(filePath)); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should fail on parsing an invalid TS file", () => { |
| 115 | + const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); |
| 116 | + expect((): unknown => |
| 117 | + loader(filePath, readFixtureContent(filePath)), |
| 118 | + ).toThrow(); |
76 | 119 | });
|
77 | 120 |
|
78 |
| - afterEach(() => { |
79 |
| - stub.mockRestore(); |
| 121 | + it("should use the same instance of jiti across multiple calls", () => { |
| 122 | + const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); |
| 123 | + loader(filePath, readFixtureContent(filePath)); |
| 124 | + loader(filePath, readFixtureContent(filePath)); |
| 125 | + expect(jitiCreateJitiSpy).toHaveBeenCalledTimes(1); |
80 | 126 | });
|
81 | 127 |
|
82 |
| - it("rethrows an error if it is not an instance of Error", () => { |
| 128 | + it("should throw a TypeScriptCompileError on error", () => { |
83 | 129 | try {
|
84 |
| - loader("filePath", "readFixtureContent(filePath)"); |
| 130 | + const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); |
| 131 | + loader(filePath, readFixtureContent(filePath)); |
85 | 132 | fail(
|
86 | 133 | "Error should be thrown upon failing to transpile an invalid TS file.",
|
87 | 134 | );
|
88 | 135 | } catch (error: unknown) {
|
89 |
| - expect(error).not.toBeInstanceOf(TypeScriptCompileError); |
90 |
| - expect(error).toStrictEqual(unknownError); |
| 136 | + expect(error).toBeInstanceOf(TypeScriptCompileError); |
91 | 137 | }
|
92 | 138 | });
|
| 139 | + |
| 140 | + describe("jiti", () => { |
| 141 | + const unknownError = "Test Error"; |
| 142 | + |
| 143 | + beforeEach(() => { |
| 144 | + jitiCreateJitiSpy.mockImplementation((() => () => { |
| 145 | + // eslint-disable-next-line @typescript-eslint/only-throw-error |
| 146 | + throw unknownError; |
| 147 | + }) as never); |
| 148 | + |
| 149 | + // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 150 | + loader = TypeScriptLoaderSync(); |
| 151 | + }); |
| 152 | + |
| 153 | + it("rethrows an error if it is not an instance of Error", () => { |
| 154 | + try { |
| 155 | + loader("filePath", "readFixtureContent(filePath)"); |
| 156 | + fail( |
| 157 | + "Error should be thrown upon failing to transpile an invalid TS file.", |
| 158 | + ); |
| 159 | + } catch (error: unknown) { |
| 160 | + expect(error).not.toBeInstanceOf(TypeScriptCompileError); |
| 161 | + expect(error).toStrictEqual(unknownError); |
| 162 | + } |
| 163 | + }); |
| 164 | + }); |
93 | 165 | });
|
94 | 166 | });
|
0 commit comments