Skip to content

Commit 17dbfee

Browse files
tavogelisaacs
authored andcommitted
Ensure process object exists for deprecation warn
Fix: #206 PR-URL: #207 Credit: @tavogel Close: #207 Reviewed-by: @isaacs
1 parent 2be1d24 commit 17dbfee

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

Diff for: index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ const deprecatedProperty = (field, instead) => {
2424
warn(code, `${field} property`, `cache.${instead}`, get)
2525
}
2626
}
27-
const shouldWarn = (code) => !(process.noDeprecation || warned.has(code))
27+
const shouldWarn = (code) => typeof process === 'object' &&
28+
process &&
29+
!(process.noDeprecation || warned.has(code))
2830
const warn = (code, what, instead, fn) => {
2931
warned.add(code)
3032
process.emitWarning(`The ${what} is deprecated. Please use ${instead} instead.`, 'DeprecationWarning', code, fn)

Diff for: tap-snapshots/test/deprecations.js.test.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Make sure to inspect the output below. Do not ignore changes!
66
*/
77
'use strict'
8-
exports[`test/deprecations.js TAP > must match snapshot 1`] = `
8+
exports[`test/deprecations.js TAP warns exactly once for a given deprecation > must match snapshot 1`] = `
99
Array [
1010
Array [
1111
"The stale option is deprecated. Please use options.allowStale instead.",

Diff for: test/deprecations.js

+56-27
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,61 @@ process.noDeprecation = false
55
const warnings = []
66
process.emitWarning = (...w) => warnings.push(w)
77

8-
const c = new LRU({
9-
max: 100,
10-
maxSize: 100,
11-
maxAge: 1000,
12-
stale: true,
13-
length: n => 1,
14-
})
15-
c.reset()
16-
t.equal(c.length, 0)
17-
t.equal(c.prune, c.purgeStale)
18-
t.equal(c.reset, c.clear)
19-
t.equal(c.del, c.delete)
20-
21-
t.matchSnapshot(warnings)
22-
23-
warnings.length = 0
24-
const d = new LRU({
25-
max: 100,
26-
maxSize: 100,
27-
maxAge: 1000,
28-
stale: true,
29-
length: n => 1,
8+
t.test('warns exactly once for a given deprecation', t => {
9+
const c = new LRU({
10+
max: 100,
11+
maxSize: 100,
12+
maxAge: 1000,
13+
stale: true,
14+
length: n => 1,
15+
})
16+
c.reset()
17+
t.equal(c.length, 0)
18+
t.equal(c.prune, c.purgeStale)
19+
t.equal(c.reset, c.clear)
20+
t.equal(c.del, c.delete)
21+
22+
t.matchSnapshot(warnings)
23+
24+
warnings.length = 0
25+
const d = new LRU({
26+
max: 100,
27+
maxSize: 100,
28+
maxAge: 1000,
29+
stale: true,
30+
length: n => 1,
31+
})
32+
d.reset()
33+
34+
t.equal(d.length, 0)
35+
t.equal(d.prune, d.purgeStale)
36+
t.equal(d.reset, d.clear)
37+
t.strictSame(warnings, [], 'only warn once')
38+
39+
warnings.length = 0
40+
t.end()
3041
})
31-
d.reset()
3242

33-
t.equal(d.length, 0)
34-
t.equal(d.prune, d.purgeStale)
35-
t.equal(d.reset, d.clear)
36-
t.strictSame(warnings, [], 'only warn once')
43+
t.test('does not do deprecation warning without process object', t => {
44+
// set process to null (emulate a browser)
45+
const proc = process
46+
t.teardown(() => global.process = proc)
47+
global.process = null
48+
49+
const c = new LRU({
50+
max: 100,
51+
maxSize: 100,
52+
maxAge: 1000,
53+
stale: true,
54+
length: n => 1,
55+
})
56+
c.reset()
57+
t.equal(c.length, 0)
58+
t.equal(c.prune, c.purgeStale)
59+
t.equal(c.reset, c.clear)
60+
t.equal(c.del, c.delete)
61+
62+
t.strictSame(warnings, [], 'no process exists')
63+
64+
t.end()
65+
})

0 commit comments

Comments
 (0)