Skip to content

Commit f0e7dd0

Browse files
chore: Add tests to confirm the number of calls ro register
1 parent 266acda commit f0e7dd0

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/loader.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ describe("TypeScriptLoader", () => {
99
const fixturesPath = path.resolve(__dirname, "__fixtures__");
1010

1111
let loader: Loader;
12+
let tsNodeSpy = jest.spyOn(tsnode, "register");
1213

1314
function readFixtureContent(file: string): string {
1415
return fs.readFileSync(file).toString();
1516
}
1617

17-
beforeEach(() => {
18+
beforeAll(() => {
1819
loader = TypeScriptLoader();
1920
});
2021

@@ -28,6 +29,14 @@ describe("TypeScriptLoader", () => {
2829
expect(() => loader(filePath, readFixtureContent(filePath))).toThrowError();
2930
});
3031

32+
it("should use the same instance of ts-node across multiple calls", () => {
33+
const filePath = path.resolve(fixturesPath, "valid.fixture.ts");
34+
loader(filePath, readFixtureContent(filePath));
35+
loader(filePath, readFixtureContent(filePath));
36+
loader(filePath, readFixtureContent(filePath));
37+
expect(tsNodeSpy).toHaveBeenCalledTimes(1);
38+
});
39+
3140
it("should throw a TypeScriptCompileError on error", () => {
3241
try {
3342
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts");

lib/loader.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { register, RegisterOptions } from "ts-node";
33
import { TypeScriptCompileError } from "./typescript-compile-error";
44

55
export function TypeScriptLoader(options?: RegisterOptions): Loader {
6-
const tsNodeInstance = register({ ...options, compilerOptions: { module: "commonjs" } })
6+
const tsNodeInstance = register({
7+
...options,
8+
compilerOptions: { module: "commonjs" },
9+
});
710
return (path: string, content: string) => {
811
try {
912
// cosmiconfig requires the transpiled configuration to be CJS
10-
tsNodeInstance.compile(
11-
content,
12-
path
13-
);
13+
tsNodeInstance.compile(content, path);
1414
const result = require(path);
1515

1616
// `default` is used when exporting using export default, some modules

0 commit comments

Comments
 (0)