/
read.test.js
48 lines (40 loc) · 1.17 KB
/
read.test.js
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
'use strict'
const fs = require('fs')
const os = require('os')
const fse = require('../..')
const path = require('path')
const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
describe('read', () => {
let TEST_DIR
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'read-json')
fse.emptyDir(TEST_DIR, done)
})
afterEach(done => fse.remove(TEST_DIR, done))
describe('+ readJSON', () => {
it('should read a file and parse the json', done => {
const obj1 = {
firstName: 'JP',
lastName: 'Richardson'
}
const file = path.join(TEST_DIR, 'file.json')
fs.writeFileSync(file, JSON.stringify(obj1))
fse.readJSON(file, (err, obj2) => {
assert.ifError(err)
assert.strictEqual(obj1.firstName, obj2.firstName)
assert.strictEqual(obj1.lastName, obj2.lastName)
done()
})
})
it('should error if it cant parse the json', done => {
const file = path.join(TEST_DIR, 'file2.json')
fs.writeFileSync(file, '%asdfasdff444')
fse.readJSON(file, (err, obj) => {
assert(err)
assert(!obj)
done()
})
})
})
})