@storybook/angular
Version:
Storybook for Angular: Develop Angular components in isolation with hot reloading.
216 lines (215 loc) • 8.6 kB
JavaScript
"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: (_, __, fn) => fn(),
};
jest.doMock('@storybook/core-server', () => buildMock);
jest.doMock('find-up', () => ({ sync: () => './storybook/tsconfig.ts' }));
const mockRunScript = jest.fn();
jest.mock('@storybook/cli', () => ({
getEnvConfig: (options) => options,
versions: {
storybook: 'x.x.x',
},
JsPackageManagerFactory: {
getPackageManager: () => ({
runPackageCommand: mockRunScript,
}),
},
}));
// Randomly fails on CI. TODO: investigate why
// eslint-disable-next-line jest/no-disabled-tests
describe.skip('Start 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(() => {
buildDevStandaloneMock.mockImplementation((_options) => Promise.resolve(_options));
});
afterEach(() => {
jest.clearAllMocks();
});
it('should start storybook with angularBrowserTarget', async () => {
const run = await architect.scheduleBuilder('@storybook/angular:start-storybook', {
browserTarget: 'angular-cli:build-2',
port: 4400,
compodoc: false,
});
const output = await run.result;
await run.stop();
expect(output.success).toBeTruthy();
expect(mockRunScript).not.toHaveBeenCalledWith();
expect(buildDevStandaloneMock).toHaveBeenCalledWith(expect.objectContaining({
angularBrowserTarget: 'angular-cli:build-2',
angularBuilderContext: expect.any(Object),
ci: false,
configDir: '.storybook',
disableTelemetry: undefined,
host: 'localhost',
https: false,
packageJson: expect.any(Object),
port: 4400,
quiet: false,
smokeTest: false,
sslCa: undefined,
sslCert: undefined,
sslKey: undefined,
tsConfig: './storybook/tsconfig.ts',
}));
});
it('should start storybook with tsConfig', async () => {
const run = await architect.scheduleBuilder('@storybook/angular:start-storybook', {
tsConfig: 'path/to/tsConfig.json',
port: 4400,
compodoc: false,
});
const output = await run.result;
await run.stop();
expect(output.success).toBeTruthy();
expect(mockRunScript).not.toHaveBeenCalledWith();
expect(buildDevStandaloneMock).toHaveBeenCalledWith(expect.objectContaining({
angularBrowserTarget: null,
angularBuilderContext: expect.any(Object),
ci: false,
configDir: '.storybook',
disableTelemetry: undefined,
host: 'localhost',
https: false,
packageJson: expect.any(Object),
port: 4400,
quiet: false,
smokeTest: false,
sslCa: undefined,
sslCert: undefined,
sslKey: undefined,
tsConfig: 'path/to/tsConfig.json',
}));
});
it('should throw error', async () => {
buildDevStandaloneMock.mockRejectedValue(true);
const run = await architect.scheduleBuilder('@storybook/angular:start-storybook', {
browserTarget: 'angular-cli:build-2',
port: 4400,
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:start-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(buildDevStandaloneMock).toHaveBeenCalledWith(expect.objectContaining({
angularBrowserTarget: 'angular-cli:build-2',
angularBuilderContext: expect.any(Object),
ci: false,
disableTelemetry: undefined,
configDir: '.storybook',
host: 'localhost',
https: false,
packageJson: expect.any(Object),
port: 9009,
quiet: false,
smokeTest: false,
sslCa: undefined,
sslCert: undefined,
sslKey: undefined,
tsConfig: './storybook/tsconfig.ts',
}));
});
it('should start storybook with styles options', async () => {
const run = await architect.scheduleBuilder('@storybook/angular:start-storybook', {
tsConfig: 'path/to/tsConfig.json',
port: 4400,
compodoc: false,
styles: ['src/styles.css'],
});
const output = await run.result;
await run.stop();
expect(output.success).toBeTruthy();
expect(mockRunScript).not.toHaveBeenCalledWith();
expect(buildDevStandaloneMock).toHaveBeenCalledWith({
angularBrowserTarget: null,
angularBuilderContext: expect.any(Object),
angularBuilderOptions: { assets: [], styles: ['src/styles.css'] },
disableTelemetry: undefined,
ci: false,
configDir: '.storybook',
host: 'localhost',
https: false,
port: 4400,
packageJson: expect.any(Object),
quiet: false,
smokeTest: false,
sslCa: undefined,
sslCert: undefined,
sslKey: undefined,
tsConfig: 'path/to/tsConfig.json',
});
});
});