UNPKG

@storybook/angular

Version:

Storybook for Angular: Develop Angular components in isolation with hot reloading.

217 lines (216 loc) • 9.08 kB
"use strict"; /* * @jest-environment node */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const architect_1 = require("@angular-devkit/architect"); const testing_1 = require("@angular-devkit/architect/testing"); const core_1 = require("@angular-devkit/core"); const path = __importStar(require("path")); const buildDevStandaloneMock = jest.fn(); const buildStaticStandaloneMock = jest.fn(); const buildMock = { buildDevStandalone: buildDevStandaloneMock, buildStaticStandalone: buildStaticStandaloneMock, withTelemetry: (name, options, fn) => fn(), }; jest.doMock('@storybook/core-server', () => buildMock); jest.doMock('@storybook/cli', () => ({ JsPackageManagerFactory: { getPackageManager: () => ({ runPackageCommand: mockRunScript, }), }, getEnvConfig: (options) => options, versions: { storybook: 'x.x.x', }, })); jest.doMock('find-up', () => ({ sync: () => './storybook/tsconfig.ts' })); const mockRunScript = jest.fn(); // Randomly fails on CI. TODO: investigate why // eslint-disable-next-line jest/no-disabled-tests describe.skip('Build Storybook Builder', () => { let architect; let architectHost; beforeEach(async () => { const registry = new core_1.schema.CoreSchemaRegistry(); registry.addPostTransform(core_1.schema.transforms.addUndefinedDefaults); architectHost = new testing_1.TestingArchitectHost(); architect = new architect_1.Architect(architectHost, registry); architectHost.addBuilder('@angular-devkit/build-angular:browser', (0, architect_1.createBuilder)(() => { return { success: true }; })); architectHost.addTarget({ project: 'angular-cli', target: 'build-2' }, '@angular-devkit/build-angular:browser', { outputPath: 'dist/angular-cli', index: 'src/index.html', main: 'src/main.ts', polyfills: 'src/polyfills.ts', tsConfig: 'src/tsconfig.app.json', assets: ['src/favicon.ico', 'src/assets'], styles: ['src/styles.css'], scripts: [], }); // This will either take a Node package name, or a path to the directory // for the package.json file. await architectHost.addBuilderFromPackage(path.join(__dirname, '../../..')); }); beforeEach(() => { buildStaticStandaloneMock.mockImplementation((_options) => Promise.resolve(_options)); }); afterEach(() => { jest.clearAllMocks(); }); it('should start storybook with angularBrowserTarget', async () => { const run = await architect.scheduleBuilder('@storybook/angular:build-storybook', { browserTarget: 'angular-cli:build-2', compodoc: false, }); const output = await run.result; await run.stop(); expect(output.success).toBeTruthy(); expect(mockRunScript).not.toHaveBeenCalledWith(); expect(buildStaticStandaloneMock).toHaveBeenCalledWith(expect.objectContaining({ angularBrowserTarget: 'angular-cli:build-2', angularBuilderContext: expect.any(Object), configDir: '.storybook', loglevel: undefined, quiet: false, disableTelemetry: undefined, outputDir: 'storybook-static', packageJson: expect.any(Object), mode: 'static', tsConfig: './storybook/tsconfig.ts', webpackStatsJson: false, })); }); it('should start storybook with tsConfig', async () => { const run = await architect.scheduleBuilder('@storybook/angular:build-storybook', { tsConfig: 'path/to/tsConfig.json', compodoc: false, }); const output = await run.result; await run.stop(); expect(output.success).toBeTruthy(); expect(mockRunScript).not.toHaveBeenCalledWith(); expect(buildStaticStandaloneMock).toHaveBeenCalledWith(expect.objectContaining({ angularBrowserTarget: null, angularBuilderContext: expect.any(Object), configDir: '.storybook', loglevel: undefined, quiet: false, disableTelemetry: undefined, outputDir: 'storybook-static', packageJson: expect.any(Object), mode: 'static', tsConfig: 'path/to/tsConfig.json', webpackStatsJson: false, })); }); it('should build storybook with webpack stats.json', async () => { const run = await architect.scheduleBuilder('@storybook/angular:build-storybook', { tsConfig: 'path/to/tsConfig.json', compodoc: false, webpackStatsJson: true, }); const output = await run.result; await run.stop(); expect(output.success).toBeTruthy(); expect(mockRunScript).not.toHaveBeenCalledWith(); expect(buildStaticStandaloneMock).toHaveBeenCalledWith(expect.objectContaining({ angularBrowserTarget: null, angularBuilderContext: expect.any(Object), configDir: '.storybook', loglevel: undefined, quiet: false, outputDir: 'storybook-static', packageJson: expect.any(Object), mode: 'static', tsConfig: 'path/to/tsConfig.json', webpackStatsJson: true, })); }); it('should throw error', async () => { buildStaticStandaloneMock.mockRejectedValue(true); const run = await architect.scheduleBuilder('@storybook/angular:build-storybook', { browserTarget: 'angular-cli:build-2', compodoc: false, }); try { await run.result; expect(false).toEqual('Throw expected'); } catch (error) { // eslint-disable-next-line jest/no-try-expect, jest/no-conditional-expect expect(error).toEqual('Broken build, fix the error above.\nYou may need to refresh the browser.'); } }); it('should run compodoc', async () => { const run = await architect.scheduleBuilder('@storybook/angular:build-storybook', { browserTarget: 'angular-cli:build-2', }); const output = await run.result; await run.stop(); expect(output.success).toBeTruthy(); expect(mockRunScript).toHaveBeenCalledWith('compodoc', ['-p', './storybook/tsconfig.ts', '-d', '.', '-e', 'json'], ''); expect(buildStaticStandaloneMock).toHaveBeenCalledWith(expect.objectContaining({ angularBrowserTarget: 'angular-cli:build-2', angularBuilderContext: expect.any(Object), configDir: '.storybook', loglevel: undefined, quiet: false, outputDir: 'storybook-static', packageJson: expect.any(Object), mode: 'static', tsConfig: './storybook/tsconfig.ts', webpackStatsJson: false, })); }); it('should start storybook with styles options', async () => { const run = await architect.scheduleBuilder('@storybook/angular:build-storybook', { tsConfig: 'path/to/tsConfig.json', compodoc: false, styles: ['style.scss'], }); const output = await run.result; await run.stop(); expect(output.success).toBeTruthy(); expect(mockRunScript).not.toHaveBeenCalledWith(); expect(buildStaticStandaloneMock).toHaveBeenCalledWith(expect.objectContaining({ angularBrowserTarget: null, angularBuilderContext: expect.any(Object), angularBuilderOptions: { assets: [], styles: ['style.scss'] }, configDir: '.storybook', loglevel: undefined, quiet: false, outputDir: 'storybook-static', packageJson: expect.any(Object), mode: 'static', tsConfig: 'path/to/tsConfig.json', webpackStatsJson: false, })); }); });