Skip to content

feat: add support for other NYC configuration files #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions common-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,9 @@ function stringToArray(prop, obj) {
return obj
}

function combineNycOptions({
pkgNycOptions,
nycrc,
nycrcJson,
defaultNycOptions
}) {
function combineNycOptions(...options) {
// last option wins
const nycOptions = Object.assign(
{},
defaultNycOptions,
nycrc,
nycrcJson,
pkgNycOptions
)
const nycOptions = Object.assign({}, ...options)

// normalize string and [string] props
stringToArray('reporter', nycOptions)
Expand Down
24 changes: 11 additions & 13 deletions cypress/integration/combine-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ describe('Combine NYC options', () => {
extends: '@istanbuljs/nyc-config-typescript',
all: true
}
const combined = combineNycOptions({
pkgNycOptions,
defaultNycOptions
})
const combined = combineNycOptions(defaultNycOptions, pkgNycOptions)
cy.wrap(combined).should('deep.equal', {
extends: '@istanbuljs/nyc-config-typescript',
all: true,
Expand All @@ -23,10 +20,7 @@ describe('Combine NYC options', () => {
const pkgNycOptions = {
reporter: 'text'
}
const combined = combineNycOptions({
pkgNycOptions,
defaultNycOptions
})
const combined = combineNycOptions(defaultNycOptions, pkgNycOptions)
cy.wrap(combined).should('deep.equal', {
'report-dir': './coverage',
reporter: ['text'],
Expand All @@ -47,15 +41,19 @@ describe('Combine NYC options', () => {
exclude: ['bar.js'],
reporter: ['json']
}
const combined = combineNycOptions({
pkgNycOptions,
const nycConfig = {
'report-dir': './report'
}
const combined = combineNycOptions(
defaultNycOptions,
nycrc,
nycrcJson,
defaultNycOptions
})
nycConfig,
pkgNycOptions
)
cy.wrap(combined).should('deep.equal', {
all: true,
'report-dir': './coverage',
'report-dir': './report',
reporter: ['json'],
extension: ['.js'],
excludeAfterRemap: false,
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"execa": "4.0.2",
"globby": "11.0.0",
"istanbul-lib-coverage": "3.0.0",
"js-yaml": "3.14.0",
"nyc": "15.0.1"
},
"devDependencies": {
Expand Down
42 changes: 38 additions & 4 deletions task-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { isAbsolute, resolve, join } = require('path')
const debug = require('debug')('code-coverage')
const chalk = require('chalk')
const globby = require('globby')
const yaml = require('js-yaml')
const { combineNycOptions, defaultNycOptions } = require('./common-utils')

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

const nycOptions = combineNycOptions({
pkgNycOptions,
const nycrcYamlFilename = join(workingDirectory, '.nycrc.yaml')
let nycrcYaml = {}
if (existsSync(nycrcYamlFilename)) {
try {
nycrcYaml = yaml.safeLoad(readFileSync(nycrcYamlFilename, 'utf8'))
} catch (error) {
throw new Error(`Failed to load .nycrc.yaml: ${error.message}`)
}
}

const nycrcYmlFilename = join(workingDirectory, '.nycrc.yml')
let nycrcYml = {}
if (existsSync(nycrcYmlFilename)) {
try {
nycrcYml = yaml.safeLoad(readFileSync(nycrcYmlFilename, 'utf8'))
} catch (error) {
throw new Error(`Failed to load .nycrc.yml: ${error.message}`)
}
}

const nycConfigFilename = join(workingDirectory, 'nyc.config.js')
let nycConfig = {}
if (existsSync(nycConfigFilename)) {
try {
nycConfig = require(nycConfigFilename)
} catch (error) {
throw new Error(`Failed to load nyc.config.js: ${error.message}`)
}
}

const nycOptions = combineNycOptions(
defaultNycOptions,
nycrc,
nycrcJson,
defaultNycOptions
})
nycrcYaml,
nycrcYml,
nycConfig,
pkgNycOptions
)
debug('combined NYC options %o', nycOptions)

return nycOptions
Expand Down