mirror of
https://github.com/jprichardson/node-fs-extra.git
synced 2026-01-18 16:13:55 +00:00
* Remove process.cwd() trick from test files * BREAKING: Switch from main to exports * Add fs-extra/esm ESM named import module, with just fs-extra methods Fixes #746
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
'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()
|
|
})
|
|
})
|
|
})
|
|
})
|