Skip to content

Commit

Permalink
chore: Add tests to confirm the number of calls ro register
Browse files Browse the repository at this point in the history
  • Loading branch information
alanquigley-toast committed Apr 22, 2022
1 parent 266acda commit f0e7dd0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 10 additions & 1 deletion lib/loader.spec.ts
Expand Up @@ -9,12 +9,13 @@ describe("TypeScriptLoader", () => {
const fixturesPath = path.resolve(__dirname, "__fixtures__");

let loader: Loader;
let 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
10 changes: 5 additions & 5 deletions lib/loader.ts
Expand Up @@ -3,14 +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" } })
const tsNodeInstance = register({
...options,
compilerOptions: { module: "commonjs" },
});
return (path: string, content: string) => {
try {
// cosmiconfig requires the transpiled configuration to be CJS
tsNodeInstance.compile(
content,
path
);
tsNodeInstance.compile(content, path);
const result = require(path);

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

0 comments on commit f0e7dd0

Please sign in to comment.