Skip to content

Commit

Permalink
squash: add tests for stringable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros committed Jan 25, 2022
1 parent 1a0df53 commit b9cf3e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion test/parallel/test-fs-promises-file-handle-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ async function validateNonUint8ArrayWrite() {
async function validateNonStringValuesWrite() {
const filePathForHandle = path.resolve(tmpDir, 'tmp-non-string-write.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const nonStringValues = [123, {}, new Map()];
const nonStringValues = [
123, {}, new Map(),
new String('notPrimitive'),
{ toString() { return 'amObject'; } },
{ [Symbol.toPrimitive]: (hint) => 'amObject' },
];
for (const nonStringValue of nonStringValues) {
await assert.rejects(
fileHandle.write(nonStringValue),
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-fs-write-sync-optional-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ const buffer = Buffer.from('zyx');
'It must be >= 0 && <= 9007199254740991. Received -1'
});

// Test if object is not interpreted as string
for (const buffer of [
{},
new Date(),
new String('notPrimitive'),
{ toString() { return 'amObject'; } },
{ [Symbol.toPrimitive]: (hint) => 'amObject' },
]) {
assert.throws(() => {
fs.writeSync(fd, buffer);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "buffer" argument must be of type string or an instance of Buffer, TypedArray, or DataView/
});
}

fs.closeSync(fd);

for (const params of [
Expand Down

0 comments on commit b9cf3e0

Please sign in to comment.