|
1 |
| -// the script allows us to support typescript in cypress test |
2 |
| -const wp = require("@cypress/webpack-preprocessor"); |
3 |
| -const istanbul = require("istanbul-lib-coverage"); |
4 |
| -const {join} = require("path"); |
5 |
| -const url = require('url'); |
6 |
| -const webpackOptions = require('../../build/cypress.config'); |
7 |
| -const {existsSync, mkdirSync, readFileSync, writeFileSync} = require("fs"); |
8 |
| -const execa = require("execa"); |
9 |
| -const debug = require("debug")("code-coverage"); |
10 |
| - |
11 |
| -/** |
12 |
| - * Replace source-map's path by the corresponding absolute file path (coverage report wouldn't work |
13 |
| - * with source-map path being relative or containing Webpack loaders and query parameters) |
14 |
| - */ |
15 |
| -function fixToAbsoluteSourcePaths(coverage) { |
16 |
| - Object.values(coverage).forEach((file) => { |
17 |
| - const {path: absolutePath, inputSourceMap} = file; |
18 |
| - const fileName = (/([^\/\\]+)$/).exec(absolutePath)[1]; |
19 |
| - if (!inputSourceMap || !fileName) { |
20 |
| - return; |
21 |
| - } |
22 |
| - |
23 |
| - if (inputSourceMap.sourceRoot) { |
24 |
| - inputSourceMap.sourceRoot = ""; |
25 |
| - } |
26 |
| - inputSourceMap.sources = inputSourceMap.sources.map((source) => (source.includes(fileName) ? absolutePath : source)); |
27 |
| - }); |
28 |
| -} |
29 |
| - |
30 |
| -// These are standard folder and file names used by NYC tools |
31 |
| -const processWorkingDirectory = process.cwd(); |
32 |
| -const outputFolder =require('./../../.nycrc.json')['temp-dir']; |
33 |
| -const coverageFolder = join(processWorkingDirectory, outputFolder); |
34 |
| -const nycFilename = join(coverageFolder, "out.json"); |
35 |
| - |
36 |
| - |
37 |
| -function saveCoverage(coverage) { |
38 |
| - if (!existsSync(coverageFolder)) { |
39 |
| - mkdirSync(coverageFolder, {recursive: true}); |
40 |
| - debug("created folder %s for output coverage", coverageFolder); |
41 |
| - } |
42 |
| - |
43 |
| - writeFileSync(nycFilename, JSON.stringify(coverage, null, 2)); |
44 |
| -} |
45 |
| - |
46 |
| -let pact; |
47 | 1 | module.exports = (on, config) => {
|
48 |
| - /** |
49 |
| - * on("task") method allows to create tasks in cypress. |
50 |
| - * Please check https://docs.cypress.io/api/commands/task.html#Syntax |
51 |
| - * the code is copied from require('@cypress/code-coverage/task')) |
52 |
| - * in order to remove hardcoded configurations like outputFolder, reportDirectory, reporter, |
53 |
| - **/ |
54 |
| - on("task", { |
55 |
| - /** |
56 |
| - * Clears accumulated code coverage information. |
57 |
| - * |
58 |
| - * Interactive mode with "cypress open" |
59 |
| - * - running a single spec or "Run all specs" needs to reset coverage |
60 |
| - * Headless mode with "cypress run" |
61 |
| - * - runs EACH spec separately, so we cannot reset the coverage |
62 |
| - * or we will lose the coverage from previous specs. |
63 |
| - */ |
64 |
| - resetCoverage({isInteractive}) { |
65 |
| - if (isInteractive) { |
66 |
| - debug("reset code coverage in interactive mode"); |
67 |
| - const coverageMap = istanbul.createCoverageMap({}); |
68 |
| - saveCoverage(coverageMap); |
69 |
| - } |
70 |
| - |
71 |
| - /* |
72 |
| - * Else in headless mode, assume the coverage file was deleted before the `cypress run` command was called |
73 |
| - * example: rm -rf .nyc_output || true |
74 |
| - */ |
75 |
| - return null; |
76 |
| - }, |
77 |
| - |
78 |
| - /** |
79 |
| - * Combines coverage information from single test with previously collected coverage. |
80 |
| - * |
81 |
| - * @param {string} sentCoverage Stringified coverage object sent by the test runner |
82 |
| - * @returns {null} Nothing is returned from this task |
83 |
| - */ |
84 |
| - combineCoverage(sentCoverage) { |
85 |
| - const coverage = JSON.parse(sentCoverage); |
86 |
| - debug("parsed sent coverage"); |
87 |
| - |
88 |
| - fixToAbsoluteSourcePaths(coverage); |
89 |
| - const previous = existsSync(nycFilename) |
90 |
| - ? JSON.parse(readFileSync(nycFilename)) |
91 |
| - : istanbul.createCoverageMap({}); |
92 |
| - const coverageMap = istanbul.createCoverageMap(previous); |
93 |
| - coverageMap.merge(coverage); |
94 |
| - saveCoverage(coverageMap); |
95 |
| - debug("wrote coverage file %s", nycFilename); |
96 |
| - |
97 |
| - return null; |
98 |
| - }, |
99 |
| - |
100 |
| - /** |
101 |
| - * Saves coverage information as a JSON file and calls NPM script to generate HTML report |
102 |
| - */ |
103 |
| - coverageReport() { |
104 |
| - if (!existsSync(nycFilename)) { |
105 |
| - console.warn("Cannot find coverage file %s", nycFilename); |
106 |
| - console.warn("Skipping coverage report"); |
107 |
| - return null; |
108 |
| - } |
109 |
| - |
110 |
| - const nycrc =require('./../../.nycrc.json'); |
111 |
| - const reportDir = nycrc['report-dir']; |
112 |
| - const reporter = nycrc.reporter; |
113 |
| - const reporters = Array.isArray(reporter) |
114 |
| - ? reporter.map((name) => `--reporter=${name}`) |
115 |
| - : `--reporter=${reporter}`; |
116 |
| - |
117 |
| - // Should we generate report via NYC module API? |
118 |
| - const command = "nyc"; |
119 |
| - const args = [ |
120 |
| - "report", |
121 |
| - "--report-dir", |
122 |
| - reportDir, |
123 |
| - "--temp-dir", |
124 |
| - coverageFolder, |
125 |
| - ].concat(reporters); |
126 |
| - debug( |
127 |
| - "saving coverage report using command: \"%s %s\"", |
128 |
| - command, |
129 |
| - args.join(" "), |
130 |
| - ); |
131 |
| - debug("current working directory is %s", process.cwd()); |
132 |
| - return execa(command, args, {stdio: "inherit"}); |
133 |
| - }, |
134 |
| - }); |
135 |
| - |
136 |
| - on("file:preprocessor", wp({webpackOptions})); |
137 |
| -}; |
| 2 | + require('@cypress/code-coverage/task')(on, config); |
| 3 | + return config |
| 4 | +} |
0 commit comments