diff --git a/src/__tests__/volume.test.ts b/src/__tests__/volume.test.ts index d1181ab26..a8b1ffb3b 100644 --- a/src/__tests__/volume.test.ts +++ b/src/__tests__/volume.test.ts @@ -196,6 +196,15 @@ describe('volume', () => { const result = vol.toJSON('/', {}, false, true)['/file']; expect(result).toStrictEqual(buffer); }); + + it('Outputs files in subdirectories as buffers too', () => { + const buffer = Buffer.from('Hello'); + const vol = new Volume(); + vol.mkdirSync('/dir'); + vol.writeFileSync('/dir/file', buffer); + const result = vol.toJSON('/', {}, false, true)['/dir/file']; + expect(result).toStrictEqual(buffer); + }); }); describe('.fromJSON(json[, cwd])', () => { diff --git a/src/volume.ts b/src/volume.ts index 32ff2c99c..ac5e0ccc2 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -554,7 +554,7 @@ export class Volume implements FsCallbackApi, FsSynchronousApi { if (path) filename = relative(path, filename); json[filename] = asBuffer ? node.getBuffer() : node.getString(); } else if (node.isDirectory()) { - this._toJSON(child, json, path); + this._toJSON(child, json, path, asBuffer); } }