Skip to content

Commit 64a8cfb

Browse files
committed
Add regression test
1 parent 8cde25b commit 64a8cfb

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/esm.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,39 @@ const { defaultGetFormat } = require('../dist-raw/node-esm-default-get-format');
3636
// from node, build our implementation of the *new* API on top of it, and implement the *old*
3737
// hooks API as a shim to the *new* API.
3838

39+
export interface NodeHooksAPI1 {
40+
resolve(
41+
specifier: string,
42+
context: { parentURL: string },
43+
defaultResolve: NodeHooksAPI1['resolve']
44+
): Promise<{ url: string }>;
45+
getFormat(
46+
url: string,
47+
context: {},
48+
defaultGetFormat: NodeHooksAPI1['getFormat']
49+
): Promise<{ format: Format }>;
50+
transformSource(
51+
source: string | Buffer,
52+
context: { url: string; format: Format },
53+
defaultTransformSource: NodeHooksAPI1['transformSource']
54+
): Promise<{ source: string | Buffer }>;
55+
}
56+
57+
export interface NodeHooksAPI2 {
58+
resolve(
59+
specifier: string,
60+
context: { parentURL: string },
61+
defaultResolve: NodeHooksAPI2['resolve']
62+
): Promise<{ url: string }>;
63+
load(
64+
url: string,
65+
context: { format: Format | null | undefined },
66+
defaultLoad: NodeHooksAPI2['load']
67+
): Promise<{ format: Format; source: string | Buffer | undefined }>;
68+
}
69+
70+
type Format = 'builtin' | 'commonjs' | 'dynamic' | 'json' | 'module' | 'wasm';
71+
3972
/** @internal */
4073
export function registerAndCreateEsmHooks(opts?: RegisterOptions) {
4174
// Automatically performs registration just like `-r ts-node/register`
@@ -62,12 +95,7 @@ export function createEsmHooks(tsNodeService: Service) {
6295
versionGteLt(process.versions.node, '12.999.999', '13.0.0');
6396

6497
// Explicit return type to avoid TS's non-ideal inferred type
65-
const hooksAPI: {
66-
resolve: typeof resolve;
67-
getFormat: typeof getFormat | undefined;
68-
transformSource: typeof transformSource | undefined;
69-
load: typeof load | undefined;
70-
} = newHooksAPI
98+
const hooksAPI: NodeHooksAPI1 | NodeHooksAPI2 = newHooksAPI
7199
? { resolve, load, getFormat: undefined, transformSource: undefined }
72100
: { resolve, getFormat, transformSource, load: undefined };
73101
return hooksAPI;
@@ -160,7 +188,6 @@ export function createEsmHooks(tsNodeService: Service) {
160188
return { format, source };
161189
}
162190

163-
type Format = 'builtin' | 'commonjs' | 'dynamic' | 'json' | 'module' | 'wasm';
164191
async function getFormat(
165192
url: string,
166193
context: {},

src/test/esm-loader.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import semver = require('semver');
77
import {
88
contextTsNodeUnderTest,
99
EXPERIMENTAL_MODULES_FLAG,
10+
resetNodeEnvironment,
1011
TEST_DIR,
1112
} from './helpers';
1213
import { createExec } from './exec-helpers';
1314
import { join } from 'path';
1415
import * as expect from 'expect';
16+
import type { NodeHooksAPI2 } from '../esm';
17+
18+
const nodeUsesNewHooksApi = semver.gte(process.version, '16.12.0');
1519

1620
const test = context(contextTsNodeUnderTest);
1721

@@ -37,3 +41,27 @@ test.suite('createEsmHooks', (test) => {
3741
});
3842
}
3943
});
44+
45+
test.suite('hooks', (_test) => {
46+
const test = _test.context(async t => {
47+
const service = t.context.tsNodeUnderTest.create();
48+
t.teardown(() => {
49+
resetNodeEnvironment();
50+
});
51+
return {
52+
service,
53+
hooks: t.context.tsNodeUnderTest.createEsmHooks(service)
54+
};
55+
});
56+
57+
if(nodeUsesNewHooksApi) {
58+
test('Correctly determines format of data URIs', async (t) => {
59+
const {hooks} = t.context;
60+
const url = 'data:text/javascript,console.log("hello world");'
61+
const result = await (hooks as NodeHooksAPI2).load(url, {format: undefined}, async (url, context, _ignored) => {
62+
return {format: context.format!, source: ''};
63+
});
64+
expect(result.format).toBe('module');
65+
});
66+
}
67+
});

0 commit comments

Comments
 (0)