Skip to content

Commit

Permalink
feat: added option to throw on missing env (#664)
Browse files Browse the repository at this point in the history
* feat: added option to throw on missing env

* fix: passing tests for throwOnMissing
  • Loading branch information
fivethreeo committed Dec 13, 2021
1 parent dc49190 commit d48e0d7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -556,6 +556,8 @@ Options:
Default is `false`.
* `dangerousExtend`: Disable security checks for `extend` query.
Default is `false`.
* `throwOnMissing`: throw a error if env is not found.
Default is `false`.
* `mobileToDesktop`: Use desktop browsers if Can I Use doesn鈥檛 have data
about this mobile version. Can I Use has only data only about
latest versions of mobile browsers. By default, `last 2 and_ff versions`
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -397,6 +397,7 @@ var cache = {}
* version in direct query.
* @param {boolean} [opts.dangerousExtend] Disable security checks
* for extend query.
* @param {boolean} [opts.throwOnMissing] Throw error on missing env.
* @param {boolean} [opts.mobileToDesktop] Alias mobile browsers to the desktop
* version when Can I Use doesn't have
* data about the specified version.
Expand Down
8 changes: 8 additions & 0 deletions node.js
Expand Up @@ -82,6 +82,14 @@ function pickEnv(config, opts) {
name = 'production'
}

if (opts.throwOnMissing) {
if (name && name !== 'defaults' && !config[name]) {
throw new BrowserslistError(
'Missing config for Browserslist environment `' + name + '`.'
)
}
}

return config[name] || config.defaults
}

Expand Down
19 changes: 19 additions & 0 deletions test/main.test.js
Expand Up @@ -260,4 +260,23 @@ test('correctly works with not and one-version browsers', () => {
equal(browserslist('last 1 Baidu version, not <2% in AT'), ['baidu 7.12'])
})

test('throws error on missing env', () => {
throws(
() => browserslist(null, { path: PACKAGE, throwOnMissing: true, env: 'test' }),
"Missing config for Browserslist environment 'test'."
)
})

test('does not throw error on missing defaults env', () => {
equal(
browserslist(null, { path: PACKAGE, throwOnMissing: true, env: 'defaults' }), DEFAULTS
)
})

test('does not throw error on missing env', () => {
equal(
browserslist(null, { path: PACKAGE, throwOnMissing: false, env: 'test' }), DEFAULTS
)
})

test.run()

0 comments on commit d48e0d7

Please sign in to comment.