diff --git a/test/js/node/test/parallel/buffer-tojson.test.js b/test/js/node/test/parallel/buffer-tojson.test.js new file mode 100644 index 0000000000..89a428a492 --- /dev/null +++ b/test/js/node/test/parallel/buffer-tojson.test.js @@ -0,0 +1,35 @@ +//#FILE: test-buffer-tojson.js +//#SHA1: 39b31549a09e67c89316a24c895db2dfab939ec4 +//----------------- +"use strict"; + +test("Buffer JSON serialization", () => { + expect(JSON.stringify(Buffer.alloc(0))).toBe('{"type":"Buffer","data":[]}'); + expect(JSON.stringify(Buffer.from([1, 2, 3, 4]))).toBe('{"type":"Buffer","data":[1,2,3,4]}'); +}); + +// issue GH-7849 +test("Buffer deserialization", () => { + const buf = Buffer.from("test"); + const json = JSON.stringify(buf); + const obj = JSON.parse(json); + const copy = Buffer.from(obj); + + expect(copy).toEqual(buf); +}); + +// GH-5110 +test("Buffer serialization and custom deserialization", () => { + const buffer = Buffer.from("test"); + const string = JSON.stringify(buffer); + + expect(string).toBe('{"type":"Buffer","data":[116,101,115,116]}'); + + function receiver(key, value) { + return value && value.type === "Buffer" ? Buffer.from(value.data) : value; + } + + expect(JSON.parse(string, receiver)).toEqual(buffer); +}); + +//<#END_FILE: test-buffer-tojson.js diff --git a/test/js/node/test/parallel/child-process-windows-hide.test.js b/test/js/node/test/parallel/child-process-windows-hide.test.js new file mode 100644 index 0000000000..45aa9f7ac6 --- /dev/null +++ b/test/js/node/test/parallel/child-process-windows-hide.test.js @@ -0,0 +1,54 @@ +//#FILE: test-child-process-windows-hide.js +//#SHA1: c3cc8bbd27694658607c3a7f42a8e7901aeab807 +//----------------- +"use strict"; + +const cp = require("child_process"); +const cmd = process.execPath; +const args = ["--print", "42"]; +const options = { windowsHide: true }; + +// Since windowsHide isn't really observable, we'll use Jest's mocking capabilities +// to verify that the flag is being passed through correctly. + +beforeEach(() => { + jest.spyOn(cp, "spawn"); + jest.spyOn(cp, "spawnSync"); +}); + +afterEach(() => { + jest.restoreAllMocks(); +}); + +test("spawnSync passes windowsHide option", () => { + const child = cp.spawnSync(cmd, args, options); + + expect(cp.spawnSync).toHaveBeenCalledWith(cmd, args, expect.objectContaining({ windowsHide: true })); + expect(child.status).toBe(0); + expect(child.signal).toBeNull(); + expect(child.stdout.toString().trim()).toBe("42"); + expect(child.stderr.toString().trim()).toBe(""); +}); + +test("spawn passes windowsHide option", done => { + const child = cp.spawn(cmd, args, options); + + expect(cp.spawn).toHaveBeenCalledWith(cmd, args, expect.objectContaining({ windowsHide: true })); + + child.on("exit", (code, signal) => { + expect(code).toBe(0); + expect(signal).toBeNull(); + done(); + }); +}); + +test("execFile passes windowsHide option", done => { + cp.execFile(cmd, args, options, (error, stdout, stderr) => { + expect(error).toBeNull(); + expect(stdout.trim()).toBe("42"); + expect(stderr.trim()).toBe(""); + done(); + }); +}); + +//<#END_FILE: test-child-process-windows-hide.js diff --git a/test/js/node/test/parallel/fs-buffertype-writesync.test.js b/test/js/node/test/parallel/fs-buffertype-writesync.test.js new file mode 100644 index 0000000000..79238a5b19 --- /dev/null +++ b/test/js/node/test/parallel/fs-buffertype-writesync.test.js @@ -0,0 +1,21 @@ +//#FILE: test-fs-buffertype-writesync.js +//#SHA1: 6af4aca43ae7299ed310d17733db6dcc43d0ed2b +//----------------- +'use strict'; +const fs = require('fs'); + +test('fs.writeSync throws for invalid data input', () => { + const invalidInputs = [ + true, false, 0, 1, Infinity, () => {}, {}, [], undefined, null, + ]; + + invalidInputs.forEach((value) => { + expect(() => fs.writeSync(1, value)).toThrow(expect.objectContaining({ + message: expect.stringMatching(/"buffer"/), + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError' + })); + }); +}); + +//<#END_FILE: test-fs-buffertype-writesync.js diff --git a/test/js/node/test/parallel/fs-read-zero-length.test.js b/test/js/node/test/parallel/fs-read-zero-length.test.js new file mode 100644 index 0000000000..258408a067 --- /dev/null +++ b/test/js/node/test/parallel/fs-read-zero-length.test.js @@ -0,0 +1,41 @@ +//#FILE: test-fs-read-zero-length.js +//#SHA1: bda4b0f0c821a8479ffbf0a9099444eed6ee5c4e +//----------------- +'use strict'; +const fs = require('fs'); +const path = require('path'); + +const fixturesPath = path.join(__dirname, '..', 'fixtures'); +const filepath = path.join(fixturesPath, 'x.txt'); + +let fd; + +beforeAll(() => { + fd = fs.openSync(filepath, 'r'); +}); + +afterAll(() => { + fs.closeSync(fd); +}); + +test('fs.read with zero length buffer (async)', (done) => { + const bufferAsync = Buffer.alloc(0); + + fs.read(fd, bufferAsync, 0, 0, 0, (err, bytesRead) => { + expect(err).toBeNull(); + expect(bytesRead).toBe(0); + expect(bufferAsync).toEqual(Buffer.alloc(0)); + done(); + }); +}); + +test('fs.readSync with zero length buffer', () => { + const bufferSync = Buffer.alloc(0); + + const bytesRead = fs.readSync(fd, bufferSync, 0, 0, 0); + + expect(bufferSync).toEqual(Buffer.alloc(0)); + expect(bytesRead).toBe(0); +}); + +//<#END_FILE: test-fs-read-zero-length.js