tschaub / mock-fs

Configurable mock for the fs module
https://npmjs.org/package/mock-fs
Other
906 stars 85 forks source link

Fails to load JSON with escape character #310

Closed dbmundell closed 4 years ago

dbmundell commented 4 years ago

When loading a JSON formatted fake file like this:

{ "something": "ONE-THING\tANOTHER " }

I get an error saying that an unexpected character was found - removing \t successfully loads the file

3cp commented 4 years ago

There is nothing wrong at mock-fs side. You probably has code to trigger that error after reading file content.

Following code runs as expected.

const fs = require('fs');
const mockfs = require('mock-fs');

mockfs({
  "something": "ONE-THING\tANOTHER "
});

const content = fs.readFileSync('something', 'utf8');
console.log(content);
3cp commented 4 years ago

Note if you write the file content in code, you have to escape \ for the json file.

mockfs({
  "a.json": `{"something":"ONE-THING\\tANOTHER "}`
});

Because in json file, you need literally two characters: \ and t. If you write \t inside js code string, it becomes only one character (the tab character), not two.