From 3d6ee3b2c0eef0345ba2bd400e9836f2d685321f Mon Sep 17 00:00:00 2001 From: Florian Loch Date: Thu, 14 May 2020 00:23:01 +0200 Subject: [PATCH] fix: 'fromJSON()' did not consider cwd when creating directories --- src/__tests__/volume.test.ts | 4 +++- src/volume.ts | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/__tests__/volume.test.ts b/src/__tests__/volume.test.ts index bea1f6a9..efa8ca7d 100644 --- a/src/__tests__/volume.test.ts +++ b/src/__tests__/volume.test.ts @@ -197,16 +197,18 @@ describe('volume', () => { expect(vol.toJSON()).toEqual(json); }); - it('Files at root with relative paths', () => { + it('Files and directories at root with relative paths', () => { const vol = new Volume(); const json = { hello: 'world', 'app.js': 'console.log(123)', + dir: {}, }; vol.fromJSON(json, '/'); expect(vol.toJSON()).toEqual({ '/hello': 'world', '/app.js': 'console.log(123)', + '/dir': null, }); }); diff --git a/src/volume.ts b/src/volume.ts index f0773e7b..6ea7d1c3 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -872,8 +872,9 @@ export class Volume { for (let filename in json) { const data = json[filename]; + filename = resolve(filename, cwd); + if (typeof data === 'string') { - filename = resolve(filename, cwd); const steps = filenameToSteps(filename); if (steps.length > 1) { const dirname = sep + steps.slice(0, steps.length - 1).join(sep);