Skip to content

Commit

Permalink
Addon-Test: Refactor bootTestRunner to handle fatal errors and improv…
Browse files Browse the repository at this point in the history
…e error reporting
  • Loading branch information
valentinpalkovic committed Oct 11, 2024
1 parent 6b6537b commit 16077e6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
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

0 comments on commit 16077e6

Please sign in to comment.