/
esm-loader.spec.ts
71 lines (62 loc) · 1.95 KB
/
esm-loader.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// ESM loader hook tests
// TODO: at the time of writing, other ESM loader hook tests have not been moved into this file.
// Should consolidate them here.
import { context } from './testlib';
import semver = require('semver');
import {
contextTsNodeUnderTest,
EXPERIMENTAL_MODULES_FLAG,
resetNodeEnvironment,
TEST_DIR,
} from './helpers';
import { createExec } from './exec-helpers';
import { join } from 'path';
import * as expect from 'expect';
import type { NodeHooksAPI2 } from '../esm';
const nodeUsesNewHooksApi = semver.gte(process.version, '16.12.0');
const test = context(contextTsNodeUnderTest);
const exec = createExec({
cwd: TEST_DIR,
});
test.suite('createEsmHooks', (test) => {
if (semver.gte(process.version, '12.16.0')) {
test('should create proper hooks with provided instance', async () => {
const { err } = await exec(
`node ${EXPERIMENTAL_MODULES_FLAG} --loader ./loader.mjs index.ts`,
{
cwd: join(TEST_DIR, './esm-custom-loader'),
}
);
if (err === null) {
throw new Error('Command was expected to fail, but it succeeded.');
}
expect(err.message).toMatch(/TS6133:\s+'unusedVar'/);
});
}
});
test.suite('hooks', (_test) => {
const test = _test.context(async (t) => {
const service = t.context.tsNodeUnderTest.create();
t.teardown(() => {
resetNodeEnvironment();
});
return {
service,
hooks: t.context.tsNodeUnderTest.createEsmHooks(service),
};
});
if (nodeUsesNewHooksApi) {
test('Correctly determines format of data URIs', async (t) => {
const { hooks } = t.context;
const url = 'data:text/javascript,console.log("hello world");';
const result = await (hooks as NodeHooksAPI2).load(
url,
{ format: undefined },
async (url, context, _ignored) => {
return { format: context.format!, source: '' };
}
);
expect(result.format).toBe('module');
});
}
});