| 1 |
const t = require('tap') |
| 2 |
const mockGlobals = require('..') |
| 3 |
|
| 4 |
/* eslint-disable no-console */ |
| 5 |
const originals = { |
| 6 |
platform: process.platform, |
| 7 |
error: console.error, |
| 8 |
stderrOn: process.stderr.on, |
| 9 |
stderrWrite: process.stderr.write, |
| 10 |
shell: process.env.SHELL, |
| 11 |
home: process.env.HOME, |
| 12 |
argv: process.argv, |
| 13 |
env: process.env, |
| 14 |
setInterval, |
| 15 |
} |
| 16 |
|
| 17 |
t.test('console', async t => { |
| 18 |
await t.test('mocks', async (t) => { |
| 19 |
const errors = [] |
| 20 |
mockGlobals(t, { |
| 21 |
'console.error': (...args) => errors.push(...args), |
| 22 |
}) |
| 23 |
|
| 24 |
console.error(1) |
| 25 |
console.error(2) |
| 26 |
console.error(3) |
| 27 |
t.strictSame(errors, [1, 2, 3], 'i got my errors') |
| 28 |
}) |
| 29 |
|
| 30 |
t.equal(console.error, originals.error) |
| 31 |
}) |
| 32 |
/* eslint-enable no-console */ |
| 33 |
|
| 34 |
t.test('platform', async (t) => { |
| 35 |
t.equal(process.platform, originals.platform) |
| 36 |
|
| 37 |
await t.test('posix', async (t) => { |
| 38 |
mockGlobals(t, { 'process.platform': 'posix' }) |
| 39 |
t.equal(process.platform, 'posix') |
| 40 |
|
| 41 |
await t.test('win32 --> woo', async (t) => { |
| 42 |
mockGlobals(t, { 'process.platform': 'win32' }) |
| 43 |
t.equal(process.platform, 'win32') |
| 44 |
|
| 45 |
mockGlobals(t, { 'process.platform': 'woo' }) |
| 46 |
t.equal(process.platform, 'woo') |
| 47 |
}) |
| 48 |
|
| 49 |
t.equal(process.platform, 'posix') |
| 50 |
}) |
| 51 |
|
| 52 |
t.equal(process.platform, originals.platform) |
| 53 |
}) |
| 54 |
|
| 55 |
t.test('manual reset', async t => { |
| 56 |
let errorHandler, data |
| 57 |
|
| 58 |
const { reset } = mockGlobals(t, { |
| 59 |
'process.stderr.on': (__, handler) => { |
| 60 |
errorHandler = handler |
| 61 |
reset['process.stderr.on']() |
| 62 |
}, |
| 63 |
'process.stderr.write': (chunk, callback) => { |
| 64 |
data = chunk |
| 65 |
process.nextTick(() => { |
| 66 |
errorHandler({ errno: 'EPIPE' }) |
| 67 |
callback() |
| 68 |
}) |
| 69 |
reset['process.stderr.write']() |
| 70 |
}, |
| 71 |
}) |
| 72 |
|
| 73 |
await new Promise((res, rej) => { |
| 74 |
process.stderr.on('error', er => er.errno === 'EPIPE' ? res() : rej(er)) |
| 75 |
process.stderr.write('hey', res) |
| 76 |
}) |
| 77 |
|
| 78 |
t.equal(process.stderr.on, originals.stderrOn) |
| 79 |
t.equal(process.stderr.write, originals.stderrWrite) |
| 80 |
t.equal(data, 'hey', 'handles EPIPE errors') |
| 81 |
t.ok(errorHandler) |
| 82 |
}) |
| 83 |
|
| 84 |
t.test('reset called multiple times', async (t) => { |
| 85 |
await t.test('single reset', async t => { |
| 86 |
const { reset } = mockGlobals(t, { 'process.platform': 'z' }) |
| 87 |
t.equal(process.platform, 'z') |
| 88 |
|
| 89 |
reset['process.platform']() |
| 90 |
t.equal(process.platform, originals.platform) |
| 91 |
|
| 92 |
reset['process.platform']() |
| 93 |
reset['process.platform']() |
| 94 |
reset['process.platform']() |
| 95 |
t.equal(process.platform, originals.platform) |
| 96 |
}) |
| 97 |
|
| 98 |
t.equal(process.platform, originals.platform) |
| 99 |
}) |
| 100 |
|
| 101 |
t.test('object mode', async t => { |
| 102 |
await t.test('mocks', async t => { |
| 103 |
const home = t.testdir() |
| 104 |
|
| 105 |
mockGlobals(t, { |
| 106 |
process: { |
| 107 |
stderr: { |
| 108 |
on: '1', |
| 109 |
}, |
| 110 |
env: { |
| 111 |
HOME: home, |
| 112 |
}, |
| 113 |
}, |
| 114 |
}) |
| 115 |
|
| 116 |
t.equal(process.stderr.on, '1') |
| 117 |
t.equal(process.env.HOME, home) |
| 118 |
}) |
| 119 |
|
| 120 |
t.equal(process.env.HOME, originals.home) |
| 121 |
t.equal(process.stderr.write, originals.stderrWrite) |
| 122 |
}) |
| 123 |
|
| 124 |
t.test('mixed object/string mode', async t => { |
| 125 |
await t.test('mocks', async t => { |
| 126 |
const home = t.testdir() |
| 127 |
|
| 128 |
mockGlobals(t, { |
| 129 |
'process.env': { |
| 130 |
HOME: home, |
| 131 |
TEST: '1', |
| 132 |
}, |
| 133 |
}) |
| 134 |
|
| 135 |
t.equal(process.env.HOME, home) |
| 136 |
t.equal(process.env.TEST, '1') |
| 137 |
}) |
| 138 |
|
| 139 |
t.equal(process.env.HOME, originals.home) |
| 140 |
t.equal(process.env.TEST, undefined) |
| 141 |
}) |
| 142 |
|
| 143 |
t.test('conflicting mixed object/string mode', async t => { |
| 144 |
await t.test('same key', async t => { |
| 145 |
t.throws( |
| 146 |
() => mockGlobals(t, { |
| 147 |
process: { |
| 148 |
env: { |
| 149 |
HOME: '1', |
| 150 |
TEST: '1', |
| 151 |
NODE_ENV: '1', |
| 152 |
}, |
| 153 |
stderr: { |
| 154 |
write: '1', |
| 155 |
}, |
| 156 |
}, |
| 157 |
'process.env.HOME': '1', |
| 158 |
'process.stderr.write': '1', |
| 159 |
}), |
| 160 |
/process.env.HOME,process.stderr.write/ |
| 161 |
) |
| 162 |
}) |
| 163 |
|
| 164 |
await t.test('partial overwrite with replace', async t => { |
| 165 |
t.throws( |
| 166 |
() => mockGlobals(t, { |
| 167 |
process: { |
| 168 |
env: { |
| 169 |
HOME: '1', |
| 170 |
TEST: '1', |
| 171 |
NODE_ENV: '1', |
| 172 |
}, |
| 173 |
stderr: { |
| 174 |
write: '1', |
| 175 |
}, |
| 176 |
}, |
| 177 |
'process.env.HOME': '1', |
| 178 |
'process.stderr.write': '1', |
| 179 |
}, { replace: true }), |
| 180 |
/process -> process.env.HOME,process.stderr.write/ |
| 181 |
) |
| 182 |
}) |
| 183 |
}) |
| 184 |
|
| 185 |
t.test('falsy values', async t => { |
| 186 |
await t.test('undefined deletes', async t => { |
| 187 |
mockGlobals(t, { 'process.platform': undefined }) |
| 188 |
t.notOk(Object.prototype.hasOwnProperty.call(process, 'platform')) |
| 189 |
t.equal(process.platform, undefined) |
| 190 |
}) |
| 191 |
|
| 192 |
await t.test('null', async t => { |
| 193 |
mockGlobals(t, { 'process.platform': null }) |
| 194 |
t.ok(Object.prototype.hasOwnProperty.call(process, 'platform')) |
| 195 |
t.equal(process.platform, null) |
| 196 |
}) |
| 197 |
|
| 198 |
t.equal(process.platform, originals.platform) |
| 199 |
}) |
| 200 |
|
| 201 |
t.test('date', async t => { |
| 202 |
await t.test('mocks', async t => { |
| 203 |
mockGlobals(t, { |
| 204 |
'Date.now': () => 100, |
| 205 |
'Date.prototype.toISOString': () => 'DDD', |
| 206 |
}) |
| 207 |
t.equal(Date.now(), 100) |
| 208 |
t.equal(new Date().toISOString(), 'DDD') |
| 209 |
}) |
| 210 |
|
| 211 |
t.ok(Date.now() > 100) |
| 212 |
t.ok(new Date().toISOString().includes('T')) |
| 213 |
}) |
| 214 |
|
| 215 |
t.test('argv', async t => { |
| 216 |
await t.test('argv', async t => { |
| 217 |
mockGlobals(t, { 'process.argv': ['node', 'woo'] }) |
| 218 |
t.strictSame(process.argv, ['node', 'woo']) |
| 219 |
}) |
| 220 |
|
| 221 |
t.strictSame(process.argv, originals.argv) |
| 222 |
}) |
| 223 |
|
| 224 |
t.test('replace', async (t) => { |
| 225 |
await t.test('env', async t => { |
| 226 |
mockGlobals(t, { 'process.env': { HOME: '1' } }, { replace: true }) |
| 227 |
t.strictSame(process.env, { HOME: '1' }) |
| 228 |
t.equal(Object.keys(process.env).length, 1) |
| 229 |
}) |
| 230 |
|
| 231 |
await t.test('setInterval', async t => { |
| 232 |
mockGlobals(t, { setInterval: 0 }, { replace: true }) |
| 233 |
t.strictSame(setInterval, 0) |
| 234 |
}) |
| 235 |
|
| 236 |
t.strictSame(setInterval, originals.setInterval) |
| 237 |
t.strictSame(process.env, originals.env) |
| 238 |
}) |
| 239 |
|
| 240 |
t.test('dot key', async t => { |
| 241 |
const dotKey = 'this.is.a.single.key' |
| 242 |
mockGlobals(t, { |
| 243 |
[`process.env."${dotKey}"`]: 'value', |
| 244 |
}) |
| 245 |
t.strictSame(process.env[dotKey], 'value') |
| 246 |
}) |
| 247 |
|
| 248 |
t.test('multiple mocks and resets', async (t) => { |
| 249 |
const initial = 'a' |
| 250 |
const platforms = ['b', 'c', 'd', 'e', 'f', 'g'] |
| 251 |
|
| 252 |
await t.test('first in, first out', async t => { |
| 253 |
mockGlobals(t, { 'process.platform': initial }) |
| 254 |
t.equal(process.platform, initial) |
| 255 |
|
| 256 |
await t.test('platforms', async (t) => { |
| 257 |
const resets = platforms.map((platform) => { |
| 258 |
const { reset } = mockGlobals(t, { 'process.platform': platform }) |
| 259 |
t.equal(process.platform, platform) |
| 260 |
return reset['process.platform'] |
| 261 |
}).reverse() |
| 262 |
|
| 263 |
;[...platforms.reverse()].forEach((platform, index) => { |
| 264 |
const reset = resets[index] |
| 265 |
const nextPlatform = index === platforms.length - 1 ? initial : platforms[index + 1] |
| 266 |
t.equal(process.platform, platform) |
| 267 |
reset() |
| 268 |
t.equal(process.platform, nextPlatform, 'first reset') |
| 269 |
reset() |
| 270 |
reset() |
| 271 |
t.equal(process.platform, nextPlatform, 'multiple resets are idempotent') |
| 272 |
}) |
| 273 |
}) |
| 274 |
|
| 275 |
t.equal(process.platform, initial) |
| 276 |
}) |
| 277 |
|
| 278 |
await t.test('last in,first out', async t => { |
| 279 |
mockGlobals(t, { 'process.platform': initial }) |
| 280 |
t.equal(process.platform, initial) |
| 281 |
|
| 282 |
await t.test('platforms', async (t) => { |
| 283 |
const resets = platforms.map((platform) => { |
| 284 |
const { reset } = mockGlobals(t, { 'process.platform': platform }) |
| 285 |
t.equal(process.platform, platform) |
| 286 |
return reset['process.platform'] |
| 287 |
}) |
| 288 |
|
| 289 |
resets.forEach((reset, index) => { |
| 290 |
// Calling a reset out of order removes it from the stack |
| 291 |
// but does not change the descriptor so it should still be the |
| 292 |
// last in descriptor until there are none left |
| 293 |
const lastPlatform = platforms[platforms.length - 1] |
| 294 |
const nextPlatform = index === platforms.length - 1 ? initial : lastPlatform |
| 295 |
t.equal(process.platform, lastPlatform) |
| 296 |
reset() |
| 297 |
t.equal(process.platform, nextPlatform, 'multiple resets are idempotent') |
| 298 |
reset() |
| 299 |
reset() |
| 300 |
t.equal(process.platform, nextPlatform, 'multiple resets are idempotent') |
| 301 |
}) |
| 302 |
}) |
| 303 |
|
| 304 |
t.equal(process.platform, initial) |
| 305 |
}) |
| 306 |
|
| 307 |
t.test('reset all', async (t) => { |
| 308 |
const { teardown } = mockGlobals(t, { 'process.platform': initial }) |
| 309 |
|
| 310 |
await t.test('platforms', async (t) => { |
| 311 |
const resets = platforms.map((p) => { |
| 312 |
const { teardown: nestedTeardown, reset } = mockGlobals(t, { 'process.platform': p }) |
| 313 |
t.equal(process.platform, p) |
| 314 |
return [ |
| 315 |
reset['process.platform'], |
| 316 |
nestedTeardown, |
| 317 |
] |
| 318 |
}) |
| 319 |
|
| 320 |
resets.forEach(r => r[1]()) |
| 321 |
t.equal(process.platform, initial, 'teardown goes to initial value') |
| 322 |
|
| 323 |
resets.forEach((r) => r[0]()) |
| 324 |
t.equal(process.platform, initial, 'calling resets after teardown does nothing') |
| 325 |
}) |
| 326 |
|
| 327 |
t.equal(process.platform, initial) |
| 328 |
teardown() |
| 329 |
t.equal(process.platform, originals.platform) |
| 330 |
}) |
| 331 |
}) |