generated from storybookjs/addon-kit
/
test-storybook.js
executable file
Β·281 lines (222 loc) Β· 8.68 KB
/
test-storybook.js
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
#!/usr/bin/env node
//@ts-check
'use strict';
const { execSync } = require('child_process');
const fetch = require('node-fetch');
const canBindToHost = require('can-bind-to-host').default;
const fs = require('fs');
const dedent = require('ts-dedent').default;
const path = require('path');
const tempy = require('tempy');
const semver = require('semver');
const { getCliOptions, getStorybookMetadata } = require('../dist/cjs/util');
const { transformPlaywrightJson } = require('../dist/cjs/playwright/transformPlaywrightJson');
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'test';
process.env.NODE_ENV = 'test';
process.env.PUBLIC_URL = '';
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', (err) => {
throw err;
});
const log = (message) => console.log(`[test-storybook] ${message}`);
const error = (message) => console.error(`[test-storybook] ${message}`);
// Clean up tmp files globally in case of control-c
let indexTmpDir;
const cleanup = () => {
if (indexTmpDir) {
log(`Cleaning up ${indexTmpDir}`);
fs.rmSync(indexTmpDir, { recursive: true, force: true });
}
};
let isWatchMode = false;
async function reportCoverage() {
if (isWatchMode || process.env.STORYBOOK_COLLECT_COVERAGE !== 'true') {
return;
}
const coverageFolderE2E = path.resolve(process.cwd(), '.nyc_output');
const coverageFolder = path.resolve(process.cwd(), 'coverage/storybook');
// in case something goes wrong and .nyc_output does not exist, bail
if (!fs.existsSync(coverageFolderE2E)) {
return;
}
// if there's no coverage folder, create one
if (!fs.existsSync(coverageFolder)) {
fs.mkdirSync(coverageFolder, { recursive: true });
}
// move the coverage files from .nyc_output folder (coming from jest-playwright) to coverage, then delete .nyc_output
fs.renameSync(`${coverageFolderE2E}/coverage.json`, `${coverageFolder}/coverage-storybook.json`);
fs.rmSync(coverageFolderE2E, { recursive: true });
// --skip-full in case we only want to show not fully covered code
// --check-coverage if we want to break if coverage reaches certain threshold
// .nycrc will be respected for thresholds etc. https://www.npmjs.com/package/nyc#coverage-thresholds
execSync(`npx nyc report --reporter=text -t ${coverageFolder} --report-dir ${coverageFolder}`, {
stdio: 'inherit',
});
}
const onProcessEnd = () => {
cleanup();
reportCoverage();
};
process.on('SIGINT', onProcessEnd);
process.on('exit', onProcessEnd);
function sanitizeURL(url) {
let finalURL = url;
// prepend URL protocol if not there
if (finalURL.indexOf('http://') === -1 && finalURL.indexOf('https://') === -1) {
finalURL = 'http://' + finalURL;
}
// remove iframe.html if present
finalURL = finalURL.replace(/iframe.html\s*$/, '');
// remove index.html if present
finalURL = finalURL.replace(/index.html\s*$/, '');
// add forward slash at the end if not there
if (finalURL.slice(-1) !== '/') {
finalURL = finalURL + '/';
}
return finalURL;
}
const checkForIncompatibilities = () => {
try {
const jestVersion = require('jest/package.json').version;
if (semver.gte(jestVersion, '28.0.0')) {
error(dedent`We detected that your project is using Jest 28.0.0 or higher, which is currently incompatible with the test runner.
You can find more info at: https://github.com/storybookjs/test-runner#errors-with-jest-28
`);
process.exit(1);
}
} catch (err) {
error(
'We detected that Jest is not installed in your project. Please install Jest@27 and run test-storybook again.'
);
process.exit(1);
}
};
async function executeJestPlaywright(args) {
const jest = require('jest');
let argv = args.slice(2);
const jestConfigPath = fs.existsSync('test-runner-jest.config.js')
? 'test-runner-jest.config.js'
: path.resolve(__dirname, '../playwright/test-runner-jest.config.js');
argv.push('--config', jestConfigPath);
await jest.run(argv);
}
async function checkStorybook(url) {
try {
const res = await fetch(url, { method: 'HEAD' });
if (res.status !== 200) throw new Error(`Unxpected status: ${res.status}`);
} catch (e) {
console.error(
dedent`[test-storybook] It seems that your Storybook instance is not running at: ${url}. Are you sure it's running?
If you're not running Storybook on the default 6006 port or want to run the tests against any custom URL, you can pass the --url flag like so:
yarn test-storybook --url http://localhost:9009
More info at https://github.com/storybookjs/test-runner#getting-started`
);
process.exit(1);
}
}
async function getIndexJson(url) {
const indexJsonUrl = new URL('index.json', url).toString();
const storiesJsonUrl = new URL('stories.json', url).toString();
const [indexRes, storiesRes] = await Promise.all([fetch(indexJsonUrl), fetch(storiesJsonUrl)]);
if (indexRes.ok) {
try {
const json = await indexRes.text();
return JSON.parse(json);
} catch (err) {}
}
if (storiesRes.ok) {
try {
const json = await storiesRes.text();
return JSON.parse(json);
} catch (err) {}
}
throw new Error(dedent`
Failed to fetch index data from the project.
Make sure that either of these URLs are available with valid data in your Storybook:
${
// TODO: switch order once index.json becomes more common than stories.json
storiesJsonUrl
}
${indexJsonUrl}
More info: https://github.com/storybookjs/test-runner/blob/main/README.md#indexjson-mode
`);
}
async function getIndexTempDir(url) {
let tmpDir;
try {
const indexJson = await getIndexJson(url);
const titleIdToTest = transformPlaywrightJson(indexJson);
tmpDir = tempy.directory();
Object.entries(titleIdToTest).forEach(([titleId, test]) => {
const tmpFile = path.join(tmpDir, `${titleId}.test.js`);
fs.writeFileSync(tmpFile, test);
});
} catch (err) {
error(err);
process.exit(1);
}
return tmpDir;
}
function ejectConfiguration() {
const origin = path.resolve(__dirname, '../playwright/test-runner-jest.config.js');
const destination = path.resolve('test-runner-jest.config.js');
const fileAlreadyExists = fs.existsSync(destination);
if (fileAlreadyExists) {
throw new Error(dedent`Found existing file at:
${destination}
Please delete it and rerun this command.
\n`);
}
fs.copyFileSync(origin, destination);
log('Configuration file successfully copied as test-runner-jest.config.js');
}
const main = async () => {
checkForIncompatibilities();
const { jestOptions, runnerOptions } = getCliOptions();
if (runnerOptions.eject) {
ejectConfiguration();
process.exit(0);
}
// set this flag to skip reporting coverage in watch mode
isWatchMode = jestOptions.watch || jestOptions.watchAll;
const rawTargetURL = process.env.TARGET_URL || runnerOptions.url || 'http://localhost:6006';
await checkStorybook(rawTargetURL);
const targetURL = sanitizeURL(rawTargetURL);
process.env.TARGET_URL = targetURL;
if (runnerOptions.coverage) {
process.env.STORYBOOK_COLLECT_COVERAGE = 'true';
}
if (process.env.REFERENCE_URL) {
process.env.REFERENCE_URL = sanitizeURL(process.env.REFERENCE_URL);
}
// Use TEST_BROWSERS if set, otherwise get from --browser option
if (!process.env.TEST_BROWSERS && runnerOptions.browsers) {
process.env.TEST_BROWSERS = runnerOptions.browsers.join(',');
}
const { hostname } = new URL(targetURL);
const isLocalStorybookIp = await canBindToHost(hostname);
const shouldRunIndexJson = runnerOptions.indexJson !== false && !isLocalStorybookIp;
if (shouldRunIndexJson) {
log(
'Detected a remote Storybook URL, running in index json mode. To disable this, run the command again with --no-index-json\n'
);
}
if (runnerOptions.indexJson || shouldRunIndexJson) {
indexTmpDir = await getIndexTempDir(targetURL);
process.env.TEST_ROOT = indexTmpDir;
process.env.TEST_MATCH = '**/*.test.js';
}
process.env.STORYBOOK_CONFIG_DIR = runnerOptions.configDir;
const { storiesPaths, lazyCompilation } = getStorybookMetadata();
process.env.STORYBOOK_STORIES_PATTERN = storiesPaths;
if (lazyCompilation && isLocalStorybookIp) {
log(
`You're running Storybook with lazy compilation enabled, and will likely cause issues with the test runner locally. Consider disabling 'lazyCompilation' in ${runnerOptions.configDir}/main.js when running 'test-storybook' locally.`
);
}
await executeJestPlaywright(jestOptions);
};
main().catch((e) => log(e));