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

Release: Prerelease alpha 8.3.0-alpha.9 #28938

Merged
merged 59 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
c621fdc
Svelte: Fix events not being logged in Actions when a story has decor…
JReinhold Jun 14, 2024
834d31d
Merge branch 'next' into jeppe/fix-svelte-decorator-actions
JReinhold Jun 14, 2024
f2901cc
pass argTypes directly to SlotDecorator
JReinhold Jun 26, 2024
0788157
feat: make PackageJson optional for starting a dev server
tobiasdiez Jul 14, 2024
d564379
Merge branch 'next' into packageJson
ndelangen Jul 16, 2024
52e56ba
Update code/core/src/core-server/build-dev.ts
ndelangen Aug 2, 2024
dfa174d
Merge branch 'next' into packageJson
ndelangen Aug 2, 2024
25141bd
Merge branch 'next' into packageJson
tobiasdiez Aug 3, 2024
6a161f1
Merge branch 'next' into packageJson
ndelangen Aug 6, 2024
4be31e9
Merge branch 'next' into packageJson
tobiasdiez Aug 8, 2024
620b587
Merge branch 'next' into packageJson
tobiasdiez Aug 13, 2024
f4f0364
fix import
tobiasdiez Aug 13, 2024
80cca52
Merge branch 'next' into pr/tobiasdiez/28594
ndelangen Aug 14, 2024
9d9b04c
fixes
ndelangen Aug 14, 2024
46a838f
Implement add command for vitest addon
kasperpeulen Aug 19, 2024
3cee65e
Better texts
kasperpeulen Aug 19, 2024
1a4a5e7
Improve glob
kasperpeulen Aug 19, 2024
f4db6c0
Add more log lines
kasperpeulen Aug 19, 2024
c98d4c2
Support adding workspace files if vitest.config.ts exists
kasperpeulen Aug 20, 2024
480f404
Update unit tests
kasperpeulen Aug 20, 2024
e9070eb
Update code/addons/vitest/src/postinstall.ts
kasperpeulen Aug 20, 2024
675fdb8
Get correct path
kasperpeulen Aug 20, 2024
f83fe19
Merge branch 'next' into packageJson
tobiasdiez Aug 21, 2024
51f64f6
Merge pull request #28594 from tobiasdiez/packageJson
ndelangen Aug 21, 2024
dbd4844
Add comment about isolation
kasperpeulen Aug 21, 2024
419b81c
Rollback
kasperpeulen Aug 21, 2024
71ad827
Fix
kasperpeulen Aug 21, 2024
718b879
Improve messages
kasperpeulen Aug 21, 2024
a26e267
Validate framework
kasperpeulen Aug 21, 2024
de8c666
Merge branch 'next' into jeppe/fix-svelte-decorator-actions
JReinhold Aug 21, 2024
6c4651b
Fix default viewports
kasperpeulen Aug 21, 2024
840c1bb
Fix tests
kasperpeulen Aug 21, 2024
1a59b66
add default options for addon-viewports via parameters
ndelangen Aug 21, 2024
1dddc61
Merge pull request #28944 from storybookjs/norbert/viewport-default-v…
ndelangen Aug 22, 2024
ce006bf
Update code/addons/vitest/src/postinstall.ts
kasperpeulen Aug 22, 2024
76734a0
Update code/addons/vitest/src/postinstall.ts
kasperpeulen Aug 22, 2024
78d544e
Update code/addons/vitest/src/postinstall.ts
kasperpeulen Aug 22, 2024
b45a9b4
Update code/addons/vitest/src/postinstall.ts
kasperpeulen Aug 22, 2024
d8a5c72
Update code/addons/vitest/src/postinstall.ts
kasperpeulen Aug 22, 2024
cc42862
Correct message
kasperpeulen Aug 22, 2024
039f2e8
Merge pull request #28247 from storybookjs/jeppe/fix-svelte-decorator…
JReinhold Aug 22, 2024
3d68903
Merge branch 'next' into kasper/vitest-add
kasperpeulen Aug 22, 2024
5026a1b
Make sure local @storybook/cli packages is used in linked mode
kasperpeulen Aug 22, 2024
9c82d82
Version it
kasperpeulen Aug 22, 2024
b80775b
Change isolate message
kasperpeulen Aug 22, 2024
ce2afff
Merge remote-tracking branch 'origin/kasper/vitest-add' into kasper/v…
kasperpeulen Aug 22, 2024
4d80b1f
Fix code style
kasperpeulen Aug 22, 2024
88a0059
Merge branch 'next' into kasper/viewport
kasperpeulen Aug 22, 2024
accf304
Merge pull request #28950 from storybookjs/kasper/add-cli-pacakge
kasperpeulen Aug 22, 2024
4bbfb14
Merge pull request #28943 from storybookjs/kasper/viewport
kasperpeulen Aug 22, 2024
0a35a32
Fix more codestyle
kasperpeulen Aug 22, 2024
f1031cb
Merge branch 'next' into kasper/vitest-add
kasperpeulen Aug 22, 2024
61338bf
Fix default export
kasperpeulen Aug 23, 2024
cd8f1f2
Fix
kasperpeulen Aug 23, 2024
c17df9f
Make renderer nullable
kasperpeulen Aug 23, 2024
9630148
Fix type issues
kasperpeulen Aug 23, 2024
dc9432a
Throw errors in renderToCanvas in portable stories
kasperpeulen Aug 23, 2024
ea48115
Merge pull request #28920 from storybookjs/kasper/vitest-add
kasperpeulen Aug 23, 2024
befba4f
Write changelog for 8.3.0-alpha.9 [skip ci]
storybook-bot Aug 23, 2024
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: 8 additions & 0 deletions CHANGELOG.prerelease.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 8.3.0-alpha.9

