forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-e2e.ts
350 lines (298 loc) · 10.1 KB
/
run-e2e.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import path from 'path';
import { ensureDir, pathExists, remove } from 'fs-extra';
import prompts from 'prompts';
import program from 'commander';
import { readConfig, writeConfig } from '@storybook/csf-tools';
import { getInterpretedFile } from '@storybook/core-common';
import { serve } from './utils/serve';
// @ts-ignore
import { filterDataForCurrentCircleCINode } from './utils/concurrency';
import * as configs from '../lib/cli/src/repro-generators/configs';
import { Parameters } from '../lib/cli/src/repro-generators/configs';
import { exec } from '../lib/cli/src/repro-generators/scripts';
const logger = console;
export interface Options {
/** CLI repro template to use */
name: string;
mainOverrides?: Parameters['mainOverrides'];
/** Pre-build hook */
ensureDir?: boolean;
cwd?: string;
}
const rootDir = path.join(__dirname, '..');
const siblingDir = path.join(__dirname, '..', '..', 'storybook-e2e-testing');
const prepareDirectory = async ({ cwd }: Options): Promise<boolean> => {
if (!(await pathExists(siblingDir))) {
await ensureDir(siblingDir);
}
return pathExists(cwd);
};
const cleanDirectory = async ({ cwd }: Options): Promise<void> => {
await remove(cwd);
};
const overrideMainConfig = async ({ cwd, mainOverrides }: Options) => {
logger.info(`📝 Overwriting main.js with the following configuration:`);
const configDir = path.join(cwd, '.storybook');
const mainConfigPath = getInterpretedFile(path.resolve(configDir, 'main'));
logger.debug(mainOverrides);
const mainConfig = await readConfig(mainConfigPath);
Object.keys(mainOverrides).forEach((field) => {
mainConfig.setFieldValue([field], mainOverrides[field]);
});
await writeConfig(mainConfig);
};
const buildStorybook = async ({ cwd }: Options) => {
await exec(
`yarn build-storybook --quiet`,
{ cwd },
{ startMessage: `👷 Building Storybook`, errorMessage: `🚨 Storybook build failed` }
);
};
const serveStorybook = async ({ cwd }: Options, port: string) => {
const staticDirectory = path.join(cwd, 'storybook-static');
logger.info(`🌍 Serving ${staticDirectory} on http://localhost:${port}`);
return serve(staticDirectory, port);
};
const runCypress = async (location: string, name: string) => {
const cypressCommand = openCypressInUIMode ? 'open' : 'run';
await exec(
`CYPRESS_ENVIRONMENT=${name} yarn cypress ${cypressCommand} --config pageLoadTimeout=4000,execTimeout=4000,taskTimeout=4000,responseTimeout=4000,defaultCommandTimeout=4000,integrationFolder="cypress/generated",videosFolder="/tmp/cypress-record/${name}" --env location="${location}"`,
{ cwd: rootDir },
{
startMessage: `🤖 Running Cypress tests`,
errorMessage: `🚨 E2E tests fails`,
}
);
};
const runStorybookTestRunner = async (options: Options) => {
const viewMode = runTestsInDocsMode ? 'docs' : 'story';
await exec(
`VIEW_MODE=${viewMode} yarn test-storybook --url http://localhost:4000`,
{ cwd: options.cwd },
{
startMessage: `🤖 Running Storybook tests`,
errorMessage: `🚨 Storybook tests fails`,
}
);
};
const runTests = async ({ name, ...rest }: Parameters) => {
const options = {
name,
...rest,
cwd: path.join(siblingDir, `${name}`),
};
logger.log();
logger.info(`🏃️Starting for ${name}`);
logger.log();
logger.debug(options);
logger.log();
if (!(await prepareDirectory(options))) {
// Call repro cli
const sbCLICommand = useLocalSbCli
? `node ${__dirname}/../lib/cli/bin/index.js repro`
: // Need to use npx because at this time we don't have Yarn 2 installed
'npx -p @storybook/cli sb repro';
const targetFolder = path.join(siblingDir, `${name}`);
const commandArgs = [
targetFolder,
`--framework ${options.framework}`,
`--template ${options.name}`,
'--e2e',
];
if (pnp) {
commandArgs.push('--pnp');
}
const command = `${sbCLICommand} ${commandArgs.join(' ')}`;
await exec(
command,
{ cwd: siblingDir },
{
startMessage: `👷 Bootstrapping ${options.framework} project`,
errorMessage: `🚨 Unable to bootstrap project`,
}
);
if (options.mainOverrides) {
await overrideMainConfig(options);
}
await buildStorybook(options);
logger.log();
}
const server = await serveStorybook(options, '4000');
logger.log();
try {
if (shouldUseTestRunner) {
await runStorybookTestRunner(options);
} else {
await runCypress('http://localhost:4000', name);
}
logger.info(`🎉 Storybook is working great with ${name}!`);
} catch (e) {
logger.info(`🥺 Storybook has some issues with ${name}!`);
throw e;
} finally {
server.close();
}
};
async function postE2ECleanup(cwd: string, parameters: Parameters) {
if (!process.env.CI) {
const { cleanup } = await prompts({
type: 'toggle',
name: 'cleanup',
message: 'Should perform cleanup?',
initial: false,
active: 'yes',
inactive: 'no',
});
if (cleanup) {
logger.log();
logger.info(`🗑 Cleaning ${cwd}`);
await cleanDirectory({ ...parameters, cwd });
} else {
logger.log();
logger.info(`🚯 No cleanup happened: ${cwd}`);
}
}
}
async function preE2ECleanup(name: string, parameters: Parameters, cwd: string) {
if (startWithCleanSlate) {
logger.log();
logger.info(`♻️ Starting with a clean slate, removing existing ${name} folder`);
await cleanDirectory({ ...parameters, cwd });
}
}
/**
* Execute E2E for input parameters and return true is everything is ok, false
* otherwise.
* @param parameters
*/
const runE2E = async (parameters: Parameters): Promise<boolean> => {
const { name } = parameters;
const cwd = path.join(siblingDir, `${name}`);
return preE2ECleanup(name, parameters, cwd)
.then(() => runTests(parameters))
.then(() => postE2ECleanup(cwd, parameters))
.then(() => true)
.catch((e) => {
logger.error(`🛑 an error occurred:`);
logger.error(e);
logger.log();
process.exitCode = 1;
return false;
});
};
program
.option('--clean', 'Clean up existing projects before running the tests', false)
.option('--pnp', 'Run tests using Yarn 2 PnP instead of Yarn 1 + npx', false)
.option(
'--use-local-sb-cli',
'Run tests using local @storybook/cli package (⚠️ Be sure @storybook/cli is properly built as it will not be rebuilt before running the tests)',
false
)
.option(
'--skip <value>',
'Skip a framework, can be used multiple times "--skip angular@latest --skip preact"',
(value, previous) => previous.concat([value]),
[]
)
.option('--test-runner', 'Run Storybook test runner instead of cypress', false)
.option('--docs-mode', 'Run Storybook test runner in docs mode', false)
.option('--all', `run e2e tests for every framework`, false);
program.parse(process.argv);
type ProgramOptions = {
all?: boolean;
pnp?: boolean;
useLocalSbCli?: boolean;
clean?: boolean;
args?: string[];
skip?: string[];
testRunner?: boolean;
docsMode?: boolean;
};
const {
all: shouldRunAllFrameworks,
args: frameworkArgs,
skip: frameworksToSkip,
testRunner: shouldUseTestRunner,
docsMode: runTestsInDocsMode,
}: ProgramOptions = program;
let { pnp, useLocalSbCli, clean: startWithCleanSlate }: ProgramOptions = program;
const typedConfigs: { [key: string]: Parameters } = configs;
let openCypressInUIMode = !process.env.CI;
const getConfig = async (): Promise<Parameters[]> => {
let e2eConfigsToRun = Object.values(typedConfigs);
if (shouldRunAllFrameworks) {
// Nothing to do here
} else if (frameworkArgs.length > 0) {
e2eConfigsToRun = e2eConfigsToRun.filter((config) => frameworkArgs.includes(config.name));
} else if (!process.env.CI) {
const selectedValues = await prompts([
{
type: 'toggle',
name: 'openCypressInUIMode',
message: 'Open cypress in UI mode',
initial: false,
active: 'yes',
inactive: 'no',
},
{
type: 'toggle',
name: 'useLocalSbCli',
message: 'Use local Storybook CLI',
initial: false,
active: 'yes',
inactive: 'no',
},
{
type: 'autocompleteMultiselect',
message: 'Select the frameworks to run',
name: 'frameworks',
min: 1,
hint: 'You can also run directly with package name like `test:e2e-framework react`, or `yarn test:e2e-framework --all` for all packages!',
choices: Object.keys(configs).map((key) => {
// @ts-ignore
const { name, version } = configs[key];
return {
// @ts-ignore
value: configs[key],
title: `${name}@${version}`,
selected: false,
};
}),
},
]);
if (!selectedValues.frameworks) {
logger.info(`No framework was selected.`);
process.exit(process.exitCode || 0);
}
useLocalSbCli = selectedValues.useLocalSbCli;
openCypressInUIMode = selectedValues.openCypressInUIMode;
e2eConfigsToRun = selectedValues.frameworks;
}
// Remove frameworks listed with `--skip` arg
e2eConfigsToRun = e2eConfigsToRun.filter((config) => !frameworksToSkip.includes(config.name));
return e2eConfigsToRun;
};
const perform = async (): Promise<Record<string, boolean>> => {
const narrowedConfigs: Parameters[] = await getConfig();
const list = filterDataForCurrentCircleCINode(narrowedConfigs) as Parameters[];
logger.info(`📑 Will run E2E tests for:${list.map((c) => `${c.name}`).join(', ')}`);
const e2eResult: Record<string, boolean> = {};
// Run all e2e tests one after another and fill result map
await list.reduce(
(previousValue, config) =>
previousValue
.then(() => runE2E(config))
.then((result) => {
e2eResult[config.name] = result;
}),
Promise.resolve()
);
return e2eResult;
};
perform().then((e2eResult) => {
logger.info(`🧮 E2E Results`);
Object.entries(e2eResult).forEach(([configName, result]) => {
logger.info(`${configName}: ${result ? 'OK' : 'KO'}`);
});
process.exit(process.exitCode || 0);
});