Skip to content

Commit

Permalink
Ensure process object exists for deprecation warn
Browse files Browse the repository at this point in the history
Fix: #206
PR-URL: #207
Credit: @tavogel
Close: #207
Reviewed-by: @isaacs
  • Loading branch information
tavogel authored and isaacs committed Mar 9, 2022
1 parent 2be1d24 commit 17dbfee
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const deprecatedProperty = (field, instead) => {
warn(code, `${field} property`, `cache.${instead}`, get)
}
}
const shouldWarn = (code) => !(process.noDeprecation || warned.has(code))
const shouldWarn = (code) => typeof process === 'object' &&
process &&
!(process.noDeprecation || warned.has(code))
const warn = (code, what, instead, fn) => {
warned.add(code)
process.emitWarning(`The ${what} is deprecated. Please use ${instead} instead.`, 'DeprecationWarning', code, fn)
Expand Down
2 changes: 1 addition & 1 deletion tap-snapshots/test/deprecations.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/deprecations.js TAP > must match snapshot 1`] = `
exports[`test/deprecations.js TAP warns exactly once for a given deprecation > must match snapshot 1`] = `
Array [
Array [
"The stale option is deprecated. Please use options.allowStale instead.",
Expand Down
83 changes: 56 additions & 27 deletions test/deprecations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,61 @@ process.noDeprecation = false
const warnings = []
process.emitWarning = (...w) => warnings.push(w)

const c = new LRU({
max: 100,
maxSize: 100,
maxAge: 1000,
stale: true,
length: n => 1,
})
c.reset()
t.equal(c.length, 0)
t.equal(c.prune, c.purgeStale)
t.equal(c.reset, c.clear)
t.equal(c.del, c.delete)

t.matchSnapshot(warnings)

warnings.length = 0
const d = new LRU({
max: 100,
maxSize: 100,
maxAge: 1000,
stale: true,
length: n => 1,
t.test('warns exactly once for a given deprecation', t => {
const c = new LRU({
max: 100,
maxSize: 100,
maxAge: 1000,
stale: true,
length: n => 1,
})
c.reset()
t.equal(c.length, 0)
t.equal(c.prune, c.purgeStale)
t.equal(c.reset, c.clear)
t.equal(c.del, c.delete)

t.matchSnapshot(warnings)

warnings.length = 0
const d = new LRU({
max: 100,
maxSize: 100,
maxAge: 1000,
stale: true,
length: n => 1,
})
d.reset()

t.equal(d.length, 0)
t.equal(d.prune, d.purgeStale)
t.equal(d.reset, d.clear)
t.strictSame(warnings, [], 'only warn once')

warnings.length = 0
t.end()
})
d.reset()

t.equal(d.length, 0)
t.equal(d.prune, d.purgeStale)
t.equal(d.reset, d.clear)
t.strictSame(warnings, [], 'only warn once')
t.test('does not do deprecation warning without process object', t => {
// set process to null (emulate a browser)
const proc = process
t.teardown(() => global.process = proc)
global.process = null

const c = new LRU({
max: 100,
maxSize: 100,
maxAge: 1000,
stale: true,
length: n => 1,
})
c.reset()
t.equal(c.length, 0)
t.equal(c.prune, c.purgeStale)
t.equal(c.reset, c.clear)
t.equal(c.del, c.delete)

t.strictSame(warnings, [], 'no process exists')

t.end()
})

0 comments on commit 17dbfee

Please # to comment.