Skip to content

Commit

Permalink
Merge branch 'next' into shilman/fix-rnw-test-install
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman authored Mar 10, 2025
2 parents a50ff74 + ab4aa24 commit bb2ad1a
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 176 deletions.
18 changes: 9 additions & 9 deletions code/.storybook/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { defaultExclude, defineProject, mergeConfig } from 'vitest/config';

import { storybookTest } from '@storybook/addon-test/vitest-plugin';

import Inspect from 'vite-plugin-inspect';

import { vitestCommonConfig } from '../vitest.workspace';
Expand All @@ -22,14 +24,12 @@ export default mergeConfig(
// @ts-expect-error added this because of testNamePattern below
defineProject({
plugins: [
import('@storybook/addon-test/vitest-plugin').then(({ storybookTest }) =>
storybookTest({
configDir: __dirname,
tags: {
include: ['vitest'],
},
})
),
storybookTest({
configDir: __dirname,
tags: {
include: ['vitest'],
},
}),
...extraPlugins,
],
test: {
Expand All @@ -40,7 +40,7 @@ export default mergeConfig(
'**/__mockdata__/**',
'../**/__mockdata__/**',
'**/Zoom.stories.tsx', // expected to fail in Vitest because of fetching /iframe.html to cause ECONNREFUSED
'**/lib/blocks/src/**', // won't work because of https://github.com/storybookjs/storybook/issues/29783
'../lib/blocks/src/**', // won't work because of https://github.com/storybookjs/storybook/issues/29783
],
// TODO: bring this back once portable stories support storybook/internal/preview-api hooks
// @ts-expect-error this type does not exist but the property does!
Expand Down
6 changes: 3 additions & 3 deletions code/addons/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@
"@types/istanbul-lib-report": "^3.0.3",
"@types/node": "^22.0.0",
"@types/semver": "^7",
"@vitest/browser": "^3.0.2",
"@vitest/runner": "^3.0.2",
"@vitest/browser": "^3.0.8",
"@vitest/runner": "^3.0.8",
"ansi-to-html": "^0.7.2",
"boxen": "^8.0.1",
"es-toolkit": "^1.22.0",
Expand All @@ -130,7 +130,7 @@
"tree-kill": "^1.2.2",
"ts-dedent": "^2.2.0",
"typescript": "^5.7.3",
"vitest": "^3.0.2"
"vitest": "^3.0.8"
},
"peerDependencies": {
"@vitest/browser": "^2.1.1 || ^3.0.0",
Expand Down
9 changes: 0 additions & 9 deletions code/addons/test/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { definePreview } from 'storybook/internal/preview-api';

import * as addonAnnotations from './preview';
import type { storybookTest as storybookTestImport } from './vitest-plugin';

export default () => definePreview(addonAnnotations);

export type { TestParameters } from './types';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-error - this is a hack to make the module's sub-path augmentable
declare module '@storybook/addon-test/vitest-plugin' {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-error - this is a hack to make the module's sub-path augmentable
export const storybookTest: typeof storybookTestImport;
}
41 changes: 40 additions & 1 deletion code/addons/test/src/node/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { API_StatusUpdate } from 'storybook/internal/types';
import type { Suite } from '@vitest/runner';
import { throttle } from 'es-toolkit';
import { satisfies } from 'semver';
import { dedent } from 'ts-dedent';

import { TEST_PROVIDER_ID } from '../constants';
import type { TestManager } from './test-manager';
Expand Down Expand Up @@ -60,6 +61,36 @@ const isVitest3OrLater = vitestVersion
? satisfies(vitestVersion, '>=3.0.0-beta.3', { includePrerelease: true })
: false;

interface VitestError extends Error {
VITEST_TEST_PATH?: string;
VITEST_TEST_NAME?: string;
}

const getErrorOrigin = (error: VitestError): string => {
const parts: string[] = [];

if (error.VITEST_TEST_PATH) {
parts.push(
dedent`
\nThis error originated in "${error.VITEST_TEST_PATH}". It doesn't mean the error was thrown inside the file itself, but while it was running.
`
);
}

if (error.VITEST_TEST_NAME) {
parts.push(
dedent`
The latest test that might've caused the error is "${error.VITEST_TEST_NAME}".
It might mean one of the following:
- The error was thrown, while Vitest was running this test.
- If the error occurred after the test had been completed, this was the last documented test before it was thrown.
`
);
}

return parts.join('\n');
};

export class StorybookReporter implements Reporter {
testStatusData: API_StatusUpdate = {};

Expand Down Expand Up @@ -219,6 +250,14 @@ export class StorybookReporter implements Reporter {

async onFinished() {
const unhandledErrors = this.ctx.state.getUnhandledErrors();
unhandledErrors.forEach((e: unknown) => {
const error = e as VitestError;
const origin = getErrorOrigin(error);
if (origin) {
error.message = `${error.message}\n${origin}`;
error.stack = `${error.stack}\n${origin}`;
}
});

const isCancelled = isVitest3OrLater
? this.testManager.vitestManager.isCancelling
Expand Down Expand Up @@ -256,7 +295,7 @@ export class StorybookReporter implements Reporter {
name: `${unhandledErrors.length} unhandled error${unhandledErrors?.length > 1 ? 's' : ''}`,
message: unhandledErrors
.map((e, index) => `[${index}]: ${(e as any).stack || (e as any).message}`)
.join('\n----------\n'),
.join('\n\n----------\n\n'),
};

this.sendReport({
Expand Down
8 changes: 4 additions & 4 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@
"@typescript-eslint/parser": "7.18.0",
"@vitejs/plugin-react": "^4.3.2",
"@vitejs/plugin-vue": "^4.4.0",
"@vitest/browser": "^3.0.2",
"@vitest/coverage-istanbul": "^3.0.2",
"@vitest/coverage-v8": "^3.0.2",
"@vitest/browser": "^3.0.8",
"@vitest/coverage-istanbul": "^3.0.8",
"@vitest/coverage-v8": "^3.0.8",
"create-storybook": "workspace:*",
"cross-env": "^7.0.3",
"danger": "^12.3.3",
Expand Down Expand Up @@ -224,7 +224,7 @@
"util": "^0.12.4",
"vite": "^4.0.0",
"vite-plugin-inspect": "^0.8.5",
"vitest": "^3.0.2",
"vitest": "^3.0.8",
"wait-on": "^7.0.1"
},
"dependenciesMeta": {
Expand Down
Loading

0 comments on commit bb2ad1a

Please sign in to comment.