|
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 | + const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); |
| 63 | + await expect( |
| 64 | + loader(filePath, readFixtureContent(filePath)), |
| 65 | + ).rejects.toThrow(TypeScriptCompileError); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("jiti", () => { |
| 69 | + const unknownError = "Test Error"; |
| 70 | + |
| 71 | + beforeEach(() => { |
| 72 | + jitiCreateJitiSpy.mockImplementation((() => ({ |
| 73 | + import: () => { |
| 74 | + // eslint-disable-next-line @typescript-eslint/only-throw-error |
| 75 | + throw unknownError; |
| 76 | + }, |
| 77 | + })) as never); |
| 78 | + |
| 79 | + loader = TypeScriptLoader(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("rethrows an error if it is not an instance of Error", async () => { |
| 83 | + try { |
| 84 | + await loader("filePath", "invalidInput"); |
| 85 | + fail( |
| 86 | + "Error should be thrown upon failing to transpile an invalid TS file.", |
| 87 | + ); |
| 88 | + } catch (error: unknown) { |
| 89 | + expect(error).not.toBeInstanceOf(TypeScriptCompileError); |
| 90 | + expect(error).toStrictEqual(unknownError); |
| 91 | + } |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe("synchronous", () => { |
| 97 | + let loader: LoaderSync; |
68 | 98 |
|
69 | 99 | beforeEach(() => { |
70 | | - stub = jest.spyOn(jiti, "default").mockImplementation((() => () => { |
71 | | - // eslint-disable-next-line @typescript-eslint/only-throw-error |
72 | | - throw unknownError; |
73 | | - }) as never); |
| 100 | + // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 101 | + loader = TypeScriptLoaderSync(); |
| 102 | + }); |
74 | 103 |
|
75 | | - loader = TypeScriptLoader(); |
| 104 | + it("should parse a valid TS file", () => { |
| 105 | + const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); |
| 106 | + loader(filePath, readFixtureContent(filePath)); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should fail on parsing an invalid TS file", () => { |
| 110 | + const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); |
| 111 | + expect((): unknown => |
| 112 | + loader(filePath, readFixtureContent(filePath)), |
| 113 | + ).toThrow(); |
76 | 114 | }); |
77 | 115 |
|
78 | | - afterEach(() => { |
79 | | - stub.mockRestore(); |
| 116 | + it("should use the same instance of jiti across multiple calls", () => { |
| 117 | + const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); |
| 118 | + loader(filePath, readFixtureContent(filePath)); |
| 119 | + loader(filePath, readFixtureContent(filePath)); |
| 120 | + expect(jitiCreateJitiSpy).toHaveBeenCalledTimes(1); |
| 121 | + }); |
| 122 | + |
| 123 | + it("should throw a TypeScriptCompileError on error", () => { |
| 124 | + const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); |
| 125 | + expect((): unknown => |
| 126 | + loader(filePath, readFixtureContent(filePath)), |
| 127 | + ).toThrow(TypeScriptCompileError); |
80 | 128 | }); |
81 | 129 |
|
82 | | - it("rethrows an error if it is not an instance of Error", () => { |
83 | | - try { |
84 | | - loader("filePath", "readFixtureContent(filePath)"); |
85 | | - fail( |
86 | | - "Error should be thrown upon failing to transpile an invalid TS file.", |
87 | | - ); |
88 | | - } catch (error: unknown) { |
89 | | - expect(error).not.toBeInstanceOf(TypeScriptCompileError); |
90 | | - expect(error).toStrictEqual(unknownError); |
91 | | - } |
| 130 | + describe("jiti", () => { |
| 131 | + const unknownError = "Test Error"; |
| 132 | + |
| 133 | + beforeEach(() => { |
| 134 | + jitiCreateJitiSpy.mockImplementation((() => () => { |
| 135 | + // eslint-disable-next-line @typescript-eslint/only-throw-error |
| 136 | + throw unknownError; |
| 137 | + }) as never); |
| 138 | + |
| 139 | + // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 140 | + loader = TypeScriptLoaderSync(); |
| 141 | + }); |
| 142 | + |
| 143 | + it("rethrows an error if it is not an instance of Error", () => { |
| 144 | + try { |
| 145 | + loader("filePath", "invalidInput"); |
| 146 | + fail( |
| 147 | + "Error should be thrown upon failing to transpile an invalid TS file.", |
| 148 | + ); |
| 149 | + } catch (error: unknown) { |
| 150 | + expect(error).not.toBeInstanceOf(TypeScriptCompileError); |
| 151 | + expect(error).toStrictEqual(unknownError); |
| 152 | + } |
| 153 | + }); |
92 | 154 | }); |
93 | 155 | }); |
94 | 156 | }); |
0 commit comments