|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const helper = require('../helper'); |
| 4 | +const fs = require('fs'); |
| 5 | +const mock = require('../../lib/index'); |
| 6 | +const path = require('path'); |
| 7 | +const withPromise = helper.withPromise; |
| 8 | + |
| 9 | +const assert = helper.assert; |
| 10 | + |
| 11 | +describe('mock.bypass()', () => { |
| 12 | + afterEach(mock.restore); |
| 13 | + |
| 14 | + it('runs a synchronous function using the real filesystem', () => { |
| 15 | + mock({'/path/to/file': 'content'}); |
| 16 | + |
| 17 | + assert.equal(fs.readFileSync('/path/to/file', 'utf8'), 'content'); |
| 18 | + assert.isNotOk(fs.existsSync(__filename)); |
| 19 | + assert.isOk(mock.bypass(() => fs.existsSync(__filename))); |
| 20 | + |
| 21 | + assert.isNotOk(fs.existsSync(__filename)); |
| 22 | + }); |
| 23 | + |
| 24 | + it('handles functions that throw', () => { |
| 25 | + mock({'/path/to/file': 'content'}); |
| 26 | + |
| 27 | + const error = new Error('oops'); |
| 28 | + |
| 29 | + assert.throws(() => { |
| 30 | + mock.bypass(() => { |
| 31 | + assert.isFalse(fs.existsSync('/path/to/file')); |
| 32 | + throw error; |
| 33 | + }); |
| 34 | + }, error); |
| 35 | + |
| 36 | + assert.equal(fs.readFileSync('/path/to/file', 'utf8'), 'content'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('bypasses patched process.cwd()', () => { |
| 40 | + const originalCwd = process.cwd(); |
| 41 | + mock({ |
| 42 | + dir: {} |
| 43 | + }); |
| 44 | + |
| 45 | + process.chdir('dir'); |
| 46 | + assert.equal(process.cwd(), path.join(originalCwd, 'dir')); |
| 47 | + |
| 48 | + mock.bypass(() => { |
| 49 | + assert.equal(process.cwd(), originalCwd); |
| 50 | + }); |
| 51 | + assert.equal(process.cwd(), path.join(originalCwd, 'dir')); |
| 52 | + mock.restore(); |
| 53 | + |
| 54 | + assert.equal(process.cwd(), originalCwd); |
| 55 | + }); |
| 56 | + |
| 57 | + withPromise.it('runs an async function using the real filesystem', done => { |
| 58 | + mock({'/path/to/file': 'content'}); |
| 59 | + |
| 60 | + assert.equal(fs.readFileSync('/path/to/file', 'utf8'), 'content'); |
| 61 | + assert.isFalse(fs.existsSync(__filename)); |
| 62 | + |
| 63 | + const promise = mock.bypass(() => fs.promises.stat(__filename)); |
| 64 | + assert.instanceOf(promise, Promise); |
| 65 | + |
| 66 | + promise |
| 67 | + .then(stat => { |
| 68 | + assert.isTrue(stat.isFile()); |
| 69 | + assert.isFalse(fs.existsSync(__filename)); |
| 70 | + done(); |
| 71 | + }) |
| 72 | + .catch(done); |
| 73 | + }); |
| 74 | + |
| 75 | + withPromise.it('handles promise rejection', done => { |
| 76 | + mock({'/path/to/file': 'content'}); |
| 77 | + |
| 78 | + assert.equal(fs.readFileSync('/path/to/file', 'utf8'), 'content'); |
| 79 | + assert.isFalse(fs.existsSync(__filename)); |
| 80 | + |
| 81 | + const error = new Error('oops'); |
| 82 | + |
| 83 | + const promise = mock.bypass(() => { |
| 84 | + assert.isTrue(fs.existsSync(__filename)); |
| 85 | + return Promise.reject(error); |
| 86 | + }); |
| 87 | + assert.instanceOf(promise, Promise); |
| 88 | + |
| 89 | + promise |
| 90 | + .then(() => { |
| 91 | + done(new Error('expected rejection')); |
| 92 | + }) |
| 93 | + .catch(err => { |
| 94 | + assert.equal(err, error); |
| 95 | + |
| 96 | + assert.equal(fs.readFileSync('/path/to/file', 'utf8'), 'content'); |
| 97 | + done(); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments