Skip to content

Commit 10efbf2

Browse files
committed
Add support for other NYC config files
1 parent 684efec commit 10efbf2

File tree

5 files changed

+57
-35
lines changed

5 files changed

+57
-35
lines changed

common-utils.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
// @ts-check
2-
function combineNycOptions({
3-
pkgNycOptions,
4-
nycrc,
5-
nycrcJson,
6-
defaultNycOptions
7-
}) {
2+
function combineNycOptions(...options) {
83
// last option wins
9-
const nycOptions = Object.assign(
10-
{},
11-
defaultNycOptions,
12-
nycrc,
13-
nycrcJson,
14-
pkgNycOptions
15-
)
4+
const nycOptions = Object.assign({}, ...options)
165

176
if (typeof nycOptions.reporter === 'string') {
187
nycOptions.reporter = [nycOptions.reporter]

cypress/integration/combine-spec.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ describe('Combine NYC options', () => {
55
extends: '@istanbuljs/nyc-config-typescript',
66
all: true
77
}
8-
const combined = combineNycOptions({
9-
pkgNycOptions,
10-
defaultNycOptions
11-
})
8+
const combined = combineNycOptions(defaultNycOptions, pkgNycOptions)
129
cy.wrap(combined).should('deep.equal', {
1310
extends: '@istanbuljs/nyc-config-typescript',
1411
all: true,
@@ -23,10 +20,7 @@ describe('Combine NYC options', () => {
2320
const pkgNycOptions = {
2421
reporter: 'text'
2522
}
26-
const combined = combineNycOptions({
27-
pkgNycOptions,
28-
defaultNycOptions
29-
})
23+
const combined = combineNycOptions(defaultNycOptions, pkgNycOptions)
3024
cy.wrap(combined).should('deep.equal', {
3125
'report-dir': './coverage',
3226
reporter: ['text'],
@@ -47,15 +41,19 @@ describe('Combine NYC options', () => {
4741
exclude: ['bar.js'],
4842
reporter: ['json']
4943
}
50-
const combined = combineNycOptions({
51-
pkgNycOptions,
44+
const nycConfig = {
45+
'report-dir': './report'
46+
}
47+
const combined = combineNycOptions(
48+
defaultNycOptions,
5249
nycrc,
5350
nycrcJson,
54-
defaultNycOptions
55-
})
51+
nycConfig,
52+
pkgNycOptions
53+
)
5654
cy.wrap(combined).should('deep.equal', {
5755
all: true,
58-
'report-dir': './coverage',
56+
'report-dir': './report',
5957
reporter: ['json'],
6058
extension: ['.js'],
6159
excludeAfterRemap: false,

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"execa": "4.0.2",
5353
"globby": "11.0.0",
5454
"istanbul-lib-coverage": "3.0.0",
55+
"js-yaml": "3.14.0",
5556
"nyc": "15.0.1"
5657
},
5758
"devDependencies": {

task-utils.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { isAbsolute, resolve, join } = require('path')
88
const debug = require('debug')('code-coverage')
99
const chalk = require('chalk')
1010
const globby = require('globby')
11+
const yaml = require('js-yaml')
1112
const { combineNycOptions, defaultNycOptions } = require('./common-utils')
1213

1314
function readNycOptions(workingDirectory) {
@@ -27,12 +28,45 @@ function readNycOptions(workingDirectory) {
2728
? JSON.parse(readFileSync(nycrcJsonFilename, 'utf8'))
2829
: {}
2930

30-
const nycOptions = combineNycOptions({
31-
pkgNycOptions,
31+
const nycrcYamlFilename = join(workingDirectory, '.nycrc.yaml')
32+
let nycrcYaml = {}
33+
if (existsSync(nycrcYamlFilename)) {
34+
try {
35+
nycrcYaml = yaml.safeLoad(readFileSync(nycrcYamlFilename, 'utf8'))
36+
} catch (error) {
37+
throw new Error(`Failed to load .nycrc.yaml: ${error.message}`)
38+
}
39+
}
40+
41+
const nycrcYmlFilename = join(workingDirectory, '.nycrc.yml')
42+
let nycrcYml = {}
43+
if (existsSync(nycrcYmlFilename)) {
44+
try {
45+
nycrcYml = yaml.safeLoad(readFileSync(nycrcYmlFilename, 'utf8'))
46+
} catch (error) {
47+
throw new Error(`Failed to load .nycrc.yml: ${error.message}`)
48+
}
49+
}
50+
51+
const nycConfigFilename = join(workingDirectory, 'nyc.config.js')
52+
let nycConfig = {}
53+
if (existsSync(nycConfigFilename)) {
54+
try {
55+
nycConfig = require(nycConfigFilename)
56+
} catch (error) {
57+
throw new Error(`Failed to load nyc.config.js: ${error.message}`)
58+
}
59+
}
60+
61+
const nycOptions = combineNycOptions(
62+
defaultNycOptions,
3263
nycrc,
3364
nycrcJson,
34-
defaultNycOptions
35-
})
65+
nycrcYaml,
66+
nycrcYml,
67+
nycConfig,
68+
pkgNycOptions
69+
)
3670
debug('combined NYC options %o', nycOptions)
3771

3872
return nycOptions

0 commit comments

Comments
 (0)