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 db8a3c3 commit f276d88
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,15 @@ describe('/test/index.js', () => {
// prepare
const runPath = join(__dirname, 'fixtures/add_file');
const file = join(runPath, 'a.ts');
if (existsSync(file)) {
unlinkSync(file);
const fileList = [
file,
join(runPath, 'dist/a.js'),
]

for (const f of fileList) {
if (existsSync(f)) {
unlinkSync(f);
}
}

const cp = execa('node', [mtscPath, '--watch', '--run', './run.js'], {
Expand All @@ -106,7 +113,51 @@ describe('/test/index.js', () => {
cp.on('exit', code => {
try {
expect(existsSync(join(runPath, 'dist/a.js'))).toBeTruthy();
expect(readFileSync(join(runPath, 'dist/a.js'), 'utf-8')).toMatch(/b/);
expect(readFileSync(join(runPath, 'dist/a.js'), 'utf-8')).toMatch(/"b"/);
resolve();
} catch (err) {
reject(err);
}
});

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

it('should test ts file init error and reload process', async () => {

// prepare
const runPath = join(__dirname, 'fixtures/add_file');
const file = join(runPath, 'a.ts');

const fileList = [
file,
join(runPath, 'dist/a.js'),
]

for (const f of fileList) {
if (existsSync(f)) {
unlinkSync(f);
}
}
// add a error file
writeFileSync(file, 'console.log("a)');

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

// 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);
Expand Down

0 comments on commit f276d88

Please sign in to comment.