- Addon Viewport: Add default options via parameters - [#28944](https://github.com/storybookjs/storybook/pull/28944), thanks @ndelangen!
- CLI: Make PackageJson optional for starting a dev server - [#28594](https://github.com/storybookjs/storybook/pull/28594), thanks @tobiasdiez!
- Svelte: Fix events not being logged in Actions when a story has decorators - [#28247](https://github.com/storybookjs/storybook/pull/28247), thanks @JReinhold!
- Vitest: Fix default viewport - [#28943](https://github.com/storybookjs/storybook/pull/28943), thanks @kasperpeulen!
- Vitest: Implement add command for vitest addon - [#28920](https://github.com/storybookjs/storybook/pull/28920), thanks @kasperpeulen!

## 8.3.0-alpha.8

- Addon Vitest: Improve transformation logic to avoid duplicate tests - [#28929](https://github.com/storybookjs/storybook/pull/28929), thanks @yannbf!
Expand Down
3 changes: 0 additions & 3 deletions code/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,6 @@ export const parameters = {
'slategray',
],
},
viewport: {
options: MINIMAL_VIEWPORTS,
},
themes: {
disable: true,
},
Expand Down
2 changes: 1 addition & 1 deletion code/addons/viewport/preview.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { globals } from './dist/preview';
export * from './dist/preview';
9 changes: 9 additions & 0 deletions code/addons/viewport/src/preview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PARAM_KEY as KEY } from './constants';
import { MINIMAL_VIEWPORTS } from './defaults';
import type { GlobalState } from './types';

const modern: Record<string, GlobalState> = {
Expand All @@ -9,3 +10,11 @@ const modern: Record<string, GlobalState> = {
const legacy = { viewport: 'reset', viewportRotated: false };

export const initialGlobals = FEATURES?.viewportStoryGlobals ? modern : legacy;

export const parameters = FEATURES?.viewportStoryGlobals
? {
[KEY]: {
options: MINIMAL_VIEWPORTS,
},
}
: {};
3 changes: 3 additions & 0 deletions code/addons/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
},
"devDependencies": {
"@vitest/browser": "^2.0.0",
"find-up": "^7.0.0",
"tinyrainbow": "^1.2.0",
"ts-dedent": "^2.2.0",
"vitest": "^2.0.0"
},
"peerDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions code/addons/vitest/src/plugin/viewports.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable no-underscore-dangle */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';

import { page } from '@vitest/browser/context';

import { DEFAULT_VIEWPORT_DIMENSIONS, type ViewportsParam, setViewport } from './viewports';
Expand Down Expand Up @@ -42,6 +44,7 @@ describe('setViewport', () => {

it('should set the dimensions of viewport from INITIAL_VIEWPORTS', async () => {
const viewportsParam: any = {
viewports: INITIAL_VIEWPORTS,
// supported by default in addon viewports
defaultViewport: 'ipad',
};
Expand Down
6 changes: 3 additions & 3 deletions code/addons/vitest/src/plugin/viewports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { UnsupportedViewportDimensionError } from 'storybook/internal/preview-er

import { page } from '@vitest/browser/context';

import { INITIAL_VIEWPORTS } from '../../../viewport/src/defaults';
import { MINIMAL_VIEWPORTS } from '../../../viewport/src/defaults';
import type { ViewportMap, ViewportStyles } from '../../../viewport/src/types';

declare global {
Expand Down Expand Up @@ -50,12 +50,12 @@ const parseDimension = (value: string, dimension: 'width' | 'height') => {
export const setViewport = async (viewportsParam: ViewportsParam = {} as ViewportsParam) => {
const defaultViewport = viewportsParam.defaultViewport;

if (!page || !globalThis.__vitest_browser__ || !defaultViewport) {
if (!page || !globalThis.__vitest_browser__) {
return;
}

const viewports = {
...INITIAL_VIEWPORTS,
...MINIMAL_VIEWPORTS,
...viewportsParam.viewports,
};

Expand Down
255 changes: 253 additions & 2 deletions code/addons/vitest/src/postinstall.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,254 @@
export default async function postinstall(context: any) {
console.log('[addon-vitest] postinstall with', context);
import { existsSync } from 'node:fs';
import * as fs from 'node:fs/promises';
import { writeFile } from 'node:fs/promises';
import { dirname, join, relative, resolve } from 'node:path';
import * as path from 'node:path';

import {
JsPackageManagerFactory,
extractProperFrameworkName,
loadAllPresets,
loadMainConfig,
validateFrameworkName,
} from 'storybook/internal/common';
import { logger } from 'storybook/internal/node-logger';

import { findUp } from 'find-up';
import c from 'tinyrainbow';
import dedent from 'ts-dedent';

import { type PostinstallOptions } from '../../../lib/cli-storybook/src/add';

const extensions = ['.ts', '.mts', '.cts', '.js', '.mjs', '.cjs'];

export default async function postInstall(options: PostinstallOptions) {
const packageManager = JsPackageManagerFactory.getPackageManager({
force: options.packageManager,
});

const info = await getFrameworkInfo(options);

if (
info.frameworkPackageName !== '@storybook/nextjs' &&
info.builderPackageName !== '@storybook/builder-vite'
) {
logger.info(
'The Vitest addon can only be used with a Vite-based Storybook framework or Next.js.'
);
return;
}

const annotationsImport = [
'@storybook/nextjs',
'@storybook/experimental-nextjs-vite',
'@storybook/sveltekit',
].includes(info.frameworkPackageName)
? info.frameworkPackageName
: info.rendererPackageName &&
['@storybook/react', '@storybook/svelte', '@storybook/vue3'].includes(
info.rendererPackageName
)
? info.rendererPackageName
: null;

if (!annotationsImport) {
logger.info('The Vitest addon cannot yet be used with: ' + info.frameworkPackageName);
return;
}

const vitestInfo = getVitestPluginInfo(info.frameworkPackageName);

const packages = ['vitest@latest', '@vitest/browser@latest', 'playwright@latest'];

logger.info(
dedent`
We detected that you're using Next.js.
We will configure the vite-plugin-storybook-nextjs plugin to allow you to run tests in Vitest.
`
);

if (info.frameworkPackageName === '@storybook/nextjs') {
packages.push('vite-plugin-storybook-nextjs@latest');
}
logger.info(c.bold('Installing packages...'));
logger.info(packages.join(', '));
await packageManager.addDependencies({ installAsDevDependencies: true }, packages);

logger.info(c.bold('Executing npx playwright install chromium --with-deps ...'));
await packageManager.executeCommand({
command: 'npx',
args: ['playwright', 'install', 'chromium', '--with-deps'],
});

logger.info(c.bold('Writing .storybook/vitest.setup.ts file...'));

const previewExists = extensions
.map((ext) => path.resolve(path.join(options.configDir, `preview${ext}`)))
.some((config) => existsSync(config));

await writeFile(
resolve(options.configDir, 'vitest.setup.ts'),
dedent`
import { beforeAll } from 'vitest'
import { setProjectAnnotations } from '${annotationsImport}'
${previewExists ? `import * as projectAnnotations from './preview'` : ''}

const project = setProjectAnnotations(${previewExists ? 'projectAnnotations' : '[]'})

beforeAll(project.beforeAll)
`
);

const configFiles = extensions.map((ext) => 'vitest.config' + ext);

const rootConfig = await findUp(configFiles, {
cwd: process.cwd(),
});

if (rootConfig) {
const extname = rootConfig ? path.extname(rootConfig) : '.ts';
const browserWorkspaceFile = resolve(dirname(rootConfig), `vitest.workspace${extname}`);
if (existsSync(browserWorkspaceFile)) {
logger.info(
dedent`
We can not automatically setup the plugin when you use Vitest with workspaces.
Please refer to the documentation to complete the setup manually:
https://storybook.js.org/docs/writing-tests/test-runner-with-vitest#manual
`
);
} else {
logger.info(c.bold('Writing vitest.workspace.ts file...'));
await writeFile(
browserWorkspaceFile,
dedent`
import { defineWorkspace } from 'vitest/config';
import { storybookTest } from '@storybook/experimental-addon-vitest/plugin';
${vitestInfo.frameworkPluginImport ? vitestInfo.frameworkPluginImport + '\n' : ''}
export default defineWorkspace([
'${relative(dirname(browserWorkspaceFile), rootConfig)}',
{
plugins: [
storybookTest(),${vitestInfo.frameworkPluginCall ? '\n' + vitestInfo.frameworkPluginCall : ''}
],
test: {
include: ['**/*.stories.?(m)[jt]s?(x)'],
browser: {
enabled: true,
name: 'chromium',
provider: 'playwright',
headless: true,
},
// Disabling isolation is faster and is similar to how tests are isolated in storybook itself.
// Consider removing this if you are seeing problems with your tests.
isolate: false,
setupFiles: ['./.storybook/vitest.setup.ts'],
},
},
]);
`
);
}
} else {
logger.info(c.bold('Writing vitest.config.ts file...'));
await writeFile(
resolve('vitest.config.ts'),
dedent`
import { defineConfig } from "vitest/config";
import { storybookTest } from "@storybook/experimental-addon-vitest/plugin";
${vitestInfo.frameworkPluginImport ? vitestInfo.frameworkPluginImport + '\n' : ''}
export default defineConfig({
plugins: [
storybookTest(),${vitestInfo.frameworkPluginCall ? '\n' + vitestInfo.frameworkPluginCall : ''}
],
test: {
include: ['**/*.stories.?(m)[jt]s?(x)'],
browser: {
enabled: true,
name: 'chromium',
provider: 'playwright',
headless: true,
},
// Disabling isolation is faster and is similar to how tests are isolated in storybook itself.
// Consider removing this, if you have flaky tests.
isolate: false,
setupFiles: ['./.storybook/vitest.setup.ts'],
},
});
`
);
}

logger.info(
dedent`
The Vitest addon is now configured and you're ready to run your tests!
Check the documentation for more information about its features and options at:
https://storybook.js.org/docs/writing-tests/test-runner-with-vitest
`
);
}

const getVitestPluginInfo = (framework: string) => {
let frameworkPluginImport = '';
let frameworkPluginCall = '';

if (framework === '@storybook/nextjs') {
frameworkPluginImport = "import vitePluginNext from 'vite-plugin-storybook-nextjs'";
frameworkPluginCall = 'vitePluginNext()';
}

if (framework === '@storybook/sveltekit') {
frameworkPluginImport = "import { storybookSveltekitPlugin } from '@storybook/sveltekit/vite'";
frameworkPluginCall = 'storybookSveltekitPlugin()';
}

return { frameworkPluginImport, frameworkPluginCall };
};

async function getFrameworkInfo({ configDir, packageManager: pkgMgr }: PostinstallOptions) {
const packageManager = JsPackageManagerFactory.getPackageManager({ force: pkgMgr });
const packageJson = await packageManager.retrievePackageJson();

const config = await loadMainConfig({ configDir, noCache: true });
const { framework } = config;

const frameworkName = typeof framework === 'string' ? framework : framework?.name;
validateFrameworkName(frameworkName);
const frameworkPackageName = extractProperFrameworkName(frameworkName);

console.log(configDir);
const presets = await loadAllPresets({
corePresets: [join(frameworkName, 'preset')],
overridePresets: [
require.resolve('@storybook/core/core-server/presets/common-override-preset'),
],
configDir,
packageJson,
isCritical: true,
});

const core = await presets.apply('core', {});

const { builder, renderer } = core;

if (!builder) {
throw new Error('Could not detect your Storybook builder.');
}

const builderPackageJson = await fs.readFile(
`${typeof builder === 'string' ? builder : builder.name}/package.json`,
'utf8'
);
const builderPackageName = JSON.parse(builderPackageJson).name;

let rendererPackageName: string | undefined;
if (renderer) {
const rendererPackageJson = await fs.readFile(`${renderer}/package.json`, 'utf8');
rendererPackageName = JSON.parse(rendererPackageJson).name;
}

return {
frameworkPackageName,
builderPackageName,
rendererPackageName,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default async (
inject: false,
template,
templateParameters: {
version: packageJson.version,
version: packageJson?.version,
globals: {
CONFIG_TYPE: configType,
LOGLEVEL: logLevel,
Expand Down
Loading