-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
register.spec.ts
206 lines (168 loc) · 5.57 KB
/
register.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { once } from 'lodash';
import {
ctxTsNode,
PROJECT_TRANSPILE_ONLY,
resetNodeEnvironment,
TEST_DIR,
tsNodeTypes,
} from './helpers';
import { context, expect } from './testlib';
import * as exp from 'expect';
import { join, resolve } from 'path';
import proxyquire = require('proxyquire');
const SOURCE_MAP_REGEXP =
/\/\/# sourceMappingURL=data:application\/json;charset=utf\-8;base64,[\w\+]+=*$/;
const createOptions: tsNodeTypes.CreateOptions = {
project: PROJECT_TRANSPILE_ONLY,
compilerOptions: {
jsx: 'preserve',
},
};
const test = context(ctxTsNode).context(
once(async (t) => {
return {
moduleTestPath: resolve(__dirname, '../../tests/module.ts'),
service: t.context.tsNodeUnderTest.create(createOptions),
};
})
);
test.beforeEach(async (t) => {
// Un-install all hook and remove our test module from cache
resetNodeEnvironment();
delete require.cache[t.context.moduleTestPath];
// Paranoid check that we are truly uninstalled
exp(() => require(t.context.moduleTestPath)).toThrow(
"Unexpected token 'export'"
);
});
test.runSerially();
test('create() does not register()', async (t) => {
// nyc sets its own `require.extensions` hooks; to truly detect if we're
// installed we must attempt to load a TS file
t.context.tsNodeUnderTest.create(createOptions);
// This error indicates node attempted to run the code as .js
exp(() => require(t.context.moduleTestPath)).toThrow(
"Unexpected token 'export'"
);
});
test('register(options) is shorthand for register(create(options))', (t) => {
t.context.tsNodeUnderTest.register(createOptions);
require(t.context.moduleTestPath);
});
test('register(service) registers a previously-created service', (t) => {
t.context.tsNodeUnderTest.register(t.context.service);
require(t.context.moduleTestPath);
});
test.suite('register(create(options))', (test) => {
test.beforeEach(async (t) => {
// Re-enable project for every test.
t.context.service.enabled(true);
t.context.tsNodeUnderTest.register(t.context.service);
t.context.service.installSourceMapSupport();
});
test('should be able to require typescript', ({
context: { moduleTestPath },
}) => {
const m = require(moduleTestPath);
expect(m.example('foo')).toBe('FOO');
});
test('should support dynamically disabling', ({
context: { service, moduleTestPath },
}) => {
delete require.cache[moduleTestPath];
expect(service.enabled(false)).toBe(false);
expect(() => require(moduleTestPath)).toThrow(/Unexpected token/);
delete require.cache[moduleTestPath];
expect(service.enabled()).toBe(false);
expect(() => require(moduleTestPath)).toThrow(/Unexpected token/);
delete require.cache[moduleTestPath];
expect(service.enabled(true)).toBe(true);
expect(() => require(moduleTestPath)).not.toThrow();
delete require.cache[moduleTestPath];
expect(service.enabled()).toBe(true);
expect(() => require(moduleTestPath)).not.toThrow();
});
test('should compile through js and ts', () => {
const m = require('../../tests/complex');
expect(m.example()).toBe('example');
});
test('should work with proxyquire', () => {
const m = proxyquire('../../tests/complex', {
'./example': 'hello',
});
expect(m.example()).toBe('hello');
});
test('should work with `require.cache`', () => {
const { example1, example2 } = require('../../tests/require-cache');
expect(example1).not.toBe(example2);
});
test('should use source maps', async () => {
try {
require('../../tests/throw error');
} catch (error: any) {
exp(error.stack).toMatch(
[
'Error: this is a demo',
` at Foo.bar (${join(TEST_DIR, './throw error.ts')}:100:17)`,
].join('\n')
);
}
});
test.suite('JSX preserve', (test) => {
let compiled: string;
test.beforeAll(async () => {
const old = require.extensions['.tsx']!;
require.extensions['.tsx'] = (m: any, fileName) => {
const _compile = m._compile;
m._compile = function (code: string, fileName: string) {
compiled = code;
return _compile.call(this, code, fileName);
};
return old(m, fileName);
};
});
test('should use source maps', async (t) => {
try {
require('../../tests/with-jsx.tsx');
} catch (error: any) {
expect(error.stack).toMatch('SyntaxError: Unexpected token');
}
expect(compiled).toMatch(SOURCE_MAP_REGEXP);
});
});
});
test('should support compiler scopes w/multiple registered compiler services at once', (t) => {
const { moduleTestPath, tsNodeUnderTest } = t.context;
const calls: string[] = [];
const compilers = [
tsNodeUnderTest.register({
projectSearchDir: join(TEST_DIR, 'scope/a'),
scopeDir: join(TEST_DIR, 'scope/a'),
scope: true,
}),
tsNodeUnderTest.register({
projectSearchDir: join(TEST_DIR, 'scope/a'),
scopeDir: join(TEST_DIR, 'scope/b'),
scope: true,
}),
];
compilers.forEach((c) => {
const old = c.compile;
c.compile = (code, fileName, lineOffset) => {
calls.push(fileName);
return old(code, fileName, lineOffset);
};
});
try {
expect(require('../../tests/scope/a').ext).toBe('.ts');
expect(require('../../tests/scope/b').ext).toBe('.ts');
} finally {
compilers.forEach((c) => c.enabled(false));
}
expect(calls).toEqual([
join(TEST_DIR, 'scope/a/index.ts'),
join(TEST_DIR, 'scope/b/index.ts'),
]);
delete require.cache[moduleTestPath];
expect(() => require(moduleTestPath)).toThrow();
});