/
index.spec.ts
193 lines (164 loc) · 5.67 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Architect, createBuilder } from '@angular-devkit/architect';
import { TestingArchitectHost } from '@angular-devkit/architect/testing';
import { schema } from '@angular-devkit/core';
import * as path from 'path';
const buildStandaloneMock = jest.fn();
jest.doMock('@storybook/angular/standalone', () => buildStandaloneMock);
jest.doMock('find-up', () => ({ sync: () => './storybook/tsconfig.ts' }));
const cpSpawnMock = {
spawn: jest.fn(),
};
jest.doMock('child_process', () => cpSpawnMock);
describe('Build Storybook Builder', () => {
let architect: Architect;
let architectHost: TestingArchitectHost;
beforeEach(async () => {
const registry = new schema.CoreSchemaRegistry();
registry.addPostTransform(schema.transforms.addUndefinedDefaults);
architectHost = new TestingArchitectHost();
architect = new Architect(architectHost, registry);
architectHost.addBuilder(
'@angular-devkit/build-angular:browser',
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(() => {
buildStandaloneMock.mockImplementation((_options: unknown) => Promise.resolve());
cpSpawnMock.spawn.mockImplementation(() => ({
stdout: { on: () => {} },
stderr: { on: () => {} },
on: (_event: string, cb: any) => cb(0),
}));
});
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(cpSpawnMock.spawn).not.toHaveBeenCalledWith();
expect(buildStandaloneMock).toHaveBeenCalledWith({
angularBrowserTarget: 'angular-cli:build-2',
angularBuilderContext: expect.any(Object),
angularBuilderOptions: {},
configDir: '.storybook',
loglevel: undefined,
quiet: false,
outputDir: 'storybook-static',
mode: 'static',
tsConfig: './storybook/tsconfig.ts',
});
});
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(cpSpawnMock.spawn).not.toHaveBeenCalledWith();
expect(buildStandaloneMock).toHaveBeenCalledWith({
angularBrowserTarget: null,
angularBuilderContext: expect.any(Object),
angularBuilderOptions: {},
configDir: '.storybook',
loglevel: undefined,
quiet: false,
outputDir: 'storybook-static',
mode: 'static',
tsConfig: 'path/to/tsConfig.json',
});
});
it('should throw error', async () => {
buildStandaloneMock.mockRejectedValue(new Error());
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
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(cpSpawnMock.spawn).toHaveBeenCalledWith(
'npx',
['compodoc', '-p', './storybook/tsconfig.ts', '-d', '', '-e', 'json'],
{
cwd: '',
env: process.env,
shell: true,
}
);
expect(buildStandaloneMock).toHaveBeenCalledWith({
angularBrowserTarget: 'angular-cli:build-2',
angularBuilderContext: expect.any(Object),
angularBuilderOptions: {},
configDir: '.storybook',
loglevel: undefined,
quiet: false,
outputDir: 'storybook-static',
mode: 'static',
tsConfig: './storybook/tsconfig.ts',
});
});
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(cpSpawnMock.spawn).not.toHaveBeenCalledWith();
expect(buildStandaloneMock).toHaveBeenCalledWith({
angularBrowserTarget: null,
angularBuilderContext: expect.any(Object),
angularBuilderOptions: { styles: ['style.scss'] },
configDir: '.storybook',
loglevel: undefined,
quiet: false,
outputDir: 'storybook-static',
mode: 'static',
tsConfig: 'path/to/tsConfig.json',
});
});
});