Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed May 3, 2024
1 parent 9261c74 commit fc56276
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ temp/
dist/
pnpm-lock.yaml
yarn.lock
package-lock.json
package-lock.json
test/fixtures/add_file/a.ts
3 changes: 2 additions & 1 deletion lib/wrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 拿到执行路径,以及执行文件
const childPath = process.env.CHILD_CMD_PATH;
const childCwd = process.env.CHILD_CWD;
const { join } = require('path');

process.on('message', data => {
if (data.title === 'server-kill') {
Expand All @@ -9,4 +10,4 @@ process.on('message', data => {
});

process.chdir(childCwd);
require(childPath);
require(join(childCwd, childPath));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"execa": "^5.1.1",
"jest": "^29.7.0",
"mwts": "^1.3.0",
"typescript": "~4.9.5"
"typescript": "^5.4.5"
},
"engines": {
"node": ">=12.0.0"
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/add_file/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('b')
2 changes: 2 additions & 0 deletions test/fixtures/add_file/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('abc')
process.send({ type: 'ready' });
21 changes: 21 additions & 0 deletions test/fixtures/add_file/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "es2018", // target es2018
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"inlineSourceMap":true,
"noImplicitThis": true,
"noUnusedLocals": true,
"stripInternal": true,
"skipLibCheck": true,
"pretty": true,
"outDir": "dist"
},
"exclude": [
"dist",
"node_modules",
"test"
]
}
39 changes: 38 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const execa = require('execa');
const { join, resolve } = require('path');
const { existsSync, unlinkSync } = require('fs');
const { existsSync, unlinkSync, writeFileSync, readFileSync } = require('fs');
const { forkRun } = require('../lib/process');

const mtscPath = join(__dirname, '../bin/mwtsc.js');
Expand Down Expand Up @@ -81,4 +81,41 @@ 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');
if (existsSync(file)) {
unlinkSync(file);
}

const cp = execa('node', [mtscPath, '--watch', '--run', './run.js'], {
cwd: runPath,
// stdio: 'ignore',
});

// add a new file
writeFileSync(file, 'console.log("a")');

// change file
writeFileSync(file, 'console.log("b")');

await new Promise((resolve, reject) => {
cp.on('exit', code => {
try {
expect(existsSync(join(runPath, 'dist/a.js'))).toBeTruthy();
expect(readFileSync(join(runPath, 'dist/a.js'), 'utf-8')).toMatch(/b/);
resolve();
} catch (err) {
reject(err);
}
});

setTimeout(() => {
cp.kill();
}, 10000);
});
});
});

0 comments on commit fc56276

Please sign in to comment.