This repository was archived by the owner on Dec 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtsnd.test.ts
More file actions
375 lines (319 loc) · 11.1 KB
/
tsnd.test.ts
File metadata and controls
375 lines (319 loc) · 11.1 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* eslint-disable @typescript-eslint/no-misused-promises */
import { describe, it } from 'mocha'
import chai from 'chai'
import { spawnTsNodeDev, scriptsDir, tmpDir, turnOnOutput } from './spawn'
import fs from 'fs-extra'
import { join } from 'path'
import touch = require('touch')
if (process.argv.slice(2).includes('--output')) {
turnOnOutput()
}
const { assert: t } = chai
export const replaceText = async (
script: string,
pattern: string | RegExp,
replace: string
) => {
const textFile = join(scriptsDir, script)
const text = await fs.readFile(textFile, 'utf-8')
return fs.writeFile(textFile, text.replace(pattern, replace))
}
export const writeFile = async (script: string, text: string) => {
const textFile = join(scriptsDir, script)
return fs.writeFile(textFile, text)
}
export const removeFile = async (script: string) => {
const textFile = join(scriptsDir, script)
return fs.remove(textFile)
}
const waitFor = (timeout: number) => {
return new Promise((resolve) => setTimeout(resolve, timeout))
}
fs.ensureDirSync(tmpDir)
fs.removeSync(join(tmpDir, 'fixture'))
fs.copySync(join(__dirname, 'fixture'), scriptsDir)
describe('ts-node-dev', function () {
this.timeout(5000)
it('should restart on file change', async () => {
const ps = spawnTsNodeDev('--respawn --poll simple.ts')
await ps.waitForLine(/v1/)
setTimeout(() => replaceText('dep.ts', 'v1', 'v2'), 250)
await ps.waitForLine(/v2/)
t.ok(true, 'Changed code version applied.')
await ps.exit()
// revert
await replaceText('dep.ts', 'v2', 'v1')
})
it('allow watch arbitrary folder/file', async () => {
const ps = spawnTsNodeDev('--respawn --watch folder,folder2 simple.ts')
await ps.waitForLine(/v1/)
setTimeout(() => touch(join(scriptsDir, 'folder/some-file')), 250)
await ps.waitForLine(/Restarting.*some-file/)
t.ok(true, 'works')
await ps.exit()
})
it('should report an error on start', async () => {
const ps = spawnTsNodeDev('--respawn with-error.ts')
await ps.waitForLine(/\[ERROR\]/)
const out = ps.getStdout()
const err = ps.getStderr()
t.ok(/Compilation error in/.test(err), 'Reports error file')
t.ok(/[ERROR].*Unable to compile TypeScript/.test(out), 'Report TS error')
t.ok(/Argument of type/.test(out), 'Report TS error diagnostics')
setTimeout(() => replaceText('with-error.ts', `'1'`, '1'), 250)
// PROBLEM: if we try to fix not required/compiled dep it does not work.
// setTimeout(() => replaceText('dep-ts-error.ts', 'number', 'string'), 250)
await ps.waitForLine(/v1/)
t.ok(true, 'Restarted successfully after error fixed.')
await ps.exit()
await replaceText('with-error.ts', '1', `'1'`)
})
it('should not output INFO messages with --quiet', async () => {
const ps = spawnTsNodeDev('--respawn --poll --quiet simple.ts')
await ps.waitForLine(/v1/)
setTimeout(() => replaceText('dep.ts', 'v1', 'v2'), 250)
await ps.waitForLine(/v2/)
await ps.exit()
t.equal(ps.getStdout(), ['v1', 'v2', ''].join('\n'))
await replaceText('dep.ts', 'v2', 'v1')
})
it('should report an error with --log-error and continue to work', async () => {
const ps = spawnTsNodeDev('--respawn --log-error with-error.ts')
await ps.waitForErrorLine(/error/)
const out = ps.getStderr()
t.ok(/error.*Argument of type/.test(out), 'Reports error in stderr')
setTimeout(() => replaceText('with-error.ts', `'1'`, '1'), 250)
await ps.waitForLine(/Restarting:/)
await ps.waitForLine(/v1/)
t.ok(true, 'Restarted successfully after error fixed.')
await ps.exit()
await replaceText('with-error.ts', '1', `'1'`)
})
it('should restart on adding not imported module', async () => {
const ps = spawnTsNodeDev('--respawn --error-recompile with-error.ts', {
env: {
TS_NODE_DEV_ERROR_RECOMPILE_TIMEOUT: 50,
},
})
await ps.waitForLine(/[ERROR]/)
setTimeout(() => replaceText('dep-ts-error.ts', 'number', 'string'), 250)
await ps.waitForLine(/v1/)
t.ok(true, 'Restarted successfully after error fixed.')
await ps.exit()
await replaceText('dep-ts-error.ts', 'string', 'number')
})
it('should recompile module on error and restarts', async () => {
const notFoundSource = `export const fn = (x: number) => {
return 'v1'
}
`
const ps = spawnTsNodeDev('--respawn --error-recompile with-not-found.ts', {
env: {
TS_NODE_DEV_ERROR_RECOMPILE_TIMEOUT: 20,
},
})
await ps.waitForLine(/[ERROR]/)
setTimeout(() => writeFile('not-found.ts', notFoundSource), 250)
await ps.waitForLine(/v1/)
t.ok(true, 'Restarted successfully after module was created.')
await ps.exit()
await removeFile('not-found.ts')
})
it('should handle allowJs option and compile JS modules', async () => {
const cOptions = { allowJs: true, esModuleInterop: false }
const ps = spawnTsNodeDev(
[
`--respawn`,
`--compiler-options=${JSON.stringify(cOptions)}`,
`js-module.js`,
].join(' ')
)
await ps.waitForLine(/JS MODULE/)
t.ok(true, 'ok')
await ps.exit()
})
it('should handle -r esm option and load JS modules', async () => {
const ps = spawnTsNodeDev([`--respawn`, `-r esm`, `js-module.js`].join(' '))
await ps.waitForLine(/JS MODULE/)
t.ok(true, 'ok')
await ps.exit()
})
it('should handle resolveJsonModule option and load JSON modules', async () => {
const cOptions = { resolveJsonModule: true }
const ps = spawnTsNodeDev(
[
`--respawn`,
`--compiler ttypescript`,
`--compiler-options=${JSON.stringify(cOptions)}`,
`import-json`,
].join(' ')
)
await ps.waitForLine(/JSON DATA: { file: 'json' }/)
t.ok(true, 'ok')
await ps.exit()
})
describe('--dir and --script-mode flags', () => {
it('should not allow --script-mode and --dir together', async () => {
const ps = spawnTsNodeDev(
[`--script-mode`, `--dir folder`, `simple.ts`].join(' ')
)
await ps.waitForErrorLine(/Script mode cannot be combined with `--dir`/)
t.ok(true, 'ok')
await ps.exit()
})
it('should use the tsconfig at --dir when defined', async () => {
const ps = spawnTsNodeDev(
[`--dir dir-test`, `dir-test/index.ts`].join(' ')
)
await ps.waitForLine(/\{ hello: 'world' \}/)
t.ok(true, 'ok')
await ps.exit()
})
it('should use the tsconfig at --script-mode when defined', async () => {
const ps = spawnTsNodeDev([`-s`, `dir-test/index.ts`].join(' '))
await ps.waitForLine(/\{ hello: 'world' \}/)
t.ok(true, 'ok')
await ps.exit()
})
it('should fail if not using --dir or --script-mode on dir-test/index.ts', async () => {
const cOptions = { allowJs: true, esModuleInterop: false }
const ps = spawnTsNodeDev(
[
`--compiler-options=${JSON.stringify(cOptions)}`,
`dir-test/index.ts`,
].join(' ')
)
await ps.waitForLine(/has no default export./)
t.ok(true, 'ok')
await ps.exit()
})
})
it('should allow to use custom TS transformers', async () => {
const cOptions = { plugins: [{ transform: 'ts-nameof', type: 'raw' }] }
const ps = spawnTsNodeDev(
[
`--respawn`,
`--compiler ttypescript`,
`--compiler-options=${JSON.stringify(cOptions)}`,
`nameof.ts`,
].join(' ')
)
await ps.waitForLine(/console/)
await ps.exit()
})
it('It allows to use custom TS Transformers', async () => {
const cOptions = { plugins: [{ transform: __dirname + '/transformer.ts' }] }
const ps = spawnTsNodeDev(
[
`--respawn`,
`--compiler ttypescript`,
`--compiler-options=${JSON.stringify(cOptions)}`,
`to-transform.ts`,
].join(' ')
)
await ps.waitForLine(/transformed/)
t.ok(true, 'ok')
await ps.exit()
})
describe('--prefer-ts-exts flag', async () => {
it('should require existing JS modules by default', async () => {
const ps = spawnTsNodeDev([`--respawn`, `prefer/prefer`].join(' '))
await ps.waitForLine(/PREFER DEP JS/)
await ps.waitForLine(/PREFER TS/)
await ps.exit()
t.ok(true)
})
it('should require TS modules with --ts-prefer-exts', async () => {
const ps = spawnTsNodeDev(
[`--respawn`, `--prefer-ts-exts`, `prefer/prefer`].join(' ')
)
await ps.waitForLine(/PREFER DEP TS/)
await ps.waitForLine(/PREFER TS/)
setTimeout(
() => replaceText('prefer/prefer-dep.ts', 'DEP', 'DEP MOD'),
250
)
await ps.waitForLine(/PREFER DEP MOD TS/)
await ps.exit()
t.ok(true)
await replaceText('prefer/prefer-dep.ts', 'DEP MOD', 'DEP')
})
})
// watching required with -r not implemented
it.skip('should add require with -r flag', async () => {
const ps = spawnTsNodeDev(
[
`-r ./add-req`,
//`--debug`,
`simple`,
].join(' ')
)
await ps.waitForLine(/added --require/)
await ps.waitForLine(/v1/)
//setTimeout(() => replaceText('add-req', 'added', 'changed'), 250)
//await ps.exit()
//
await ps.waitForLine(/changed --require/)
t.ok(true)
})
it('should handle --deps flag', async () => {
const ps = spawnTsNodeDev([`--deps`, `--respawn`, `req-package`].join(' '))
await ps.waitForLine(/PACKAGE/)
setTimeout(
() =>
replaceText(
'node_modules/package/index.js',
'PACKAGE',
'CHANGED PACKAGE'
),
100
)
await ps.waitForLine(/CHANGED PACKAGE/)
await ps.exit()
t.ok(true)
})
it('should handle deep deps with --deps flag', async () => {
const ps = spawnTsNodeDev(
[`--all-deps`, `--respawn`, `req-package`].join(' ')
)
await ps.waitForLine(/PACKAGE/)
setTimeout(
() =>
replaceText(
'node_modules/package/node_modules/level2-package/index.js',
'PACKAGE',
'CHANGED PACKAGE'
),
100
)
await ps.waitForLine(/CHANGED PACKAGE/)
await ps.exit()
t.ok(true)
})
it.skip('should error on wrong cli flag', async () => {
const ps = spawnTsNodeDev([`--transpileOnly`, `req-package`].join(' '))
await ps.waitForLine(/bad option/)
await ps.waitForLine(/CHANGED PACKAGE/)
await ps.exit()
t.ok(true)
})
it('should put compiled sources in custom --cache-directory', async () => {
const cacheDir = join(tmpDir, 'test-cached-dir')
fs.removeSync(cacheDir)
const ps = spawnTsNodeDev(`--cache-directory ${cacheDir} simple.ts`)
await ps.waitForLine(/v1/)
await ps.exit()
const list = fs.readdirSync(cacheDir)
t.ok(list[0] === 'compiled', '"compiled" dir is there')
})
it('should handle --file-change-hook flag', async () => {
writeFile('test.ts', 'a')
const ps = spawnTsNodeDev([`--file-change-hook`, `fileChangeHook.js`, `--respawn`, `--watch`, `test.ts`, `simple.ts`].join(' '))
await ps.waitForLine(/v1/)
writeFile('test.ts', 'b')
await ps.waitForLine(/Restarting.*test.ts/)
t.ok(true, 'works')
await ps.exit()
await removeFile('test.ts')
})
})