Skip to content

Commit

Permalink
test: improve flaky test (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 9, 2023
1 parent 0e83db7 commit 2995601
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions tests/specs/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export default testSuite(async ({ describe }) => {
expect(tsxProcess.stderr).toMatch('Error: Missing required parameter "script path"');
});

test('watch files for changes', async ({ onTestFinish }) => {
let initialValue = Date.now();
test('watch files for changes', async ({ onTestFinish, onTestFail }) => {
const fixtureWatch = await createFixture({
'package.json': JSON.stringify({
type: 'module',
Expand All @@ -31,7 +30,7 @@ export default testSuite(async ({ describe }) => {
import { value } from './value.js';
console.log(value);
`,
'value.js': `export const value = ${initialValue};`,
'value.js': 'export const value = \'hello world\';',
});
onTestFinish(async () => await fixtureWatch.rm());

Expand All @@ -43,17 +42,26 @@ export default testSuite(async ({ describe }) => {
cwd: fixtureWatch.path,
});

onTestFail(async () => {
if (tsxProcess.exitCode === null) {
console.log('Force killing hanging process\n\n');
tsxProcess.kill('SIGKILL');
console.log({
tsxProcess: await tsxProcess,
});
}
});

await processInteract(
tsxProcess.stdout!,
[
async (data) => {
if (data.includes(`${initialValue}\n`)) {
initialValue = Date.now();
await fixtureWatch.writeFile('value.js', `export const value = ${initialValue};`);
if (data.includes('hello world\n')) {
fixtureWatch.writeFile('value.js', 'export const value = \'goodbye world\';');
return true;
}
},
data => data.includes(`${initialValue}\n`),
data => data.includes('goodbye world\n'),
],
5000,
);
Expand Down

0 comments on commit 2995601

Please sign in to comment.