From 519dcb1f704d1e7928a60afe781572650147263d Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Sat, 4 May 2024 14:43:27 +0800 Subject: [PATCH] chore: add test --- lib/index.js | 13 +++++++++++++ lib/process.js | 5 ++++- lib/util.js | 8 ++++++++ package.json | 4 ++-- test/index.test.js | 27 ++++++++++++--------------- test/util.js | 30 +++++++++++++++++++++++++----- 6 files changed, 64 insertions(+), 23 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7fa8069..268441c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,6 +10,7 @@ const { colors, debug, getRelativeDir, + triggerMessage, } = require('./util'); const path = require('path'); const { replaceTscAliasPaths } = require('tsc-alias'); @@ -20,6 +21,8 @@ function run() { const [runCmd, tscArgs, runArgs, isCleanDirectory] = parseArgs(process.argv); const cwd = process.cwd(); + debug(`main process running, pid = ${process.pid}`); + // 调试模式下 if (process.env.NODE_DEBUG === 'midway:debug') { tscArgs.push(['--preserveWatchOutput']); @@ -205,24 +208,34 @@ function run() { } console.log(''); } + triggerMessage('server-first-ready'); } else { output( `${colors.green('Node.js server')} ${colors.dim( 'restarted in' )} ${during} ms\n` ); + triggerMessage('server-ready'); } } ); } + debug('watch compile success first'); + triggerMessage('watch-compile-success-first'); }, onWatchCompileSuccess: fileChangedList => { + debug('watch compile success'); + triggerMessage('watch-compile-success'); restart(fileChangedList); }, onWatchCompileFail: () => { + debug('watch compile fail'); + triggerMessage('watch-compile-fail'); restart.clear(); }, onCompileSuccess: () => { + debug('compile success'); + triggerMessage('compile-success'); runAfterTsc(); }, }); diff --git a/lib/process.js b/lib/process.js index d77126c..1c2418a 100644 --- a/lib/process.js +++ b/lib/process.js @@ -1,5 +1,5 @@ const { fork, spawn } = require('child_process'); -const { filterFileChangedText } = require('./util'); +const { filterFileChangedText, debug } = require('./util'); // is windows const isWin = process.platform === 'win32'; @@ -21,6 +21,8 @@ const forkTsc = (tscArgs = [], options = {}) => { shell: isWin ? true : undefined, }); + debug(`fork tsc process, pid = ${child.pid}`); + let totalFileChangedList = []; child.stdout.on('data', data => { @@ -89,6 +91,7 @@ const forkRun = (runCmdPath, runArgs = [], options = {}) => { }, execArgv: process.execArgv.concat(['-r', 'source-map-support/register']), }); + debug(`fork run process, pid = ${runChild.pid}`); const onServerReady = async data => { try { if (data.title === 'server-ready') { diff --git a/lib/util.js b/lib/util.js index 8669b6a..2e461d0 100644 --- a/lib/util.js +++ b/lib/util.js @@ -280,3 +280,11 @@ exports.filterFileChangedText = function (data) { return [newData, fileChangedList]; }; + +exports.triggerMessage = function (message) { + if (process.send) { + process.send(message); + } else { + process.emit('message', message); + } +}; diff --git a/package.json b/package.json index b75aa99..bb6d802 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,8 @@ "tsc-watch" ], "scripts": { - "test": "jest --testTimeout=30000", - "cov": "jest --coverage" + "test": "jest --testTimeout=30000 --runInBand", + "cov": "jest --coverage --runInBand" }, "repository": { "type": "git", diff --git a/test/index.test.js b/test/index.test.js index 3ed9bcb..9106391 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -6,10 +6,10 @@ const { execa, sleep } = require('./util'); const mtscPath = join(__dirname, '../bin/mwtsc.js'); describe('/test/index.js', () => { - it('should throw error when no --run parameter', async () => { - await new Promise(resolve => { - const cp = execa('node', [mtscPath], {}); + it.skip('should throw error when no --run parameter', async () => { + const cp = await execa('node', [mtscPath], {}); + await new Promise(resolve => { cp.on('exit', code => { console.log('exit', code); resolve(); @@ -21,13 +21,13 @@ describe('/test/index.js', () => { }) }); - it('should compile ts file and run custom js', async () => { - await new Promise((resolve, reject) => { - const runPath = join(__dirname, 'fixtures/base'); - const cp = execa('node', [mtscPath, '--run', './run.js'], { - cwd: runPath, - }); + it('should compile ts file and ignore run script without watch args', async () => { + const runPath = join(__dirname, 'fixtures/base'); + const cp = await execa('node', [mtscPath, '--run', './run.js'], { + cwd: runPath, + }); + await new Promise((resolve, reject) => { cp.on('exit', code => { try { expect(existsSync(join(runPath, 'dist/a.js'))).toBeTruthy(); @@ -44,7 +44,6 @@ describe('/test/index.js', () => { }); it('should test ts file change and reload process', async () => { - // prepare const runPath = join(__dirname, 'fixtures/add_file'); const file = join(runPath, 'a.ts'); @@ -59,7 +58,7 @@ describe('/test/index.js', () => { } } - const cp = execa('node', [mtscPath, '--watch', '--run', './run.js'], { + const cp = await execa('node', [mtscPath, '--watch', '--run', './run.js'], { cwd: runPath, }); @@ -107,12 +106,10 @@ describe('/test/index.js', () => { } } - await sleep(500); - // add a error file writeFileSync(file, 'console.log("a)'); - const cp = execa('node', [mtscPath, '--watch', '--run', './run.js'], { + const cp = await execa('node', [mtscPath, '--watch', '--run', './run.js'], { cwd: runPath, }); @@ -138,7 +135,7 @@ describe('/test/index.js', () => { }); }); - it.skip('should send server-kill event to child process and receive response', (done) => { + it('should send server-kill event to child process and receive response', (done) => { const childProcess = forkRun(resolve(__dirname, './fixtures/custom-event.js')); childProcess.getRealChild().on('message', (data) => { if (data === 'server-kill-complete') { diff --git a/test/util.js b/test/util.js index c7981f5..697fb5c 100644 --- a/test/util.js +++ b/test/util.js @@ -1,11 +1,31 @@ const cp = require('child_process'); function execa(cmd, args, options) { - // mock execa - return cp.spawn(cmd, args, Object.assign({ - cwd: __dirname, - stdio: 'ignore', - }, options)); + return new Promise((resolve, reject) => { + // mock execa + const child = cp.spawn(cmd, args, Object.assign({ + cwd: __dirname, + stdio: ['inherit', 'inherit', 'inherit', 'ipc'], + }, options)); + + child.on('message', (data) => { + if (args.includes('--watch')) { + if (data === 'watch-compile-success-first' || data === 'watch-compile-fail') { + resolve(child); + } else { + console.log('got event:', data); + } + } else { + if (data === 'compile-success') { + resolve(child); + } else { + console.log('got event:', data); + } + } + }); + }); + + return child; } function sleep(ms) {