Skip to content

Commit b9cf3e0

Browse files
committed
squash: add tests for stringable objects
1 parent 1a0df53 commit b9cf3e0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

test/parallel/test-fs-promises-file-handle-write.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ async function validateNonUint8ArrayWrite() {
5353
async function validateNonStringValuesWrite() {
5454
const filePathForHandle = path.resolve(tmpDir, 'tmp-non-string-write.txt');
5555
const fileHandle = await open(filePathForHandle, 'w+');
56-
const nonStringValues = [123, {}, new Map()];
56+
const nonStringValues = [
57+
123, {}, new Map(),
58+
new String('notPrimitive'),
59+
{ toString() { return 'amObject'; } },
60+
{ [Symbol.toPrimitive]: (hint) => 'amObject' },
61+
];
5762
for (const nonStringValue of nonStringValues) {
5863
await assert.rejects(
5964
fileHandle.write(nonStringValue),

test/parallel/test-fs-write-sync-optional-params.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ const buffer = Buffer.from('zyx');
7171
'It must be >= 0 && <= 9007199254740991. Received -1'
7272
});
7373

74+
// Test if object is not interpreted as string
75+
for (const buffer of [
76+
{},
77+
new Date(),
78+
new String('notPrimitive'),
79+
{ toString() { return 'amObject'; } },
80+
{ [Symbol.toPrimitive]: (hint) => 'amObject' },
81+
]) {
82+
assert.throws(() => {
83+
fs.writeSync(fd, buffer);
84+
}, {
85+
code: 'ERR_INVALID_ARG_TYPE',
86+
name: 'TypeError',
87+
message: /^The "buffer" argument must be of type string or an instance of Buffer, TypedArray, or DataView/
88+
});
89+
}
90+
7491
fs.closeSync(fd);
7592

7693
for (const params of [

0 commit comments

Comments
 (0)