-
Notifications
You must be signed in to change notification settings - Fork 0
/
istanbul.js
52 lines (45 loc) · 1.81 KB
/
istanbul.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
// https://github.com/cypress-io/cypress/issues/346#issuecomment-365220178
// https://github.com/cypress-io/cypress/issues/346#issuecomment-368832585
const istanbul = require('istanbul-lib-coverage');
const map = istanbul.createCoverageMap({});
const coverageFolder = Cypress.config('coverageFolder');
const coverageFile = `${ coverageFolder }/out-${Date.now()}.json`;
Cypress.on('window:before:unload', e => {
const coverage = e.currentTarget.__coverage__;
if (coverage) {
map.merge(coverage);
}
});
after(() => {
cy.window().then(win => {
const specWin = win.parent.document.querySelector('iframe[id~="Spec:"]').contentWindow;
const unitCoverage = specWin.__coverage__;
const coverage = win.__coverage__;
if (unitCoverage) {
map.merge(unitCoverage);
}
if (coverage) {
map.merge(coverage);
}
cy.writeFile(coverageFile, JSON.stringify(map));
cy.exec('npx nyc report --reporter=html -t=coverage')
cy.exec('npm run coverage')
.then(coverage => {
// output coverage report
const out = coverage.stdout
// 替换bash红色标识符
.replace(/\[31;1m/g, '')
.replace(/\[0m/g, '')
// 替换粗体标识符
.replace(/\[3[23];1m/g, '');
console.log(out);
})
.then(() => {
// output html file link to current test report
const link = Cypress.spec.absolute
.replace(Cypress.spec.relative, `${coverageFolder}/${Cypress.spec.relative}`)
.replace('cypress.spec.', '');
console.log(`check coverage detail: file://${link}.html`);
});
});
});