Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ describe("TypeScriptLoader", () => {
const fixturesPath = path.resolve(__dirname, "__fixtures__");

let loader: Loader;
let tsNodeSpy = jest.spyOn(tsnode, "register");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is constant, and only used in one test so can be moved to the test itself:

  it("should use the same instance of ts-node across multiple calls", () => {
    const tsNodeSpy = jest.spyOn(tsnode, "register");


function readFixtureContent(file: string): string {
return fs.readFileSync(file).toString();
}

beforeEach(() => {
beforeAll(() => {
loader = TypeScriptLoader();
});

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

it("should use the same instance of ts-node across multiple calls", () => {
const filePath = path.resolve(fixturesPath, "valid.fixture.ts");
loader(filePath, readFixtureContent(filePath));
loader(filePath, readFixtureContent(filePath));
loader(filePath, readFixtureContent(filePath));
expect(tsNodeSpy).toHaveBeenCalledTimes(1);
});

it("should throw a TypeScriptCompileError on error", () => {
try {
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts");
Expand Down
9 changes: 5 additions & 4 deletions lib/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { register, RegisterOptions } from "ts-node";
import { TypeScriptCompileError } from "./typescript-compile-error";

export function TypeScriptLoader(options?: RegisterOptions): Loader {
const tsNodeInstance = register({
...options,
compilerOptions: { module: "commonjs" },
});
return (path: string, content: string) => {
try {
// cosmiconfig requires the transpiled configuration to be CJS
register({ ...options, compilerOptions: { module: "commonjs" } }).compile(
content,
path
);
tsNodeInstance.compile(content, path);
const result = require(path);

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