-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
promises.test.ts
662 lines (659 loc) · 23.8 KB
/
promises.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import { Volume } from '../volume';
describe('Promises API', () => {
describe('FileHandle', () => {
it('API should have a FileHandle property', () => {
const vol = new Volume();
const { promises } = vol;
expect(typeof promises.FileHandle).toBe('function');
});
describe('fd', () => {
it('FileHandle should have a fd property', async () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
const fileHandle = await promises.open('/foo', 'r');
expect(typeof fileHandle.fd).toEqual('number');
await fileHandle.close();
});
});
describe('appendFile(data[, options])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Append data to an existing file', async () => {
const fileHandle = await promises.open('/foo', 'a');
await fileHandle.appendFile('baz');
expect(vol.readFileSync('/foo').toString()).toEqual('barbaz');
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'a');
await fileHandle.close();
return expect(fileHandle.appendFile('/foo', 'baz')).rejects.toBeInstanceOf(Error);
});
});
describe('chmod(mode)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Change mode of existing file', async () => {
const fileHandle = await promises.open('/foo', 'a');
await fileHandle.chmod(0o444);
expect(vol.statSync('/foo').mode & 0o777).toEqual(0o444);
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'a');
await fileHandle.close();
return expect(fileHandle.chmod(0o666)).rejects.toBeInstanceOf(Error);
});
});
describe('chown(uid, gid)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
const { uid, gid } = vol.statSync('/foo');
it('Change uid and gid of existing file', async () => {
const fileHandle = await promises.open('/foo', 'a');
await fileHandle.chown(uid + 1, gid + 1);
const stats = vol.statSync('/foo');
expect(stats.uid).toEqual(uid + 1);
expect(stats.gid).toEqual(gid + 1);
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'a');
await fileHandle.close();
return expect(fileHandle.chown(uid + 2, gid + 2)).rejects.toBeInstanceOf(Error);
});
});
// close(): covered by all other tests
describe('datasync()', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Synchronize data with an existing file', async () => {
const fileHandle = await promises.open('/foo', 'r+');
await fileHandle.datasync();
expect(vol.readFileSync('/foo').toString()).toEqual('bar');
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'r+');
await fileHandle.close();
return expect(fileHandle.datasync()).rejects.toBeInstanceOf(Error);
});
});
describe('read(buffer, offset, length, position)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Read data from an existing file', async () => {
const fileHandle = await promises.open('/foo', 'r+');
const buff = Buffer.from('foo');
const { bytesRead, buffer } = await fileHandle.read(buff, 0, 42, 0);
expect(bytesRead).toEqual(3);
expect(buffer).toBe(buff);
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'r+');
await fileHandle.close();
return expect(fileHandle.read(Buffer.from('foo'), 0, 42, 0)).rejects.toBeInstanceOf(Error);
});
});
describe('readFile([options])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Read data from an existing file', async () => {
const fileHandle = await promises.open('/foo', 'r+');
expect((await fileHandle.readFile()).toString()).toEqual('bar');
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'r+');
await fileHandle.close();
return expect(fileHandle.readFile()).rejects.toBeInstanceOf(Error);
});
});
describe('stat()', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Return stats of an existing file', async () => {
const fileHandle = await promises.open('/foo', 'r+');
expect((await fileHandle.stat()).isFile()).toEqual(true);
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'r+');
await fileHandle.close();
return expect(fileHandle.stat()).rejects.toBeInstanceOf(Error);
});
});
describe('.stat(path, options)', () => {
const { promises: vol } = new Volume();
it('Does not reject when entry does not exist if throwIfNoEntry is false', async () => {
const stat = await vol.stat('/no', { throwIfNoEntry: false });
expect(stat).toBeUndefined();
});
it('Rejects when entry does not exist if throwIfNoEntry is true', async () => {
await expect(vol.stat('/foo', { throwIfNoEntry: true })).rejects.toBeInstanceOf(Error);
});
it('Rejects when entry does not exist if throwIfNoEntry is not specified', async () => {
await expect(vol.stat('/foo')).rejects.toBeInstanceOf(Error);
});
it('Rejects when entry does not exist if throwIfNoEntry is explicitly undefined', async () => {
await expect(vol.stat('/foo', { throwIfNoEntry: undefined })).rejects.toBeInstanceOf(Error);
});
});
describe('.lstat(path, options)', () => {
const { promises: vol } = new Volume();
it('Does not throw when entry does not exist if throwIfNoEntry is false', async () => {
const stat = await vol.lstat('/foo', { throwIfNoEntry: false });
expect(stat).toBeUndefined();
});
it('Rejects when entry does not exist if throwIfNoEntry is true', async () => {
await expect(vol.lstat('/foo', { throwIfNoEntry: true })).rejects.toBeInstanceOf(Error);
});
it('Rejects when entry does not exist if throwIfNoEntry is not specified', async () => {
await expect(vol.lstat('/foo')).rejects.toBeInstanceOf(Error);
});
it('Rejects when entry does not exist if throwIfNoEntry is explicitly undefined', async () => {
await expect(vol.lstat('/foo', { throwIfNoEntry: undefined })).rejects.toBeInstanceOf(Error);
});
});
describe('truncate([len])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': '0123456789',
});
it('Truncate an existing file', async () => {
const fileHandle = await promises.open('/foo', 'r+');
await fileHandle.truncate(5);
expect(vol.readFileSync('/foo').toString()).toEqual('01234');
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'r+');
await fileHandle.close();
return expect(fileHandle.truncate(5)).rejects.toBeInstanceOf(Error);
});
});
describe('utimes(atime, mtime)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': '0123456789',
});
const fttDeparture = new Date(1985, 9, 26, 1, 21); // ftt stands for "first time travel" :-)
const fttArrival = new Date(fttDeparture.getTime() + 60000);
it('Changes times of an existing file', async () => {
const fileHandle = await promises.open('/foo', 'r+');
await fileHandle.utimes(fttArrival, fttDeparture);
const stats = vol.statSync('/foo');
expect(stats.atime).toEqual(new Date(fttArrival as any));
expect(stats.mtime).toEqual(new Date(fttDeparture as any));
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'r+');
await fileHandle.close();
return expect(fileHandle.utimes(fttArrival, fttDeparture)).rejects.toBeInstanceOf(Error);
});
});
describe('write(buffer[, offset[, length[, position]]])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Write data to an existing file', async () => {
const fileHandle = await promises.open('/foo', 'w');
await fileHandle.write(Buffer.from('foo'));
expect(vol.readFileSync('/foo').toString()).toEqual('foo');
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'w');
await fileHandle.close();
return expect(fileHandle.write(Buffer.from('foo'))).rejects.toBeInstanceOf(Error);
});
});
describe('writeFile(data[, options])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Write data to an existing file', async () => {
const fileHandle = await promises.open('/foo', 'w');
await fileHandle.writeFile('foo');
expect(vol.readFileSync('/foo').toString()).toEqual('foo');
await fileHandle.close();
});
it('Reject when the file handle was closed', async () => {
const fileHandle = await promises.open('/foo', 'w');
await fileHandle.close();
return expect(fileHandle.writeFile('foo')).rejects.toBeInstanceOf(Error);
});
});
});
describe('access(path[, mode])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Resolve when file exists', () => {
return expect(promises.access('/foo')).resolves.toBeUndefined();
});
it('Reject when file does not exist', () => {
return expect(promises.access('/bar')).rejects.toBeInstanceOf(Error);
});
});
describe('appendFile(path, data[, options])', () => {
it('Append data to existing file', async () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
await promises.appendFile('/foo', 'baz');
expect(vol.readFileSync('/foo').toString()).toEqual('barbaz');
});
it('Append data to existing file using FileHandle', async () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
const fileHandle = await promises.open('/foo', 'a');
await promises.appendFile(fileHandle, 'baz');
await fileHandle.close();
expect(vol.readFileSync('/foo').toString()).toEqual('barbaz');
});
it('Reject when trying to write on a directory', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': null,
});
return expect(promises.appendFile('/foo', 'bar')).rejects.toBeInstanceOf(Error);
});
});
describe('chmod(path, mode)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Change mode of existing file', async () => {
await promises.chmod('/foo', 0o444);
expect(vol.statSync('/foo').mode & 0o777).toEqual(0o444);
});
it('Reject when file does not exist', () => {
return expect(promises.chmod('/bar', 0o444)).rejects.toBeInstanceOf(Error);
});
});
describe('chown(path, uid, gid)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Change uid and gid of existing file', async () => {
const { uid, gid } = vol.statSync('/foo');
await promises.chown('/foo', uid + 1, gid + 1);
const stats = vol.statSync('/foo');
expect(stats.uid).toEqual(uid + 1);
expect(stats.gid).toEqual(gid + 1);
});
it('Reject when file does not exist', () => {
return expect(promises.chown('/bar', 0, 0)).rejects.toBeInstanceOf(Error);
});
});
describe('copyFile(src, dest[, flags])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Copy existing file', async () => {
await promises.copyFile('/foo', '/bar');
expect(vol.readFileSync('/bar').toString()).toEqual('bar');
});
it('Reject when file does not exist', () => {
return expect(promises.copyFile('/baz', '/qux')).rejects.toBeInstanceOf(Error);
});
});
describe('lchmod(path, mode)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
vol.symlinkSync('/foo', '/bar');
it('Change mode of existing file', async () => {
await promises.lchmod('/bar', 0o444);
expect(vol.statSync('/foo').mode & 0o777).toEqual(0o666);
expect(vol.lstatSync('/bar').mode & 0o777).toEqual(0o444);
});
it('Reject when file does not exist', () => {
return expect(promises.lchmod('/baz', 0o444)).rejects.toBeInstanceOf(Error);
});
});
describe('lchown(path, uid, gid)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
vol.symlinkSync('/foo', '/bar');
it('Change uid and gid of existing file', async () => {
const fooStatsBefore = vol.statSync('/foo');
const { uid, gid } = vol.statSync('/bar');
await promises.lchown('/bar', uid + 1, gid + 1);
const fooStatsAfter = vol.statSync('/foo');
expect(fooStatsAfter.uid).toEqual(fooStatsBefore.uid);
expect(fooStatsAfter.gid).toEqual(fooStatsBefore.gid);
const stats = vol.lstatSync('/bar');
expect(stats.uid).toEqual(uid + 1);
expect(stats.gid).toEqual(gid + 1);
});
it('Reject when file does not exist', () => {
return expect(promises.lchown('/baz', 0, 0)).rejects.toBeInstanceOf(Error);
});
});
describe('link(existingPath, newPath)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Create hard link on existing file', async () => {
await promises.link('/foo', '/bar');
expect(vol.existsSync('/bar')).toEqual(true);
});
it('Reject when file does not exist', () => {
return expect(promises.link('/baz', '/qux')).rejects.toBeInstanceOf(Error);
});
});
describe('lstat(path)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
vol.symlinkSync('/foo', '/bar');
it('Get stats on an existing symbolic link', async () => {
const stats = await promises.lstat('/bar');
expect(stats.isSymbolicLink()).toEqual(true);
});
it('Reject when symbolic link does not exist', () => {
return expect(promises.lstat('/baz')).rejects.toBeInstanceOf(Error);
});
});
describe('mkdir(path[, options])', () => {
const vol = new Volume();
const { promises } = vol;
it('Creates a directory', async () => {
await promises.mkdir('/foo');
expect(vol.statSync('/foo').isDirectory()).toEqual(true);
});
it('Reject when a file already exists', () => {
vol.writeFileSync('/bar', 'bar');
return expect(promises.mkdir('/bar')).rejects.toBeInstanceOf(Error);
});
});
describe('mkdtemp(prefix[, options])', () => {
const vol = new Volume();
const { promises } = vol;
it('Creates a temporary directory', async () => {
const tmp = await promises.mkdtemp('/foo');
expect(vol.statSync(tmp).isDirectory()).toEqual(true);
});
it('Reject when parent directory does not exist', () => {
return expect(promises.mkdtemp('/foo/bar')).rejects.toBeInstanceOf(Error);
});
});
describe('open(path, flags[, mode])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Open an existing file', async () => {
expect(await promises.open('/foo', 'r')).toBeInstanceOf(promises.FileHandle);
});
it('Reject when file does not exist', () => {
return expect(promises.open('/bar', 'r')).rejects.toBeInstanceOf(Error);
});
});
describe('readdir(path[, options])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': null,
'/foo/bar': 'bar',
'/foo/baz': 'baz',
});
it('Read an existing directory', async () => {
expect(await promises.readdir('/foo')).toEqual(['bar', 'baz']);
});
it('Reject when directory does not exist', () => {
return expect(promises.readdir('/bar')).rejects.toBeInstanceOf(Error);
});
});
describe('readFile(id[, options])', () => {
it('Read existing file', async () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
expect((await promises.readFile('/foo')).toString()).toEqual('bar');
});
it('Read existing file using FileHandle', async () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
const fileHandle = await promises.open('/foo', 'r');
expect((await promises.readFile(fileHandle)).toString()).toEqual('bar');
await fileHandle.close();
});
it('Reject when file does not exist', () => {
const vol = new Volume();
const { promises } = vol;
return expect(promises.readFile('/foo')).rejects.toBeInstanceOf(Error);
});
});
describe('readlink(path[, options])', () => {
const vol = new Volume();
const { promises } = vol;
vol.symlinkSync('/foo', '/bar');
it('Read an existing symbolic link', async () => {
expect((await promises.readlink('/bar')).toString()).toEqual('/foo');
});
it('Reject when symbolic link does not exist', () => {
return expect(promises.readlink('/foo')).rejects.toBeInstanceOf(Error);
});
});
describe('realpath(path[, options])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': null,
'/foo/bar': null,
'/foo/baz': 'baz',
});
vol.symlinkSync('/foo/baz', '/foo/qux');
it('Return real path of existing file', async () => {
expect((await promises.realpath('/foo/bar/../qux')).toString()).toEqual('/foo/baz');
});
it('Reject when file does not exist', () => {
return expect(promises.realpath('/bar')).rejects.toBeInstanceOf(Error);
});
});
describe('rename(oldPath, newPath)', () => {
it('Rename existing file', async () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
await promises.rename('/foo', '/bar');
expect(vol.readFileSync('/bar').toString()).toEqual('bar');
});
it('Reject when file does not exist', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
return expect(promises.rename('/bar', '/baz')).rejects.toBeInstanceOf(Error);
});
});
describe('rmdir(path)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': null,
});
it('Remove an existing directory', async () => {
await promises.rmdir('/foo');
expect(vol.existsSync('/foo')).toEqual(false);
});
it('Reject when directory does not exist', () => {
return expect(promises.rmdir('/bar')).rejects.toBeInstanceOf(Error);
});
});
describe('stat(path)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': null,
});
it('Return stats of an existing directory', async () => {
expect((await promises.stat('/foo')).isDirectory()).toEqual(true);
});
it('Reject when directory does not exist', () => {
return expect(promises.stat('/bar')).rejects.toBeInstanceOf(Error);
});
});
describe('symlink(target, path[, type])', () => {
it('Create symbolic link', async () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
await promises.symlink('/foo', '/bar');
expect(vol.lstatSync('/bar').isSymbolicLink()).toEqual(true);
});
it('Reject when file already exists', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
return expect(promises.symlink('/bar', '/foo')).rejects.toBeInstanceOf(Error);
});
});
describe('truncate(path[, len])', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': '0123456789',
});
it('Truncate an existing file', async () => {
await promises.truncate('/foo', 5);
expect(vol.readFileSync('/foo').toString()).toEqual('01234');
});
it('Reject when file does not exist', () => {
return expect(promises.truncate('/bar')).rejects.toBeInstanceOf(Error);
});
});
describe('unlink(path)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
it('Unlink an existing file', async () => {
await promises.unlink('/foo');
expect(vol.existsSync('/foo')).toEqual(false);
});
it('Reject when file does not exist', () => {
return expect(promises.unlink('/bar')).rejects.toBeInstanceOf(Error);
});
});
describe('utimes(path, atime, mtime)', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': 'bar',
});
const fttDeparture = new Date(1985, 9, 26, 1, 21);
const fttArrival = new Date(fttDeparture.getTime() + 60000);
it('Changes times of an existing file', async () => {
await promises.utimes('/foo', fttArrival, fttDeparture);
const stats = vol.statSync('/foo');
expect(stats.atime).toEqual(new Date(fttArrival as any));
expect(stats.mtime).toEqual(new Date(fttDeparture as any));
});
it('Reject when file does not exist', () => {
return expect(promises.utimes('/bar', fttArrival, fttDeparture)).rejects.toBeInstanceOf(Error);
});
});
describe('writeFile(id, data[, options])', () => {
it('Write data to an existing file', async () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': '',
});
await promises.writeFile('/foo', 'bar');
expect(vol.readFileSync('/foo').toString()).toEqual('bar');
});
it('Write data to existing file using FileHandle', async () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': '',
});
const fileHandle = await promises.open('/foo', 'w');
await promises.writeFile(fileHandle, 'bar');
expect(vol.readFileSync('/foo').toString()).toEqual('bar');
await fileHandle.close();
});
it('Reject when trying to write on a directory', () => {
const vol = new Volume();
const { promises } = vol;
vol.fromJSON({
'/foo': null,
});
return expect(promises.writeFile('/foo', 'bar')).rejects.toBeInstanceOf(Error);
});
});
});