From 23d267dd061f5d9c36ef54c2dbfe53d4aeb675db Mon Sep 17 00:00:00 2001 From: katerina Date: Fri, 23 Jul 2021 14:23:11 +0300 Subject: [PATCH] fix(storybook): when migrating to 12.1 do not throw if project has no targets ISSUES CLOSED: #6128 --- .../fix-storybook-tsconfig.spec.ts | 97 ++++++++++++------- .../update-12-1-0/fix-storybook-tsconfig.ts | 4 +- 2 files changed, 65 insertions(+), 36 deletions(-) diff --git a/packages/storybook/src/migrations/update-12-1-0/fix-storybook-tsconfig.spec.ts b/packages/storybook/src/migrations/update-12-1-0/fix-storybook-tsconfig.spec.ts index afdf228263c17..a49ec4f275496 100644 --- a/packages/storybook/src/migrations/update-12-1-0/fix-storybook-tsconfig.spec.ts +++ b/packages/storybook/src/migrations/update-12-1-0/fix-storybook-tsconfig.spec.ts @@ -5,50 +5,79 @@ import updateStorybookTsconfig from './fix-storybook-tsconfig'; describe('Fix Storybook TSConfig to avoid VSCode error', () => { let tree: Tree; - beforeEach(async () => { - tree = createTreeWithEmptyWorkspace(); - - writeJson(tree, 'workspace.json', { - projects: { - ['home-ui-react']: { - projectType: 'library', - root: 'libs/home/ui-react', - sourceRoot: 'libs/home/ui-react/src', - targets: { - storybook: { - builder: '@nrwl/storybook:storybook', - options: { - uiFramework: '@storybook/react', - port: 4400, - config: { - configFolder: 'libs/home/ui-react/.storybook', + describe('when project has valid configuration and targets', () => { + beforeEach(async () => { + tree = createTreeWithEmptyWorkspace(); + + writeJson(tree, 'workspace.json', { + projects: { + ['home-ui-react']: { + projectType: 'library', + root: 'libs/home/ui-react', + sourceRoot: 'libs/home/ui-react/src', + targets: { + storybook: { + builder: '@nrwl/storybook:storybook', + options: { + uiFramework: '@storybook/react', + port: 4400, + config: { + configFolder: 'libs/home/ui-react/.storybook', + }, }, }, }, }, }, - }, + }); + + writeJson(tree, 'libs/home/ui-react/.storybook/tsconfig.json', { + extends: '../tsconfig.json', + compilerOptions: { + emitDecoratorMetadata: true, + }, + }); }); - writeJson(tree, 'libs/home/ui-react/.storybook/tsconfig.json', { - extends: '../tsconfig.json', - compilerOptions: { - emitDecoratorMetadata: true, - }, + it(`should add outDir to the storybook tsconfig to avoid VSCode errors`, async () => { + await updateStorybookTsconfig(tree); + + expect( + readJson(tree, 'libs/home/ui-react/.storybook/tsconfig.json') + ).toMatchObject({ + extends: '../tsconfig.json', + compilerOptions: { + emitDecoratorMetadata: true, + outDir: '', + }, + }); }); }); - it(`should add outDir to the storybook tsconfig to avoid VSCode errors`, async () => { - await updateStorybookTsconfig(tree); - - expect( - readJson(tree, 'libs/home/ui-react/.storybook/tsconfig.json') - ).toMatchObject({ - extends: '../tsconfig.json', - compilerOptions: { - emitDecoratorMetadata: true, - outDir: '', - }, + describe('when project has no targets', () => { + beforeEach(async () => { + tree = createTreeWithEmptyWorkspace(); + + writeJson(tree, 'workspace.json', { + projects: { + ['home-ui-react']: { + projectType: 'library', + root: 'libs/home/ui-react', + sourceRoot: 'libs/home/ui-react/src', + }, + }, + }); + + writeJson(tree, 'libs/home/ui-react/.storybook/tsconfig.json', { + extends: '../tsconfig.json', + compilerOptions: { + emitDecoratorMetadata: true, + }, + }); + }); + + it(`should not throw errors`, async () => { + await expect(updateStorybookTsconfig(tree)).resolves.not.toThrow(); }); }); }); diff --git a/packages/storybook/src/migrations/update-12-1-0/fix-storybook-tsconfig.ts b/packages/storybook/src/migrations/update-12-1-0/fix-storybook-tsconfig.ts index b7cbac98da781..90dd905878852 100644 --- a/packages/storybook/src/migrations/update-12-1-0/fix-storybook-tsconfig.ts +++ b/packages/storybook/src/migrations/update-12-1-0/fix-storybook-tsconfig.ts @@ -23,7 +23,7 @@ export default async function updateStorybookTsconfig(tree: Tree) { ), }; - const storybookExecutor = Object.keys(targets).find( + const storybookExecutor = Object.keys(targets || {}).find( (x) => targets[x].executor === '@nrwl/storybook:storybook' ); @@ -38,7 +38,7 @@ export default async function updateStorybookTsconfig(tree: Tree) { } const isReactProject = isFramework('react', { - uiFramework: targets[storybookExecutor].options + uiFramework: targets[storybookExecutor]?.options ?.uiFramework as Parameters[1]['uiFramework'], });