Skip to content

Commit 060f17b

Browse files
authored
fix: split utils into support and task utils (cypress-io#202)
* fix: split utils for task from utils for support file * fix typo * add TypeScript type check * move function * fix options
1 parent 8f48955 commit 060f17b

File tree

8 files changed

+98
-72
lines changed

8 files changed

+98
-72
lines changed

.circleci/config.yml

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ jobs:
1717
- checkout
1818
- node/with-cache:
1919
steps:
20-
- run: CYPRESS_INSTALL_BINARY=0 npm ci
21-
- run: npm run format:check
20+
- run:
21+
name: Install dependencies 📦
22+
# installs NPM dependencies but skips Cypress
23+
command: CYPRESS_INSTALL_BINARY=0 npm ci
24+
- run:
25+
name: Code style check 🧹
26+
command: npm run format:check
27+
- run:
28+
name: Types lint 🧹
29+
command: npm run types
2230

2331
publish:
2432
description: Publishes the new version of the plugin to NPM

cypress/integration/filtering.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { filterSpecsFromCoverage } = require('../../utils')
1+
const { filterSpecsFromCoverage } = require('../../support-utils')
22

33
describe('minimatch', () => {
44
it('string matches', () => {

cypress/integration/spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// <reference types="Cypress" />
44

55
import { add } from '../unit'
6-
const { fixSourcePathes } = require('../../utils')
6+
const { fixSourcePaths } = require('../../support-utils')
77

88
context('Page test', () => {
99
beforeEach(() => {
@@ -54,7 +54,7 @@ context('Unit tests', () => {
5454
}
5555
}
5656

57-
fixSourcePathes(coverage)
57+
fixSourcePaths(coverage)
5858

5959
expect(coverage).to.deep.eq({
6060
'/absolute/src/component.vue': {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"format": "prettier --write '*.js'",
1616
"format:check": "prettier --check '*.js'",
1717
"check:markdown": "find *.md -exec npx markdown-link-check {} \\;",
18-
"effective:config": "circleci config process .circleci/config.yml | sed /^#/d"
18+
"effective:config": "circleci config process .circleci/config.yml | sed /^#/d",
19+
"types": "tsc --noEmit --allowJs *.js cypress/integration/*.js"
1920
},
2021
"peerDependencies": {
2122
"cypress": "*"

support-utils.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// @ts-check
2+
// helper functions that are safe to use in the browser
3+
// from support.js file - no file system access
4+
5+
/**
6+
* remove coverage for the spec files themselves,
7+
* only keep "external" application source file coverage
8+
*/
9+
const filterSpecsFromCoverage = (totalCoverage, config = Cypress.config) => {
10+
const integrationFolder = config('integrationFolder')
11+
// @ts-ignore
12+
const testFilePattern = config('testFiles')
13+
14+
// test files chould be:
15+
// wild card string "**/*.*" (default)
16+
// wild card string "**/*spec.js"
17+
// list of wild card strings or names ["**/*spec.js", "spec-one.js"]
18+
const testFilePatterns = Array.isArray(testFilePattern)
19+
? testFilePattern
20+
: [testFilePattern]
21+
22+
const isUsingDefaultTestPattern = testFilePattern === '**/*.*'
23+
24+
const isTestFile = filename => {
25+
const matchedPattern = testFilePatterns.some(specPattern =>
26+
Cypress.minimatch(filename, specPattern)
27+
)
28+
const matchedEndOfPath = testFilePatterns.some(specPattern =>
29+
filename.endsWith(specPattern)
30+
)
31+
return matchedPattern || matchedEndOfPath
32+
}
33+
34+
const isInIntegrationFolder = filename =>
35+
filename.startsWith(integrationFolder)
36+
37+
const isA = (fileCoverge, filename) => isInIntegrationFolder(filename)
38+
const isB = (fileCoverge, filename) => isTestFile(filename)
39+
40+
const isTestFileFilter = isUsingDefaultTestPattern ? isA : isB
41+
42+
const coverage = Cypress._.omitBy(totalCoverage, isTestFileFilter)
43+
return coverage
44+
}
45+
46+
/**
47+
* Replace source-map's path by the corresponding absolute file path
48+
* (coverage report wouldn't work with source-map path being relative
49+
* or containing Webpack loaders and query parameters)
50+
*/
51+
function fixSourcePaths(coverage) {
52+
Object.values(coverage).forEach(file => {
53+
const { path: absolutePath, inputSourceMap } = file
54+
const fileName = /([^\/\\]+)$/.exec(absolutePath)[1]
55+
if (!inputSourceMap || !fileName) return
56+
57+
if (inputSourceMap.sourceRoot) inputSourceMap.sourceRoot = ''
58+
inputSourceMap.sources = inputSourceMap.sources.map(source =>
59+
source.includes(fileName) ? absolutePath : source
60+
)
61+
})
62+
}
63+
64+
module.exports = {
65+
fixSourcePaths,
66+
filterSpecsFromCoverage
67+
}

support.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference types="cypress" />
22
// @ts-check
33

4-
const { filterSpecsFromCoverage } = require('./utils')
4+
const { filterSpecsFromCoverage } = require('./support-utils')
55

66
/**
77
* Sends collected code coverage object to the backend code
@@ -35,6 +35,7 @@ const logMessage = s => {
3535
const filterSupportFilesFromCoverage = totalCoverage => {
3636
const integrationFolder = Cypress.config('integrationFolder')
3737
const supportFile = Cypress.config('supportFile')
38+
// @ts-ignore
3839
const supportFolder = Cypress.config('supportFolder')
3940

4041
const isSupportFile = filename => filename === supportFile
@@ -61,12 +62,14 @@ const registerHooks = () => {
6162

6263
const hasE2ECoverage = () => Boolean(windowCoverageObjects.length)
6364

65+
// @ts-ignore
6466
const hasUnitTestCoverage = () => Boolean(window.__coverage__)
6567

6668
before(() => {
6769
// we need to reset the coverage when running
6870
// in the interactive mode, otherwise the counters will
6971
// keep increasing every time we rerun the tests
72+
// @ts-ignore
7073
cy.task('resetCoverage', { isInteractive: Cypress.config('isInteractive') })
7174
})
7275

@@ -133,7 +136,9 @@ const registerHooks = () => {
133136

134137
// there might be server-side code coverage information
135138
// we should grab it once after all tests finish
139+
// @ts-ignore
136140
const baseUrl = Cypress.config('baseUrl') || cy.state('window').origin
141+
// @ts-ignore
137142
const runningEndToEndTests = baseUrl !== Cypress.config('proxyUrl')
138143
const specType = Cypress._.get(Cypress.spec, 'specType', 'integration')
139144
const isIntegrationSpec = specType === 'integration'
@@ -152,7 +157,9 @@ const registerHooks = () => {
152157
log: false,
153158
failOnStatusCode: false
154159
})
155-
.then(r => Cypress._.get(r, 'body.coverage', null), { log: false })
160+
.then(r => {
161+
return Cypress._.get(r, 'body.coverage', null)
162+
})
156163
.then(coverage => {
157164
if (!coverage) {
158165
// we did not get code coverage - this is the
@@ -171,6 +178,7 @@ const registerHooks = () => {
171178
// then we will have unit test coverage
172179
// NOTE: spec iframe is NOT reset between the tests, so we can grab
173180
// the coverage information only once after all tests have finished
181+
// @ts-ignore
174182
const unitTestCoverage = window.__coverage__
175183
if (unitTestCoverage) {
176184
sendCoverage(unitTestCoverage, 'unit')

utils.js renamed to task-utils.js

+3-61
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// helper functions to use from "task.js" plugins code
2+
// that need access to the file system
3+
14
// @ts-check
25
/// <reference types="cypress" />
36
const { readFileSync, writeFileSync, existsSync } = require('fs')
@@ -26,65 +29,6 @@ function checkAllPathsNotFound(nycFilename) {
2629
return allFilesAreMissing
2730
}
2831

29-
/**
30-
* remove coverage for the spec files themselves,
31-
* only keep "external" application source file coverage
32-
*/
33-
const filterSpecsFromCoverage = (totalCoverage, config = Cypress.config) => {
34-
const integrationFolder = config('integrationFolder')
35-
// @ts-ignore
36-
const testFilePattern = config('testFiles')
37-
38-
// test files chould be:
39-
// wild card string "**/*.*" (default)
40-
// wild card string "**/*spec.js"
41-
// list of wild card strings or names ["**/*spec.js", "spec-one.js"]
42-
const testFilePatterns = Array.isArray(testFilePattern)
43-
? testFilePattern
44-
: [testFilePattern]
45-
46-
const isUsingDefaultTestPattern = testFilePattern === '**/*.*'
47-
48-
const isTestFile = filename => {
49-
const matchedPattern = testFilePatterns.some(specPattern =>
50-
Cypress.minimatch(filename, specPattern)
51-
)
52-
const matchedEndOfPath = testFilePatterns.some(specPattern =>
53-
filename.endsWith(specPattern)
54-
)
55-
return matchedPattern || matchedEndOfPath
56-
}
57-
58-
const isInIntegrationFolder = filename =>
59-
filename.startsWith(integrationFolder)
60-
61-
const isA = (fileCoverge, filename) => isInIntegrationFolder(filename)
62-
const isB = (fileCoverge, filename) => isTestFile(filename)
63-
64-
const isTestFileFilter = isUsingDefaultTestPattern ? isA : isB
65-
66-
const coverage = Cypress._.omitBy(totalCoverage, isTestFileFilter)
67-
return coverage
68-
}
69-
70-
/**
71-
* Replace source-map's path by the corresponding absolute file path
72-
* (coverage report wouldn't work with source-map path being relative
73-
* or containing Webpack loaders and query parameters)
74-
*/
75-
function fixSourcePathes(coverage) {
76-
Object.values(coverage).forEach(file => {
77-
const { path: absolutePath, inputSourceMap } = file
78-
const fileName = /([^\/\\]+)$/.exec(absolutePath)[1]
79-
if (!inputSourceMap || !fileName) return
80-
81-
if (inputSourceMap.sourceRoot) inputSourceMap.sourceRoot = ''
82-
inputSourceMap.sources = inputSourceMap.sources.map(source =>
83-
source.includes(fileName) ? absolutePath : source
84-
)
85-
})
86-
}
87-
8832
/**
8933
* A small debug utility to inspect paths saved in NYC output JSON file
9034
*/
@@ -251,8 +195,6 @@ function tryFindingLocalFiles(nycFilename) {
251195
}
252196

253197
module.exports = {
254-
fixSourcePathes,
255-
filterSpecsFromCoverage,
256198
showNycInfo,
257199
resolveRelativePaths,
258200
checkAllPathsNotFound,

task.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ const { join, resolve } = require('path')
44
const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
55
const execa = require('execa')
66
const {
7-
fixSourcePathes,
87
showNycInfo,
98
resolveRelativePaths,
109
checkAllPathsNotFound,
1110
tryFindingLocalFiles
12-
} = require('./utils')
11+
} = require('./task-utils')
12+
const { fixSourcePaths } = require('./support-utils')
1313
const NYC = require('nyc')
1414

1515
const debug = require('debug')('code-coverage')
@@ -79,7 +79,7 @@ const tasks = {
7979
const coverage = JSON.parse(sentCoverage)
8080
debug('parsed sent coverage')
8181

82-
fixSourcePathes(coverage)
82+
fixSourcePaths(coverage)
8383
const previous = existsSync(nycFilename)
8484
? JSON.parse(readFileSync(nycFilename, 'utf8'))
8585
: istanbul.createCoverageMap({})

0 commit comments

Comments
 (0)