|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const helper = require('../helper');
|
| 4 | +const fs = require('fs'); |
| 5 | +const mock = require('../../lib/index'); |
4 | 6 | const {
|
5 | 7 | patchReadFileContext,
|
6 | 8 | getReadFileContextPrototype
|
7 | 9 | } = require('../../lib/readfilecontext');
|
8 | 10 |
|
9 | 11 | const assert = helper.assert;
|
| 12 | +const inVersion = helper.inVersion; |
10 | 13 |
|
11 | 14 | describe('getReadFileContextPrototype', function() {
|
12 | 15 | it('provides access to the internal ReadFileContext', function() {
|
@@ -105,3 +108,41 @@ describe('patchReadFileContext', function() {
|
105 | 108 | });
|
106 | 109 | });
|
107 | 110 | });
|
| 111 | + |
| 112 | +describe('fs.readFile() with ReadFileContext', function() { |
| 113 | + // fs.readFile() is already tested elsewhere, here we just make sure we have |
| 114 | + // coverage of the mocked ReadFileContext implementation. |
| 115 | + |
| 116 | + beforeEach(function() { |
| 117 | + mock({ |
| 118 | + 'path/to/file.txt': 'file content', |
| 119 | + 1: 'fd content' |
| 120 | + }); |
| 121 | + }); |
| 122 | + afterEach(mock.restore); |
| 123 | + |
| 124 | + inVersion('>=15.0.0').it('allows file reads to be aborted', function(done) { |
| 125 | + const controller = new AbortController(); |
| 126 | + const {signal} = controller; |
| 127 | + |
| 128 | + fs.readFile('path/to/file.txt', {signal}, function(err) { |
| 129 | + assert.instanceOf(err, Error); |
| 130 | + assert.equal(err.name, 'AbortError'); |
| 131 | + assert.equal(err.code, 'ABORT_ERR'); |
| 132 | + done(); |
| 133 | + }); |
| 134 | + |
| 135 | + // By aborting after the call it will be handled by the context rather than readFile() |
| 136 | + controller.abort(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('allows file reads with a numeric descriptor', function(done) { |
| 140 | + // This isn't actually supported by mock-fs, but let's make sure the call goes through |
| 141 | + // It also covers the case of reading an empty file and reading with encoding |
| 142 | + fs.readFile(1, 'utf-8', function(err, data) { |
| 143 | + assert.isNull(err); |
| 144 | + assert.equal(data, ''); |
| 145 | + done(); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments