Skip to content

Commit 5033436

Browse files
committed
Test if a non-error error is thrown.
- Brings coverage to 100%.
1 parent 8eaf19b commit 5033436

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

.eslintrc.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
"parserOptions": {
55
"project": "./tsconfig.test.json"
66
},
7-
"ignorePatterns": ["lib/__fixtures__/**/*.ts", "jest.config.ts", "dist"],
7+
"ignorePatterns": [
8+
"lib/__fixtures__/**/*.ts",
9+
"jest.config.ts",
10+
"coverage",
11+
"dist"
12+
],
813
"rules": {
914
"@typescript-eslint/await-thenable": "warn",
1015
"@typescript-eslint/no-floating-promises": [

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.npmrc
2+
coverage
23
dist
34
node_modules

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
coverage
12
dist
23
lib/__fixtures__

lib/loader.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from "fs";
22
import path from "path";
33
import { Loader } from "cosmiconfig";
4+
import * as tsnode from "ts-node";
45
import { TypeScriptLoader } from "./loader";
56
import { TypeScriptCompileError } from "./typescript-compile-error";
67

@@ -38,4 +39,33 @@ describe("TypeScriptLoader", () => {
3839
expect(error).toBeInstanceOf(TypeScriptCompileError);
3940
}
4041
});
42+
43+
describe("ts-node", () => {
44+
const unknownError = "Test Error";
45+
46+
let stub: jest.SpyInstance<tsnode.Service, [service: tsnode.Service]>;
47+
48+
beforeEach(() => {
49+
stub = jest.spyOn(tsnode, "register").mockImplementation(() => {
50+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
51+
throw unknownError;
52+
});
53+
});
54+
55+
afterEach(() => {
56+
stub.mockRestore();
57+
});
58+
59+
it("rethrows an error if it is not an instance of Error", () => {
60+
try {
61+
loader("filePath", "readFixtureContent(filePath)");
62+
fail(
63+
"Error should be thrown upon failing to transpile an invalid TS file."
64+
);
65+
} catch (error: unknown) {
66+
expect(error).not.toBeInstanceOf(TypeScriptCompileError);
67+
expect(error).toStrictEqual(unknownError);
68+
}
69+
});
70+
});
4171
});

0 commit comments

Comments
 (0)