Skip to content

Commit de71205

Browse files
committed
fix: convert test mock main configs to valid ESM
The tests package has "type": "module" but mock main.js files used module.exports (CJS), causing storybook's loadMainConfig to fail on the first attempt and fall back. Converting to export default fixes the warning and adds a dedicated CJS test case with main.cjs.
1 parent a5e4541 commit de71205

12 files changed

Lines changed: 45 additions & 7 deletions

File tree

tests/scripts/generate.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ describe('loader', () => {
4141
});
4242
});
4343

44+
describe('when the main config is a cjs file', () => {
45+
it('writes the story imports', async (t) => {
46+
mock.method(require('fs'), 'writeFileSync', mockFs.writeFileSync);
47+
await generate({ configPath: 'scripts/mocks/cjs-config' });
48+
mock.reset();
49+
50+
assert.strictEqual(
51+
pathMock,
52+
path.resolve(__dirname, 'mocks/cjs-config/storybook.requires.ts')
53+
);
54+
t.assert.snapshot(fileContentMock);
55+
});
56+
});
57+
4458
describe('when using js', () => {
4559
it('writes the story imports without types', async (t) => {
4660
mock.method(require('fs'), 'writeFileSync', mockFs.writeFileSync);

tests/scripts/generate.test.ts.snapshot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ exports[`loader > writeRequires > when only port is provided without host > does
2626
"/* do not change this file, it is auto generated by storybook. */\\n/// <reference types=\\"@storybook/react-native/metro-env\\" />\\nimport { start, updateView, View, type Features } from '@storybook/react-native';\\n\\nimport \\"@storybook/addon-ondevice-notes/register\\";\\nimport \\"@storybook/addon-ondevice-controls/register\\";\\nimport \\"@storybook/addon-ondevice-backgrounds/register\\";\\nimport \\"@storybook/addon-ondevice-actions/register\\";\\n\\nconst normalizedStories = [\\n {\\n titlePrefix: \\"\\",\\n directory: \\"./scripts/mocks/all-config-files\\",\\n files: \\"FakeStory.stories.tsx\\",\\n importPathMatcher: /^\\\\.[\\\\\\\\/](?:FakeStory\\\\.stories\\\\.tsx)$/,\\n req: require.context(\\n './',\\n false,\\n /^\\\\.[\\\\\\\\/](?:FakeStory\\\\.stories\\\\.tsx)$/\\n ),\\n }\\n];\\n\\n\\ndeclare global {\\n var view: View;\\n var STORIES: typeof normalizedStories;\\n var STORYBOOK_WEBSOCKET: { host: string; port: number } | undefined;\\n var FEATURES: Features;\\n}\\n\\n\\nconst annotations = [\\n require('./preview'),\\n require(\\"@storybook/react-native/preview\\")\\n];\\n\\nglobalThis.STORIES = normalizedStories;\\n\\n\\nmodule?.hot?.accept?.();\\n\\n\\n\\nif (!globalThis.view) {\\n globalThis.view = start({\\n annotations,\\n storyEntries: normalizedStories,\\n\\n });\\n} else {\\n updateView(globalThis.view, annotations, normalizedStories);\\n}\\n\\nexport const view: View = globalThis.view;\\n"
2727
`;
2828

29+
exports[`loader > writeRequires > when the main config is a cjs file > writes the story imports 1`] = `
30+
"/* do not change this file, it is auto generated by storybook. */\\n/// <reference types=\\"@storybook/react-native/metro-env\\" />\\nimport { start, updateView, View, type Features } from '@storybook/react-native';\\n\\nimport \\"@storybook/addon-ondevice-notes/register\\";\\nimport \\"@storybook/addon-ondevice-controls/register\\";\\nimport \\"@storybook/addon-ondevice-backgrounds/register\\";\\nimport \\"@storybook/addon-ondevice-actions/register\\";\\n\\nconst normalizedStories = [\\n {\\n titlePrefix: \\"\\",\\n directory: \\"./scripts/mocks/cjs-config\\",\\n files: \\"FakeStory.stories.tsx\\",\\n importPathMatcher: /^\\\\.[\\\\\\\\/](?:FakeStory\\\\.stories\\\\.tsx)$/,\\n req: require.context(\\n './',\\n false,\\n /^\\\\.[\\\\\\\\/](?:FakeStory\\\\.stories\\\\.tsx)$/\\n ),\\n }\\n];\\n\\n\\ndeclare global {\\n var view: View;\\n var STORIES: typeof normalizedStories;\\n var STORYBOOK_WEBSOCKET: { host: string; port: number } | undefined;\\n var FEATURES: Features;\\n}\\n\\n\\nconst annotations = [\\n require(\\"@storybook/react-native/preview\\")\\n];\\n\\nglobalThis.STORIES = normalizedStories;\\n\\n\\nmodule?.hot?.accept?.();\\n\\n\\n\\nif (!globalThis.view) {\\n globalThis.view = start({\\n annotations,\\n storyEntries: normalizedStories,\\n\\n });\\n} else {\\n updateView(globalThis.view, annotations, normalizedStories);\\n}\\n\\nexport const view: View = globalThis.view;\\n"
31+
`;
32+
2933
exports[`loader > writeRequires > when there are different file extensions > writes the story imports 1`] = `
3034
"/* do not change this file, it is auto generated by storybook. */\\n/// <reference types=\\"@storybook/react-native/metro-env\\" />\\nimport { start, updateView, View, type Features } from '@storybook/react-native';\\n\\nimport \\"@storybook/addon-ondevice-notes/register\\";\\nimport \\"@storybook/addon-ondevice-controls/register\\";\\nimport \\"@storybook/addon-ondevice-backgrounds/register\\";\\nimport \\"@storybook/addon-ondevice-actions/register\\";\\n\\nconst normalizedStories = [\\n {\\n titlePrefix: \\"\\",\\n directory: \\"./scripts/mocks/file-extensions\\",\\n files: \\"FakeStory.stories.tsx\\",\\n importPathMatcher: /^\\\\.[\\\\\\\\/](?:FakeStory\\\\.stories\\\\.tsx)$/,\\n req: require.context(\\n './',\\n false,\\n /^\\\\.[\\\\\\\\/](?:FakeStory\\\\.stories\\\\.tsx)$/\\n ),\\n }\\n];\\n\\n\\ndeclare global {\\n var view: View;\\n var STORIES: typeof normalizedStories;\\n var STORYBOOK_WEBSOCKET: { host: string; port: number } | undefined;\\n var FEATURES: Features;\\n}\\n\\n\\nconst annotations = [\\n require('./preview'),\\n require(\\"@storybook/react-native/preview\\")\\n];\\n\\nglobalThis.STORIES = normalizedStories;\\n\\n\\nmodule?.hot?.accept?.();\\n\\n\\n\\nif (!globalThis.view) {\\n globalThis.view = start({\\n annotations,\\n storyEntries: normalizedStories,\\n\\n });\\n} else {\\n updateView(globalThis.view, annotations, normalizedStories);\\n}\\n\\nexport const view: View = globalThis.view;\\n"
3135
`;

tests/scripts/mocks/all-config-files/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
stories: ['./FakeStory.stories.tsx'],
33
addons: [
44
'@storybook/addon-ondevice-notes',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
stories: [],
33
addons: [],
44
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const FakeComponent = () => null;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { FakeComponent } from './FakeComponent';
2+
3+
export default {
4+
title: 'components/FakeComponent',
5+
component: FakeComponent,
6+
};
7+
8+
export const Basic = {
9+
args: {},
10+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
stories: ['./FakeStory.stories.tsx'],
3+
addons: [
4+
'@storybook/addon-ondevice-notes',
5+
'@storybook/addon-ondevice-controls',
6+
'@storybook/addon-ondevice-backgrounds',
7+
'@storybook/addon-ondevice-actions',
8+
],
9+
};

tests/scripts/mocks/configuration-objects/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
stories: [
33
{
44
files: '**/*.stories.tsx',

tests/scripts/mocks/exclude-config-files/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
stories: ['**/*.stories.tsx'],
33
reactNativeOptions: {
44
excludePaths: '**/exclude-components/**',

tests/scripts/mocks/no-preview/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
stories: ['./FakeStory.stories.tsx'],
33
addons: [
44
'@storybook/addon-ondevice-notes',

0 commit comments

Comments
 (0)