diff --git a/package.json b/package.json index 9e8b0561d..e1cb6349f 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "clean": "rimraf dist", "tsc": "tsc", "build": "npm run clean && npm run tsc", - "test-spec": "mocha dist/**/*.spec.js -R spec --bail", + "test-spec": "mocha --reporter mocha-reporter dist/**/*.spec.js --bail", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- \"dist/**/*.spec.js\" -R spec --bail", "test": "npm run build && npm run lint && npm run test-cov", "prepublish": "typings install && npm run build" @@ -70,6 +70,7 @@ "make-error": "^1.1.1", "minimist": "^1.2.0", "mkdirp": "^0.5.1", + "mocha-reporter": "0.0.3", "source-map-support": "^0.4.0", "tsconfig": "^7.0.0", "v8flags": "^3.0.0", diff --git a/src/bin.ts b/src/bin.ts index 2aa928fab..51b72388a 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -57,6 +57,11 @@ v8flags(function (err, v8flags) { { stdio: 'inherit' } ) + // Ignore the following signals; the child process will be receiving them, + // and should exit by itself + process.on('SIGINT', () => true) + process.on('SIGTERM', () => true) + proc.on('exit', function (code: number, signal: string) { process.on('exit', function () { if (signal) { diff --git a/src/index.spec.ts b/src/index.spec.ts index 839fb4f40..4f4e038cb 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'chai' -import { exec } from 'child_process' +import { exec, spawn } from 'child_process' import { join } from 'path' import semver = require('semver') import ts = require('typescript') @@ -20,6 +20,24 @@ describe('ts-node', function () { }) describe('cli', function () { + it('should not react to signals, but forward them to the child process', function (done) { + const proc = spawn('node', [ + EXEC_PATH, + './tests/signals.ts' + ]) + + proc.stdout.on('data', (data) => { + const message = data.toString() + expect(message).to.be.eq('exit') + done() + }) + + proc.stderr.on('data', (data) => console.log(data.toString())) + + proc.on('exit', (code) => expect(code).to.be.eq(null)) + setTimeout(() => proc.kill('SIGTERM'), 100) + }) + it('should execute cli', function (done) { exec(`${BIN_EXEC} tests/hello-world`, function (err, stdout) { expect(err).to.equal(null) diff --git a/tests/signals.ts b/tests/signals.ts new file mode 100644 index 000000000..c5473c4b0 --- /dev/null +++ b/tests/signals.ts @@ -0,0 +1,8 @@ +process.on('SIGINT', () => { + setTimeout(() => { + console.log('exited') + process.exit(0) + }, 500) +}) + +setInterval(() => console.log('should not be reached'), 2000)