Skip to content

Commit f637e2d

Browse files
authored
feat: add support for other NYC configuration files (#247)
1 parent 5de2e63 commit f637e2d

File tree

5 files changed

+61
-39
lines changed

5 files changed

+61
-39
lines changed

common-utils.js

+2-13
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,9 @@ function stringToArray(prop, obj) {
77
return obj
88
}
99

10-
function combineNycOptions({
11-
pkgNycOptions,
12-
nycrc,
13-
nycrcJson,
14-
defaultNycOptions
15-
}) {
10+
function combineNycOptions(...options) {
1611
// last option wins
17-
const nycOptions = Object.assign(
18-
{},
19-
defaultNycOptions,
20-
nycrc,
21-
nycrcJson,
22-
pkgNycOptions
23-
)
12+
const nycOptions = Object.assign({}, ...options)
2413

2514
// normalize string and [string] props
2615
stringToArray('reporter', nycOptions)

cypress/integration/combine-spec.js

+15-17
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,
@@ -77,12 +75,12 @@ describe('Combine NYC options', () => {
7775
exclude: 'bar.js',
7876
reporter: ['json']
7977
}
80-
const combined = combineNycOptions({
81-
pkgNycOptions,
78+
const combined = combineNycOptions(
79+
defaultNycOptions,
8280
nycrc,
8381
nycrcJson,
84-
defaultNycOptions
85-
})
82+
pkgNycOptions
83+
)
8684
cy.wrap(combined).should('deep.equal', {
8785
all: true,
8886
'report-dir': './coverage',

package-lock.json

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
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

+38-4
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)