Skip to content

Commit 7d11d12

Browse files
committed
Fixed broken coverage, since it was broken. After updating to @cypress/code-coverage 3.x they seem to fixed most hardoded things, but temp-dir in .nycrc.json, so I forked their repo and added pr cypress-io/code-coverage#332 so it can be updated in package.json on next release if this happens
tsconfig.json should not have 'sourceMap', seems like cypress adds 'inlineSourceMap' on top of my config which causes `TS5053: Option 'sourceMap' cannot be specified with option 'inlineSourceMap'.` If in future adding this option doesn't cause `yarn test:cypress:run` to fail, it can be restored
1 parent 44fef92 commit 7d11d12

File tree

7 files changed

+569
-253
lines changed

7 files changed

+569
-253
lines changed

.github/workflows/main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ jobs:
5151
steps:
5252
- name: coverage
5353
run: yarn run test:coverage
54+
- name: eslint
55+
run: yarn run lint

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ import {defaultModule} from "@/store/default"; // this is a single import with a
463463
- if build process is killed or get stack that could be because of out of memory. The only option is to build frontend on machine with more memory and copy files
464464

465465
## TODO
466+
- https://github.com/cypress-io/code-coverage/pull/332
466467
- ![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/akoidan/vue-webpack-typescript)
467468
- @for sass loops doesn't work in linter https://github.com/AleshaOleg/postcss-sass/issues/53
468469
- https://github.com/bahmutov/cypress-vue-unit-test

build/webpack.config.base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const { VueLoaderPlugin } = require('vue-loader');
33
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
44
const VuetifyLoaderPlugin = require('vuetify-loader/lib/plugin');
5-
const {getConfig, createProgressPlugin} = require('./utils');
5+
const {getConfig} = require('./utils');
66

77

88
module.exports = {

cypress/plugins/index.js

+3-136
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,4 @@
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;
471
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+
}

cypress/tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"noFallthroughCasesInSwitch": true,
2020
"noImplicitReturns": true,
2121
"pretty": true,
22-
"sourceMap": true,
2322
"strict": true,
2423
"moduleResolution": "node"
2524
},

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"scripts": {
55
"build:prod": "webpack --progress --config build/webpack.config.prod.js",
66
"build:test": "APP_VERSION=app-v1 APP_TEST=true webpack --progress --config build/webpack.config.prod.js",
7+
"build:test:no-progress": "APP_VERSION=app-v1 APP_TEST=true webpack --config build/webpack.config.prod.js",
78
"build:file": "APP_PUBLIC_PATH='./' APP_FILE_MODE=true webpack --progress --config build/webpack.config.prod.js",
89
"start": "NODE_OPTIONS='--max-old-space-size=8192' webpack-dev-server --progress --host 0.0.0.0 --config build/webpack.config.dev.js --hot --port 8080 --colors",
910
"start:test": "APP_VERSION=app-v1 APP_TEST=true webpack-dev-server --progress --host 0.0.0.0 --config build/webpack.config.dev.js --hot --port 8080 --colors",
@@ -15,7 +16,7 @@
1516
"test:reports:combine": "mochawesome-merge ./nyc/mocha-reports/mochawesome*.json > nyc/mocha-reports/combined-report.json",
1617
"test:reports:generate": "marge nyc/mocha-reports/combined-report.json -f report -o nyc",
1718
"test:cypress": "run-s clean build:test test:cypress:run:report",
18-
"test:unit": "rm -rf ./nyc/.nyc_output_unit && rm -rf ./nyc/coverage_unit && yarn nyc --temp-dir ./nyc/.nyc_output_unit --report-dir ./nyc/coverage_unit mocha",
19+
"test:unit": "rm -rf ./nyc/coverage-unit-output && rm -rf ./nyc/coverage_unit && yarn nyc --temp-dir ./nyc/coverage-unit-output --report-dir ./nyc/coverage_unit mocha",
1920
"test": "run-p -c test:unit test:cypress",
2021
"lint:style": "stylelint 'src/**/*.vue' 'src/**/*.sass'",
2122
"lint:es:src": "eslint --ext .ts,.vue --max-warnings=0 src",
@@ -34,7 +35,7 @@
3435
"@babel/plugin-proposal-numeric-separator": "7.10.4",
3536
"@babel/plugin-proposal-optional-chaining": "7.11.0",
3637
"@babel/preset-env": "7.11.5",
37-
"@cypress/code-coverage": "3.8.1",
38+
"@cypress/code-coverage": "git+https://github.com/akoidan/code-coverage#d13b0858f1b15c686f559ec0932b2adb8362fd6f",
3839
"@cypress/webpack-preprocessor": "5.4.6",
3940
"@testing-library/cypress": "7.0.1",
4041
"@types/chai": "4.2.12",

0 commit comments

Comments
 (0)