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: Correctly stop Storybook when Vitest closes #30012

Merged
merged 1 commit into from
Dec 11, 2024
Merged
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
1 change: 1 addition & 0 deletions code/addons/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"slash": "^5.0.0",
"strip-ansi": "^7.1.0",
"tinyglobby": "^0.2.10",
"tree-kill": "^1.2.2",
"ts-dedent": "^2.2.0",
"typescript": "^5.3.2",
"vitest": "^2.1.3"
Expand Down
27 changes: 16 additions & 11 deletions code/addons/test/src/vitest-plugin/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { GlobalSetupContext } from 'vitest/node';

import { logger } from 'storybook/internal/node-logger';

import treeKill from 'tree-kill';

let storybookProcess: ChildProcess | null = null;

const getIsVitestStandaloneRun = () => {
Expand Down Expand Up @@ -59,23 +61,26 @@ const startStorybookIfNotRunning = async () => {
}
};

const killProcess = (process: ChildProcess) => {
return new Promise((resolve, reject) => {
process.on('close', resolve);
process.on('error', reject);
process.kill();
});
};

export const setup = async ({ config }: GlobalSetupContext) => {
if (config.watch && isVitestStandaloneRun) {
await startStorybookIfNotRunning();
}
};

export const teardown = async () => {
if (storybookProcess) {
logger.verbose('Stopping Storybook process');
await killProcess(storybookProcess);
if (!storybookProcess) {
return;
}
logger.verbose('Stopping Storybook process');
await new Promise<void>((resolve, reject) => {
// Storybook starts multiple child processes, so we need to kill the whole tree
treeKill(storybookProcess.pid, 'SIGTERM', (error) => {
if (error) {
logger.error('Failed to stop Storybook process:');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: error message is incomplete - should include the actual error details

reject(error);
return;
}
resolve();
});
});
};
3 changes: 2 additions & 1 deletion code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6426,6 +6426,7 @@ __metadata:
slash: "npm:^5.0.0"
strip-ansi: "npm:^7.1.0"
tinyglobby: "npm:^0.2.10"
tree-kill: "npm:^1.2.2"
ts-dedent: "npm:^2.2.0"
typescript: "npm:^5.3.2"
vitest: "npm:^2.1.3"
Expand Down Expand Up @@ -27745,7 +27746,7 @@ __metadata:
languageName: node
linkType: hard

"tree-kill@npm:1.2.2":
"tree-kill@npm:1.2.2, tree-kill@npm:^1.2.2":
version: 1.2.2
resolution: "tree-kill@npm:1.2.2"
bin:
Expand Down
Loading