-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinstall.ts
125 lines (101 loc) · 3.16 KB
/
install.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
import { HttpArchiveLog } from '@currents/cypress-debugger-support';
import {
ensureBrowserFlags,
install as installHarPlugin,
} from '@neuralegion/cypress-har-generator';
import path from 'path';
import {
browserLaunchHandler,
clearLogs,
getLogs,
recordLogs,
} from './browserLogs';
import { createDir, readFile, removeDir, removeFile, writeFile } from './fs';
import { sanitizeFilename, truncatePathParts } from './lib';
import { PluginOptions, TestExecutionResult } from './types';
const harDir = 'dump_har';
const createDumpFile = (data: TestExecutionResult, dumpDir: string): string => {
createDir(dumpDir);
const specDirPath = path.join(dumpDir, sanitizeFilename(data.meta.spec));
createDir(specDirPath);
const filename = `${data.meta.test.join(' -- ')} (${data.meta.state})${
data.meta.retryAttempt > 0 ? ` (attempt ${data.meta.retryAttempt + 1})` : ''
}`;
const resultsPath = path.join(
specDirPath,
`${sanitizeFilename(filename)}.json`
);
writeFile(truncatePathParts(resultsPath), JSON.stringify(data, null, 2));
return resultsPath;
};
const getHar = (filename: string): HttpArchiveLog | null => {
try {
const filePath = path.join(harDir, filename);
const data = readFile(filePath);
const parsed = JSON.parse(data.toString('utf-8'));
removeFile(filePath);
return parsed;
} catch (error) {
return null;
}
};
function install(
on: Cypress.PluginEvents,
config: Cypress.PluginConfigOptions,
options?: PluginOptions
) {
if (options?.failedTestsOnly) {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
config.env.__cypress_debugger_failedTestsOnly = true;
}
on('task', {
// called in "afterEach" hook
dumpEvents(
data: Pick<TestExecutionResult, 'id' | 'cy' | 'rr' | 'meta'> & {
harFilename: string;
}
) {
const har = getHar(data.harFilename);
const browserLogs = getLogs();
clearLogs();
const dumpData = {
id: data.id,
cy: data.cy,
rr: data.rr,
meta: data.meta,
har,
browserLogs,
pluginMeta: options?.meta,
};
const dumpDir =
options?.targetDirectory &&
path.resolve(options.targetDirectory) !== path.resolve(harDir)
? truncatePathParts(options.targetDirectory)
: 'dump';
const resultsFilePath = createDumpFile(dumpData, dumpDir);
if (options && options.callback) {
options.callback(resultsFilePath, dumpData);
}
return null;
},
// called in "after" hook
cleanup() {
removeDir(harDir);
return null;
},
});
// install cypress-har-generator
installHarPlugin(on);
on('before:browser:launch', (browser, launchOptions) => {
// cypress-har-generator uses this event, details here: https://github.com/NeuraLegion/cypress-har-generator/blob/master/README.md?plain=1#L74
ensureBrowserFlags(browser, launchOptions);
// use chrome debugging protocol to listen to console events
browserLaunchHandler(browser, launchOptions);
return launchOptions;
});
on('before:spec', async () => {
await recordLogs();
});
return config;
}
export default install;