|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const { |
4 | | - ObjectAssign, |
5 | | - ObjectDefineProperty, |
6 | | -} = primordials; |
| 3 | +// Flags: --expose-internals |
| 4 | +require('../common'); |
| 5 | +const assert = require('assert'); |
7 | 6 |
|
8 | | -const { test, suite, before, after, beforeEach, afterEach } = require('internal/test_runner/harness'); |
9 | | -const { run } = require('internal/test_runner/runner'); |
| 7 | +// This test verifies mocking and restoring process.env.TEST_ENV |
10 | 8 |
|
11 | | -module.exports = test; |
12 | | -ObjectAssign(module.exports, { |
13 | | - after, |
14 | | - afterEach, |
15 | | - before, |
16 | | - beforeEach, |
17 | | - describe: suite, |
18 | | - it: test, |
19 | | - run, |
20 | | - suite, |
21 | | - test, |
22 | | -}); |
| 9 | +const hadOriginal = Object.prototype.hasOwnProperty.call(process.env, 'TEST_ENV'); |
| 10 | +const original = process.env.TEST_ENV; |
23 | 11 |
|
24 | | -let lazyMock; |
| 12 | +// Mock environment variable |
| 13 | +process.env.TEST_ENV = 'mocked'; |
| 14 | +assert.strictEqual(process.env.TEST_ENV, 'mocked'); |
| 15 | +console.log('Mocked process.env.TEST_ENV successfully'); |
25 | 16 |
|
26 | | -ObjectDefineProperty(module.exports, 'mock', { |
27 | | - __proto__: null, |
28 | | - configurable: true, |
29 | | - enumerable: true, |
30 | | - get() { |
31 | | - if (lazyMock === undefined) { |
32 | | - const { MockTracker } = require('internal/test_runner/mock/mock'); |
| 17 | +// Restore original variable |
| 18 | +if (hadOriginal) { |
| 19 | + process.env.TEST_ENV = original; |
| 20 | +} else { |
| 21 | + delete process.env.TEST_ENV; |
| 22 | +} |
33 | 23 |
|
34 | | - lazyMock = new MockTracker(); |
35 | | - } |
36 | 24 |
|
37 | | - return lazyMock; |
38 | | - }, |
39 | | -}); |
| 25 | +if (hadOriginal) { |
| 26 | + assert.strictEqual(process.env.TEST_ENV, original); |
| 27 | +} else { |
| 28 | + assert.ok(!('TEST_ENV' in process.env)); |
| 29 | +} |
40 | 30 |
|
41 | | -let lazySnapshot; |
42 | | - |
43 | | -ObjectDefineProperty(module.exports, 'snapshot', { |
44 | | - __proto__: null, |
45 | | - configurable: true, |
46 | | - enumerable: true, |
47 | | - get() { |
48 | | - if (lazySnapshot === undefined) { |
49 | | - const { |
50 | | - setDefaultSnapshotSerializers, |
51 | | - setResolveSnapshotPath, |
52 | | - } = require('internal/test_runner/snapshot'); |
53 | | - |
54 | | - lazySnapshot = { |
55 | | - __proto__: null, |
56 | | - setDefaultSnapshotSerializers, |
57 | | - setResolveSnapshotPath, |
58 | | - }; |
59 | | - } |
60 | | - |
61 | | - return lazySnapshot; |
62 | | - }, |
63 | | -}); |
64 | | - |
65 | | -ObjectDefineProperty(module.exports, 'assert', { |
66 | | - __proto__: null, |
67 | | - configurable: true, |
68 | | - enumerable: true, |
69 | | - get() { |
70 | | - const { register } = require('internal/test_runner/assert'); |
71 | | - const assert = { __proto__: null, register }; |
72 | | - ObjectDefineProperty(module.exports, 'assert', assert); |
73 | | - return assert; |
74 | | - }, |
75 | | -}); |
| 31 | +console.log('Restored original process.env.TEST_ENV'); |
0 commit comments