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

Addon-Test: Refactor bootTestRunner to handle fatal errors and improve error reporting #29336

Draft
wants to merge 1 commit into
base: unified-ui-testing
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
41 changes: 29 additions & 12 deletions code/addons/test/src/node/boot-test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@ import { execaNode } from 'execa';
import { TEST_PROVIDER_ID } from '../constants';
import { log } from '../logger';

const MAX_START_TIME = 8000;
const MAX_START_TIME = 30000;

// This path is a bit confusing, but essentially `boot-test-runner` gets bundled into the preset bundle
// which is at the root. Then, from the root, we want to load `node/vitest.mjs`
const vitestModulePath = join(__dirname, 'node', 'vitest.mjs');

export const bootTestRunner = async (channel: Channel, initEvent?: string, initArgs?: any[]) => {
let child: null | ChildProcess;
let child: null | ChildProcess;
let ready = false;

const bootTestRunner = async (channel: Channel, initEvent?: string, initArgs?: any[]) => {
let stderr: string[] = [];

function reportFatalError(e: any) {
channel.emit(TESTING_MODULE_CRASH_REPORT, {
providerId: TEST_PROVIDER_ID,
message: String(e),
} as TestingModuleCrashReportPayload);
}

const forwardRun = (...args: any[]) =>
child?.send({ args, from: 'server', type: TESTING_MODULE_RUN_REQUEST });
const forwardRunAll = (...args: any[]) =>
Expand Down Expand Up @@ -84,11 +93,14 @@ export const bootTestRunner = async (channel: Channel, initEvent?: string, initA
resolve();
} else if (result.type === 'error') {
killChild();

channel.emit(TESTING_MODULE_CRASH_REPORT, {
providerId: TEST_PROVIDER_ID,
message: stderr.join('\n'),
} as TestingModuleCrashReportPayload);
log(result.message);
log(result.error);
// Reject if the child process reports an error before it's ready
if (!ready) {
reject(new Error(`${result.message}\n${result.error}`));
} else {
reportFatalError(result.error);
}
} else {
channel.emit(result.type, ...result.args);
}
Expand All @@ -107,10 +119,15 @@ export const bootTestRunner = async (channel: Channel, initEvent?: string, initA
);

await Promise.race([startChildProcess(), timeout]).catch((e) => {
channel.emit(TESTING_MODULE_CRASH_REPORT, {
providerId: TEST_PROVIDER_ID,
message: String(e),
} as TestingModuleCrashReportPayload);
reportFatalError(e);
throw e;
});
};

export const runTestRunner = async (channel: Channel, initEvent?: string, initArgs?: any[]) => {
if (!child) {
ready = false;
await bootTestRunner(channel, initEvent, initArgs);
ready = true;
}
};
26 changes: 6 additions & 20 deletions code/addons/test/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'storybook/internal/core-events';
import type { Options } from 'storybook/internal/types';

import { bootTestRunner } from './node/boot-test-runner';
import { runTestRunner } from './node/boot-test-runner';

export const checkActionsLoaded = (configDir: string) => {
checkAddonOrder({
Expand Down Expand Up @@ -37,31 +37,17 @@ export const experimental_serverChannel = async (channel: Channel, options: Opti
return channel;
}

let booting = false;
let booted = false;
const start =
const execute =
(eventName: string) =>
(...args: any[]) => {
if (!booted && !booting) {
booting = true;
bootTestRunner(channel, eventName, args)
.then(() => {
booted = true;
})
.catch(() => {
booted = false;
})
.finally(() => {
booting = false;
});
}
runTestRunner(channel, eventName, args);
};

channel.on(TESTING_MODULE_RUN_ALL_REQUEST, start(TESTING_MODULE_RUN_ALL_REQUEST));
channel.on(TESTING_MODULE_RUN_REQUEST, start(TESTING_MODULE_RUN_REQUEST));
channel.on(TESTING_MODULE_RUN_ALL_REQUEST, execute(TESTING_MODULE_RUN_ALL_REQUEST));
channel.on(TESTING_MODULE_RUN_REQUEST, execute(TESTING_MODULE_RUN_REQUEST));
channel.on(TESTING_MODULE_WATCH_MODE_REQUEST, (payload) => {
if (payload.watchMode) {
start(TESTING_MODULE_WATCH_MODE_REQUEST)(payload);
execute(TESTING_MODULE_WATCH_MODE_REQUEST)(payload);
}
});

Expand Down
Loading