Skip to content

Commit b05dffd

Browse files
committed
add error code, not found tests for future
1 parent 08fc620 commit b05dffd

File tree

7 files changed

+72
-18
lines changed

7 files changed

+72
-18
lines changed

lib/child-require-hook.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var checkFileScript = join(__dirname, 'check-file-exists.js')
2222
var waitForFile = function(fileName) {
2323
var start = new Date().getTime()
2424
while (true) {
25-
const exists = execCheck
25+
var exists = execCheck
2626
? execSync(['node', checkFileScript, '"' + fileName + '"'].join(' '), {
2727
stdio: 'inherit'
2828
}) || true
@@ -139,7 +139,7 @@ if (readyFile) {
139139
}
140140

141141
if (exitChild) {
142-
process.on('SIGTERM', function() {
142+
process.on('SIGTERM', function() {
143143
console.log('Child got SIGTERM, exiting.')
144144
process.exit()
145145
})

lib/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var tsVersion = require('typescript').version
99
var kill = require('tree-kill')
1010
var readline = require('readline')
1111

12-
1312
module.exports = function (script, scriptArgs, nodeArgs, opts) {
1413
if (typeof script !== 'string' || script.length === 0) {
1514
throw new TypeError('`script` must be a string')
@@ -171,9 +170,9 @@ module.exports = function (script, scriptArgs, nodeArgs, opts) {
171170
})
172171

173172
// Upon errors, display a notification and tell the child to exit.
174-
ipc.on(child, 'error', function (m) {
173+
ipc.on(child, 'error', function (m) {
175174
log.debug('Child error')
176-
notify(m.error, m.message, 'error')
175+
notify(m.error, m.message, 'error')
177176
stop(m.willTerminate)
178177
})
179178
compiler.writeReadyFile()
@@ -184,19 +183,19 @@ module.exports = function (script, scriptArgs, nodeArgs, opts) {
184183
if (opts['tree-kill']) {
185184
log.debug('Using tree-kill')
186185
kill(child.pid)
187-
} else {
186+
} else {
188187
child.kill('SIGTERM')
189188
}
190189
}
191190
function stop(willTerminate) {
192191
if (!child || child.stopping) {
193192
return
194-
}
193+
}
195194
child.stopping = true
196195
child.respawn = true
197196
if (child.connected === undefined || child.connected === true) {
198197
log.debug('Disconnecting from child')
199-
child.disconnect()
198+
child.disconnect()
200199
//if (!willTerminate) {
201200
killChild()
202201
//}

lib/wrap.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ var resolve = require('resolve').sync
55
var hook = require('./hook')
66
var ipc = require('./ipc')
77
var resolveMain = require('./resolveMain')
8-
8+
// var Module = require('module')
99
// Remove wrap.js from the argv array
10+
1011
process.argv.splice(1, 1)
1112

1213
// Resolve the location of the main script relative to cwd
@@ -36,6 +37,13 @@ if (cfg.fork) {
3637
}
3738
}
3839

40+
// var lastRequired = null
41+
// var origRequire = Module.prototype.require
42+
// Module.prototype.require = function (requirePath) {
43+
// lastRequired = { path: requirePath, filename: this.filename }
44+
// return origRequire.apply(this, arguments)
45+
// }
46+
3947
// Error handler that displays a notification and logs the stack to stderr:
4048
var caught = false
4149
process.on('uncaughtException', function (err) {
@@ -48,11 +56,13 @@ process.on('uncaughtException', function (err) {
4856
var hasCustomHandler = process.listeners('uncaughtException').length > 1
4957
var isTsError = err && err.message && /TypeScript/.test(err.message)
5058
if (!hasCustomHandler && !isTsError) {
51-
console.error((err && err.stack) || err)
59+
//console.error((err && err.stack) || err)
5260
}
5361
ipc.send({
54-
error: isTsError ? '' : err && err.name || 'Error',
62+
error: isTsError ? '' : (err && err.name) || 'Error',
63+
// lastRequired: lastRequired,
5564
message: err ? err.message : '',
65+
code: err && err.code,
5666
willTerminate: hasCustomHandler,
5767
})
5868
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./not-found-js')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { fn } from './not-found-js'
2+
3+
console.log(fn(1))

test/index.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ test('It should --prefer-ts', async (t) => {
198198
//`--prefer-ts-exts`,
199199
`prefer/prefer.js`,
200200
].join(' ')
201-
)//.turnOnOutput()
201+
) //.turnOnOutput()
202202
await ps.waitForLine(/PREFER DEP JS/)
203203
await ps.waitForLine(/PREFER JS/)
204204
await ps.exit()
@@ -211,13 +211,13 @@ test('It should --prefer-ts', async (t) => {
211211
//`--prefer-ts`,
212212
`prefer/prefer`,
213213
].join(' ')
214-
)//.turnOnOutput()
214+
) //.turnOnOutput()
215215
await ps.waitForLine(/PREFER DEP JS/)
216216
await ps.waitForLine(/PREFER TS/)
217217
await ps.exit()
218218
t.pass()
219219
})
220-
220+
221221
t.test('Use require all TS with --ts-prefer', async (t) => {
222222
const ps = spawnTsNodeDev(
223223
[
@@ -226,16 +226,55 @@ test('It should --prefer-ts', async (t) => {
226226
//'--debug',
227227
`prefer/prefer`,
228228
].join(' ')
229-
)//.turnOnOutput()
229+
) //.turnOnOutput()
230230
await ps.waitForLine(/PREFER DEP TS/)
231231
await ps.waitForLine(/PREFER TS/)
232232

233233
setTimeout(() => replaceText('prefer/prefer-dep.ts', 'DEP', 'DEP MOD'), 250)
234234

235-
await ps.waitForLine(/PREFER DEP MOD TS/)
235+
await ps.waitForLine(/PREFER DEP MOD TS/)
236236

237237
await ps.exit()
238238
t.pass()
239239
replaceText('prefer/prefer-dep.ts', 'DEP MOD', 'DEP')
240240
})
241241
})
242+
243+
// maybe later
244+
test.skip('Can not find module', async (t) => {
245+
const foundText = 'FOUND NOW!'
246+
247+
const createNotFound = () =>
248+
setTimeout(
249+
() => writeFile('not-found/not-found.js', `console.log('${foundText}')`),
250+
100
251+
)
252+
const removeNotFound = () => removeFile('not-found/not-found.js')
253+
254+
// t.test('Not found in TS', async () => {
255+
// const ps = spawnTsNodeDev(
256+
// [`--respawn`, 'not-found/with-not-found-js'].join(' ')
257+
// ).turnOnOutput()
258+
259+
// await ps.waitForLine('Cannot find module')
260+
261+
// createNotFound()
262+
263+
// require.resolve('')
264+
265+
// await ps.waitForLine('Restarting')
266+
267+
// //await removeNotFound()
268+
// })
269+
270+
t.test('Not found in JS', async () => {
271+
const ps = spawnTsNodeDev(
272+
['not-found/js-with-not-found.js'].join(' ')
273+
).turnOnOutput()
274+
275+
await ps.waitForLine('Cannot find module')
276+
277+
278+
//await removeNotFound()
279+
})
280+
})

test/spawn.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const spawnTsNodeDev = (
2020
})
2121
var out = ''
2222
var err = ''
23-
23+
2424
ps.stderr.on('data', function (data) {
2525
if (opts.stderr) {
2626
console.log('STDERR:', data.toString())
@@ -33,7 +33,9 @@ export const spawnTsNodeDev = (
3333
}
3434
out += data.toString()
3535
})
36-
36+
ps.on('disconnect', () => {
37+
console.log('im out')
38+
})
3739
const testPattern = (pattern: string | RegExp, str: string) => {
3840
return typeof pattern === 'string'
3941
? str.indexOf(pattern) >= 0

0 commit comments

Comments
 (0)