Skip to content

Commit 023cf79

Browse files
authored
Merge pull request #24 from alanquigley-toast/ts-node-single-instance
Update loader.ts
2 parents 0d871aa + 5e4d4fb commit 023cf79

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/loader.spec.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import { TypeScriptCompileError } from "./typescript-compile-error";
77

88
describe("TypeScriptLoader", () => {
99
const fixturesPath = path.resolve(__dirname, "__fixtures__");
10+
const tsNodeSpy = jest.spyOn(tsnode, "register");
1011

1112
let loader: Loader;
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,13 @@ 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+
expect(tsNodeSpy).toHaveBeenCalledTimes(1);
37+
});
38+
3139
it("should throw a TypeScriptCompileError on error", () => {
3240
try {
3341
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts");
@@ -46,10 +54,16 @@ describe("TypeScriptLoader", () => {
4654
let stub: jest.SpyInstance<tsnode.Service, [service: tsnode.Service]>;
4755

4856
beforeEach(() => {
49-
stub = jest.spyOn(tsnode, "register").mockImplementation(() => {
50-
// eslint-disable-next-line @typescript-eslint/no-throw-literal
51-
throw unknownError;
52-
});
57+
stub = jest.spyOn(tsnode, "register").mockImplementation(
58+
() =>
59+
({
60+
compile: (): string => {
61+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
62+
throw unknownError;
63+
},
64+
} as any)
65+
);
66+
loader = TypeScriptLoader();
5367
});
5468

5569
afterEach(() => {

lib/loader.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +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({
7+
...options,
8+
compilerOptions: { module: "commonjs" },
9+
});
610
return (path: string, content: string) => {
711
try {
812
// cosmiconfig requires the transpiled configuration to be CJS
9-
register({ ...options, compilerOptions: { module: "commonjs" } }).compile(
10-
content,
11-
path
12-
);
13+
tsNodeInstance.compile(content, path);
1314
const result = require(path);
1415

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

0 commit comments

Comments
 (0)