node/test/parallel/test-process-env-symbols.js
Anna Henningsen 3295a7feba
src: allow getting Symbols on process.env
1aa595e5bd introduced a `throw` for accessing `Symbol` properties of
`process.env`. However, this breaks `util.inspect(process)` and
things like `Object.prototype.toString.call(process.env)`, so this
patch changes the behaviour for the getter to just always return
`undefined`.

Ref: https://github.com/nodejs/node/pull/9446
Fixes: https://github.com/nodejs/node/issues/9641
PR-URL: https://github.com/nodejs/node/pull/9631
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-11-20 01:40:33 +01:00

28 lines
789 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
require('../common');
const assert = require('assert');
const symbol = Symbol('sym');
const errRegExp = /^TypeError: Cannot convert a Symbol value to a string$/;
// Verify that getting via a symbol key returns undefined.
assert.strictEqual(process.env[symbol], undefined);
// Verify that assigning via a symbol key throws.
assert.throws(() => {
process.env[symbol] = 42;
}, errRegExp);
// Verify that assigning a symbol value throws.
assert.throws(() => {
process.env.foo = symbol;
}, errRegExp);
// Verify that using a symbol with the in operator throws.
assert.throws(() => {
symbol in process.env;
}, errRegExp);
// Checks that well-known symbols like `Symbol.toStringTag` wont throw.
assert.doesNotThrow(() => Object.prototype.toString.call(process.env));