Skip to content

Commit

Permalink
feat(storybook): add support for basic test-runner configuration (#11101
Browse files Browse the repository at this point in the history
)

Closes #11047
  • Loading branch information
Djiit committed Sep 27, 2022
1 parent 24880aa commit 2f91d96
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,11 @@
"**/**/src/**/*.other.*",
"libs/my-lib/src/not-stories/**,**/**/src/**/*.other.*,apps/my-app/**/*.something.ts"
]
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
}
},
"additionalProperties": false,
Expand Down
5 changes: 5 additions & 0 deletions docs/generated/packages/react.json
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,11 @@
"**/**/src/**/*.other.*",
"libs/my-lib/src/not-stories/**,**/**/src/**/*.other.*,apps/my-app/**/*.something.ts"
]
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
}
},
"required": ["name"],
Expand Down
5 changes: 5 additions & 0 deletions docs/generated/packages/storybook.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean"
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
}
},
"required": ["name"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface StorybookConfigurationOptions {
tsConfiguration?: boolean;
skipFormat?: boolean;
ignorePaths?: string[];
configureTestRunner?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
"**/**/src/**/*.other.*",
"libs/my-lib/src/not-stories/**,**/**/src/**/*.other.*,apps/my-app/**/*.something.ts"
]
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
}
},
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface StorybookConfigureSchema {
cypressDirectory?: string;
standaloneConfig?: boolean;
ignorePaths?: string[];
configureTestRunner?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
"**/**/src/**/*.other.*",
"libs/my-lib/src/not-stories/**,**/**/src/**/*.other.*,apps/my-app/**/*.something.ts"
]
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
}
},
"required": ["name"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,27 @@ describe('@nrwl/storybook:configuration', () => {
tree.exists('libs/test-ui-lib-2/.storybook/preview.js')
).toBeFalsy();
});

it.only('should add test-storybook target', async () => {
await configurationGenerator(tree, {
name: 'test-ui-lib',
uiFramework: '@storybook/react',
configureTestRunner: true,
});

expect(
readJson(tree, 'package.json').devDependencies['@storybook/test-runner']
).toBeTruthy();

const project = readProjectConfiguration(tree, 'test-ui-lib');
expect(project.targets['test-storybook']).toEqual({
executor: 'nx:run-commands',
options: {
command:
'test-storybook -c libs/test-ui-lib/.storybook --url=http://localhost:4400',
},
});
});
});

describe('for other types of projects - Next.js and the swc compiler', () => {
Expand Down
22 changes: 20 additions & 2 deletions packages/storybook/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { findStorybookAndBuildTargetsAndCompiler } from '../../utils/utilities';
import {
storybookNextAddonVersion,
storybookSwcAddonVersion,
storybookTestRunnerVersion,
} from '../../utils/versions';

export async function configurationGenerator(
Expand Down Expand Up @@ -64,9 +65,14 @@ export async function configurationGenerator(
addBuildStorybookToCacheableOperations(tree);

if (schema.uiFramework === '@storybook/angular') {
addAngularStorybookTask(tree, schema.name);
addAngularStorybookTask(tree, schema.name, schema.configureTestRunner);
} else {
addStorybookTask(tree, schema.name, schema.uiFramework);
addStorybookTask(
tree,
schema.name,
schema.uiFramework,
schema.configureTestRunner
);
}

if (schema.configureCypress) {
Expand Down Expand Up @@ -107,6 +113,18 @@ export async function configurationGenerator(
);
}

if (schema.configureTestRunner === true) {
tasks.push(
addDependenciesToPackageJson(
tree,
{},
{
'@storybook/test-runner': storybookTestRunnerVersion,
}
)
);
}

await formatFiles(tree);

return runTasksInSerial(...tasks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export interface StorybookConfigureSchema {
tsConfiguration?: boolean;
cypressDirectory?: string;
standaloneConfig?: boolean;
configureTestRunner?: boolean;
}
5 changes: 5 additions & 0 deletions packages/storybook/src/generators/configuration/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean"
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
}
},
"required": ["name"]
Expand Down
33 changes: 30 additions & 3 deletions packages/storybook/src/generators/configuration/util-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ import {
import { StorybookConfigureSchema } from './schema';
import { getRootTsConfigPathInTree } from '@nrwl/workspace/src/utilities/typescript';

const DEFAULT_PORT = 4400;

export function addStorybookTask(
tree: Tree,
projectName: string,
uiFramework: string
uiFramework: string,
configureTestRunner: boolean
) {
if (uiFramework === '@storybook/react-native') {
return;
Expand All @@ -37,7 +40,7 @@ export function addStorybookTask(
executor: '@nrwl/storybook:storybook',
options: {
uiFramework,
port: 4400,
port: DEFAULT_PORT,
config: {
configFolder: `${projectConfig.root}/.storybook`,
},
Expand All @@ -48,6 +51,7 @@ export function addStorybookTask(
},
},
};

projectConfig.targets['build-storybook'] = {
executor: '@nrwl/storybook:build',
outputs: ['{options.outputPath}'],
Expand All @@ -65,10 +69,23 @@ export function addStorybookTask(
},
};

if (configureTestRunner === true) {
projectConfig.targets['test-storybook'] = {
executor: 'nx:run-commands',
options: {
command: `test-storybook -c ${projectConfig.root}/.storybook --url=http://localhost:${DEFAULT_PORT}`,
},
};
}

updateProjectConfiguration(tree, projectName, projectConfig);
}

export function addAngularStorybookTask(tree: Tree, projectName: string) {
export function addAngularStorybookTask(
tree: Tree,
projectName: string,
configureTestRunner: boolean
) {
const projectConfig = readProjectConfiguration(tree, projectName);
const { ngBuildTarget } = findStorybookAndBuildTargetsAndCompiler(
projectConfig.targets
Expand All @@ -89,6 +106,7 @@ export function addAngularStorybookTask(tree: Tree, projectName: string) {
},
},
};

projectConfig.targets['build-storybook'] = {
executor: '@storybook/angular:build-storybook',
outputs: ['{options.outputDir}'],
Expand All @@ -107,6 +125,15 @@ export function addAngularStorybookTask(tree: Tree, projectName: string) {
},
};

if (configureTestRunner === true) {
projectConfig.targets['test-storybook'] = {
executor: 'nx:run-commands',
options: {
command: `test-storybook -c ${projectConfig.root}/.storybook --url=http://localhost:${DEFAULT_PORT}`,
},
};
}

updateProjectConfiguration(tree, projectName, projectConfig);
}

Expand Down
1 change: 1 addition & 0 deletions packages/storybook/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export const storybookReactNativeVersion = '^6.0.1-beta.5';
export const reactNativeStorybookLoader = '^2.0.5';
export const storybookSwcAddonVersion = '^1.1.7';
export const storybookNextAddonVersion = '^1.6.6';
export const storybookTestRunnerVersion = '^0.7.2';
export const litHtmlVersion = '^2.3.1';

1 comment on commit 2f91d96

@vercel
Copy link

@vercel vercel bot commented on 2f91d96 Sep 27, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.