Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
/.project
.vscode/
package-lock.json

26 changes: 26 additions & 0 deletions test/write-file-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,30 @@ describe('+ writeFileSync()', () => {
done()
})
})

describe('> when obj is not serializable', () => {
it('should throw a TypeError when obj is undefined', () => {
const file = path.join(TEST_DIR, 'somefile.json')
assert.throws(
() => jf.writeFileSync(file, undefined),
{ name: 'TypeError', message: 'Converting undefined value to JSON is not supported' }
)
})

it('should throw a TypeError when obj is a function', () => {
const file = path.join(TEST_DIR, 'somefile.json')
assert.throws(
() => jf.writeFileSync(file, function () {}),
{ name: 'TypeError', message: 'Converting function value to JSON is not supported' }
)
})

it('should throw a TypeError when obj is a Symbol', () => {
const file = path.join(TEST_DIR, 'somefile.json')
assert.throws(
() => jf.writeFileSync(file, Symbol('test')),
{ name: 'TypeError', message: 'Converting symbol value to JSON is not supported' }
)
})
})
})
40 changes: 40 additions & 0 deletions test/write-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,44 @@ describe('+ writeFile()', () => {
})
})
})

describe('> when obj is not serializable', () => {
it('should reject the promise with a TypeError when obj is undefined', (done) => {
const file = path.join(TEST_DIR, 'somefile.json')
jf.writeFile(file, undefined)
.catch(err => {
assert(err instanceof TypeError)
assert.strictEqual(err.message, 'Converting undefined value to JSON is not supported')
done()
})
})

it('should pass a TypeError to callback when obj is undefined', (done) => {
const file = path.join(TEST_DIR, 'somefile.json')
jf.writeFile(file, undefined, (err) => {
assert(err instanceof TypeError)
assert.strictEqual(err.message, 'Converting undefined value to JSON is not supported')
done()
})
})

it('should pass a TypeError to callback when obj is a function', (done) => {
const file = path.join(TEST_DIR, 'somefile.json')
jf.writeFile(file, function () {}, (err) => {
assert(err instanceof TypeError)
assert.strictEqual(err.message, 'Converting function value to JSON is not supported')
done()
})
})

it('should reject the promise with a TypeError when obj is a Symbol', (done) => {
const file = path.join(TEST_DIR, 'somefile.json')
jf.writeFile(file, Symbol('test'))
.catch(err => {
assert(err instanceof TypeError)
assert.strictEqual(err.message, 'Converting symbol value to JSON is not supported')
done()
})
})
})
})
4 changes: 4 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ function stringify (obj, { EOL = '\n', finalEOL = true, replacer = null, spaces
const EOF = finalEOL ? EOL : ''
const str = JSON.stringify(obj, replacer, spaces)

if (str === undefined) {
throw new TypeError(`Converting ${typeof obj} value to JSON is not supported`)
}

return str.replace(/\n/g, EOL) + EOF
}

Expand Down
Loading