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: Add Vitest 3 support #30181

Merged
merged 26 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
09196b1
Addon Test: Add Vitest 3 support
valentinpalkovic Jan 3, 2025
e61762b
Adjust tests for Vitest 3
valentinpalkovic Jan 3, 2025
cbc7305
Remove unnecessary spread of browser config in vitest-plugin
valentinpalkovic Jan 3, 2025
91ad81f
Merge remote-tracking branch 'origin/valentin/a11y-refactorings' into…
valentinpalkovic Jan 3, 2025
dc6db06
Update yarn.lock
valentinpalkovic Jan 3, 2025
dba7ad3
Downgrade Vite and fix type issues
valentinpalkovic Jan 3, 2025
c2245b0
Further Vitest 3 compat adjustments
valentinpalkovic Jan 3, 2025
aded285
Fix tests
valentinpalkovic Jan 3, 2025
161a29c
Merge remote-tracking branch 'origin/valentin/a11y-refactorings' into…
valentinpalkovic Jan 10, 2025
61996e1
Upgrade to Vitest 3.0.0-beta.4
valentinpalkovic Jan 10, 2025
d099241
Remove obsolete comma
valentinpalkovic Jan 10, 2025
a6be1e6
Format file outputs in postinstall of addon-test
valentinpalkovic Jan 10, 2025
e6ec8b1
Merge remote-tracking branch 'origin/valentin/a11y-refactorings' into…
valentinpalkovic Jan 10, 2025
ecf9f7f
Fix tests
valentinpalkovic Jan 10, 2025
644690c
Fix tests
valentinpalkovic Jan 10, 2025
ccbdb71
Merge remote-tracking branch 'origin/valentin/a11y-refactorings' into…
valentinpalkovic Jan 13, 2025
ed3ed9a
Merge remote-tracking branch 'origin/next' into valentin/add-vitest-3…
valentinpalkovic Jan 13, 2025
b2da8de
Update Vitest packages to 3.0.0 beta
valentinpalkovic Jan 13, 2025
e90d717
Refactor Vitest browser configuration
valentinpalkovic Jan 15, 2025
a00619c
Remove comment
valentinpalkovic Jan 15, 2025
b1e99df
Remove test timeout
valentinpalkovic Jan 15, 2025
2fdb0bf
Merge remote-tracking branch 'origin/next' into valentin/add-vitest-3…
valentinpalkovic Jan 15, 2025
4c45960
Refactor Vitest version import and check for compatibility with versi…
valentinpalkovic Jan 15, 2025
a539692
Fix optional chaining for Vitest compatibility checks
valentinpalkovic Jan 15, 2025
3071566
fix watch mode in Vitest 3
valentinpalkovic Jan 15, 2025
f4373a5
Cleanup and apply requested changes from review
valentinpalkovic Jan 15, 2025
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
8 changes: 6 additions & 2 deletions code/.storybook/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default mergeConfig(
plugins: [
import('@storybook/experimental-addon-test/vitest-plugin').then(({ storybookTest }) =>
storybookTest({
configDir: process.cwd(),
configDir: __dirname,
tags: {
include: ['vitest'],
},
Expand All @@ -47,10 +47,14 @@ export default mergeConfig(
testNamePattern: /^(?!.*(UseState)).*$/,
browser: {
enabled: true,
name: 'chromium',
provider: 'playwright',
headless: true,
screenshotFailures: false,
instances: [
{
browser: 'chromium',
},
],
},
setupFiles: ['./storybook.setup.ts'],
environment: 'happy-dom',
Expand Down
12 changes: 6 additions & 6 deletions code/addons/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
"@types/istanbul-lib-report": "^3.0.3",
"@types/node": "^22.0.0",
"@types/semver": "^7",
"@vitest/browser": "^2.1.3",
"@vitest/runner": "^2.1.3",
"@vitest/browser": "3.0.0-beta.4",
"@vitest/runner": "3.0.0-beta.4",
"ansi-to-html": "^0.7.2",
"boxen": "^8.0.1",
"es-toolkit": "^1.22.0",
Expand All @@ -116,13 +116,13 @@
"tree-kill": "^1.2.2",
"ts-dedent": "^2.2.0",
"typescript": "^5.3.2",
"vitest": "^2.1.3"
"vitest": "3.0.0-beta.4"
},
"peerDependencies": {
"@vitest/browser": "^2.1.1",
"@vitest/runner": "^2.1.1",
"@vitest/browser": "^2.1.1 || ^3.0.0",
"@vitest/runner": "^2.1.1 || ^3.0.0",
"storybook": "workspace:^",
"vitest": "^2.1.1"
"vitest": "^2.1.1 || ^3.0.0"
},
"peerDependenciesMeta": {
"@vitest/browser": {
Expand Down
10 changes: 9 additions & 1 deletion code/addons/test/src/node/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TaskState } from 'vitest';
import type { Vitest } from 'vitest/node';
import { version as vitestVersion } from 'vitest/node';
import { type Reporter } from 'vitest/reporters';

import type {
Expand All @@ -12,6 +13,7 @@ import type { API_StatusUpdate } from '@storybook/types';

import type { Suite } from '@vitest/runner';
import { throttle } from 'es-toolkit';
import { satisfies } from 'semver';

import { TEST_PROVIDER_ID } from '../constants';
import type { TestManager } from './test-manager';
Expand Down Expand Up @@ -50,8 +52,11 @@ const statusMap: Record<TaskState, TestStatus> = {
run: 'pending',
skip: 'skipped',
todo: 'skipped',
queued: 'pending',
};

const isVitest3OrLater = satisfies(vitestVersion, '>=3.0.0-beta.3', { includePrerelease: true });

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

Expand Down Expand Up @@ -213,7 +218,10 @@ export class StorybookReporter implements Reporter {
async onFinished() {
const unhandledErrors = this.ctx.state.getUnhandledErrors();

const isCancelled = this.ctx.isCancelling;
const isCancelled = isVitest3OrLater
? this.testManager.vitestManager.isCancelling
: // @ts-expect-error isCancelling is private in Vitest 3.
this.ctx.isCancelling;
const report = await this.getProgressReport(Date.now());

const testSuiteFailures = report.details.testResults.filter(
Expand Down
22 changes: 11 additions & 11 deletions code/addons/test/src/node/test-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ const vitest = vi.hoisted(() => ({
cancelCurrentRun: vi.fn(),
globTestSpecs: vi.fn(),
getModuleProjects: vi.fn(() => []),
configOverride: {
actualTestNamePattern: undefined,
get testNamePattern() {
return this.actualTestNamePattern!;
setGlobalTestNamePattern: setTestNamePattern,
vite: {
watcher: {
removeAllListeners: vi.fn(),
on: vi.fn(),
},
set testNamePattern(value: string) {
setTestNamePattern(value);
// @ts-expect-error Ignore for testing
this.actualTestNamePattern = value;
moduleGraph: {
getModulesByFile: () => [],
invalidateModule: vi.fn(),
},
},
}));

vi.mock('vitest/node', () => ({
vi.mock('vitest/node', async (importOriginal) => ({
...(await importOriginal()),
createVitest: vi.fn(() => Promise.resolve(vitest)),
}));
const createVitest = vi.mocked(actualCreateVitest);
Expand Down Expand Up @@ -85,7 +86,7 @@ const options: ConstructorParameters<typeof TestManager>[1] = {
onReady: vi.fn(),
};

describe('TestManager', () => {
describe('TestManager', { timeout: 2000 }, () => {
it('should create a vitest instance', async () => {
new TestManager(mockChannel, options);
await new Promise((r) => setTimeout(r, 1000));
Expand Down Expand Up @@ -137,7 +138,6 @@ describe('TestManager', () => {
storyIds: [],
});
expect(vitest.runFiles).toHaveBeenCalledWith([], true);
expect(vitest.configOverride.testNamePattern).toBeUndefined();

await testManager.handleRunRequest({
providerId: TEST_PROVIDER_ID,
Expand Down
Loading
Loading