Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve flaky test #375

Merged
merged 6 commits into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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