mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
add tests already passing
This commit is contained in:
35
test/js/node/test/parallel/buffer-tojson.test.js
Normal file
35
test/js/node/test/parallel/buffer-tojson.test.js
Normal file
@@ -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
|
||||
@@ -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
|
||||
21
test/js/node/test/parallel/fs-buffertype-writesync.test.js
Normal file
21
test/js/node/test/parallel/fs-buffertype-writesync.test.js
Normal file
@@ -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
|
||||
41
test/js/node/test/parallel/fs-read-zero-length.test.js
Normal file
41
test/js/node/test/parallel/fs-read-zero-length.test.js
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user