Skip to content

Commit 17d8993

Browse files
committed
Refactoring
1 parent 5d13def commit 17d8993

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/options/testing.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ const { multipleValidOptions } = require('jest-validate')
55
const { RUNNERS } = require('./runners')
66

77
// Apply `options.testing` which is basically a preset of options.
8-
const applyTesting = function({ opts, opts: { testing, level, ...optsA } }) {
8+
const applyTesting = function({ opts, opts: { level, testing } }) {
99
if (testing === undefined) {
1010
return opts
1111
}
1212

1313
const testOpts = RUNNERS[testing]
1414

1515
validateTesting({ testOpts, testing })
16-
validateTestOpts({ opts: optsA, testing })
16+
validateTestOpts({ opts, testOpts, testing })
1717

1818
return {
19-
...optsA,
19+
...opts,
2020
...testOpts,
2121
// Users can override `level.default` but not the ones defined in `testOpts`
2222
level: { default: 'error', ...level, ...testOpts.level },
@@ -28,28 +28,37 @@ const validateTesting = function({ testOpts, testing }) {
2828
return
2929
}
3030

31+
const runners = Object.keys(RUNNERS).join(', ')
3132
throw new Error(
32-
`Invalid option 'testing' '${testing}': must be one of ${Object.keys(
33-
RUNNERS,
34-
).join(', ')}`,
33+
`Invalid option 'testing' '${testing}': must be one of ${runners}`,
3534
)
3635
}
3736

3837
// Presets override other options. We make sure users do not assume their
3938
// options are used when they are actually overriden.
40-
// However we allow overriding preset's `level` so users can filter events.
41-
const validateTestOpts = function({ opts, testing }) {
42-
const [optName] = Object.keys(opts)
39+
const validateTestOpts = function({ opts, testOpts, testing }) {
40+
const forbiddenOpts = Object.keys(testOpts).filter(isForbiddenOpt)
4341

44-
if (optName === undefined) {
42+
const invalidOpt = Object.keys(opts).find(optName =>
43+
forbiddenOpts.includes(optName),
44+
)
45+
46+
if (invalidOpt === undefined) {
4547
return
4648
}
4749

4850
throw new Error(
49-
`Invalid option '${optName}': it must not be defined together with the option 'testing' '${testing}'`,
51+
`Invalid option '${invalidOpt}': it must not be defined together with the option 'testing' '${testing}'`,
5052
)
5153
}
5254

55+
// We allow overriding preset's `level` so users can filter events.
56+
const isForbiddenOpt = function(optName) {
57+
return !ALLOWED_OPTS.includes(optName)
58+
}
59+
60+
const ALLOWED_OPTS = ['level']
61+
5362
// Use during options validation
5463
const getExampleTesting = function() {
5564
return multipleValidOptions(...Object.keys(RUNNERS))

0 commit comments

Comments
 (0)