Skip to content

Commit

Permalink
test(node): get test-assert.js working (#15698)
Browse files Browse the repository at this point in the history
Co-authored-by: Don Isaac <[email protected]>
Co-authored-by: DonIsaac <[email protected]>
  • Loading branch information
3 people authored Jan 10, 2025
1 parent 7bcd825 commit 0372ca5
Show file tree
Hide file tree
Showing 30 changed files with 4,548 additions and 1,264 deletions.
4 changes: 4 additions & 0 deletions bunfig.node-test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# FIXME: move this back to test/js/node
# https://github.com/oven-sh/bun/issues/16289
[test]
preload = ["./test/js/node/harness.ts", "./test/preload.ts"]
25 changes: 18 additions & 7 deletions scripts/runner.node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const { values: options, positionals: filters } = parseArgs({
type: "boolean",
default: false,
},
/** Path to bun binary */
["exec-path"]: {
type: "string",
default: "bun",
Expand Down Expand Up @@ -252,15 +253,19 @@ async function runTests() {

if (!failedResults.length) {
for (const testPath of tests) {
const title = relative(cwd, join(testsPath, testPath)).replace(/\\/g, "/");
const absoluteTestPath = join(testsPath, testPath);
const title = relative(cwd, absoluteTestPath).replaceAll(sep, "/");
if (isNodeParallelTest(testPath)) {
const subcommand = title.includes("needs-test") ? "test" : "run";
await runTest(title, async () => {
const { ok, error, stdout } = await spawnBun(execPath, {
cwd: cwd,
args: [title],
args: [subcommand, "--config=./bunfig.node-test.toml", absoluteTestPath],
timeout: getNodeParallelTestTimeout(title),
env: {
FORCE_COLOR: "0",
NO_COLOR: "1",
BUN_DEBUG_QUIET_LOGS: "1",
},
stdout: chunk => pipeTestStdout(process.stdout, chunk),
stderr: chunk => pipeTestStdout(process.stderr, chunk),
Expand All @@ -278,9 +283,9 @@ async function runTests() {
stdoutPreview: stdoutPreview,
};
});
continue;
} else {
await runTest(title, async () => spawnBunTest(execPath, join("test", testPath)));
}
await runTest(title, async () => spawnBunTest(execPath, join("test", testPath)));
}
}

Expand Down Expand Up @@ -537,7 +542,7 @@ async function spawnSafe(options) {
}

/**
* @param {string} execPath
* @param {string} execPath Path to bun binary
* @param {SpawnOptions} options
* @returns {Promise<SpawnResult>}
*/
Expand Down Expand Up @@ -565,9 +570,11 @@ async function spawnBun(execPath, { args, cwd, timeout, env, stdout, stderr }) {
// Used in Node.js tests.
TEST_TMPDIR: tmpdirPath,
};

if (env) {
Object.assign(bunEnv, env);
}

if (isWindows) {
delete bunEnv["PATH"];
bunEnv["Path"] = path;
Expand Down Expand Up @@ -862,7 +869,7 @@ function isJavaScriptTest(path) {
* @returns {boolean}
*/
function isNodeParallelTest(testPath) {
return testPath.replaceAll(sep, "/").includes("js/node/test/parallel/")
return testPath.replaceAll(sep, "/").includes("js/node/test/parallel/");
}

/**
Expand Down Expand Up @@ -892,6 +899,9 @@ function isHidden(path) {
return /node_modules|node.js/.test(dirname(path)) || /^\./.test(basename(path));
}

/** Files with these extensions are not treated as test cases */
const IGNORED_EXTENSIONS = new Set([".md"]);

/**
* @param {string} cwd
* @returns {string[]}
Expand All @@ -901,8 +911,9 @@ function getTests(cwd) {
const dirname = join(cwd, path);
for (const entry of readdirSync(dirname, { encoding: "utf-8", withFileTypes: true })) {
const { name } = entry;
const ext = name.slice(name.lastIndexOf("."));
const filename = join(path, name);
if (isHidden(filename)) {
if (isHidden(filename) || IGNORED_EXTENSIONS.has(ext)) {
continue;
}
if (entry.isFile() && isTest(filename)) {
Expand Down
16 changes: 15 additions & 1 deletion src/bun.js/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ pub const Lifetime = enum {
allocated,
temporary,
};

/// Marshall a zig value into a JSValue using comptime reflection.
///
/// - Primitives are converted to their JS equivalent.
/// - Types with `toJS` or `toJSNewlyCreated` methods have them called
/// - Slices are converted to JS arrays
/// - Enums are converted to 32-bit numbers.
pub fn toJS(globalObject: *JSC.JSGlobalObject, comptime ValueType: type, value: ValueType, comptime lifetime: Lifetime) JSC.JSValue {
const Type = comptime brk: {
var CurrentType = ValueType;
Expand Down Expand Up @@ -75,7 +82,7 @@ pub fn toJS(globalObject: *JSC.JSGlobalObject, comptime ValueType: type, value:

var array = JSC.JSValue.createEmptyArray(globalObject, value.len);
for (value, 0..) |*item, i| {
const res = toJS(globalObject, *Child, item, lifetime);
const res = toJS(globalObject, *const Child, item, lifetime);
if (res == .zero) return .zero;
array.putIndex(
globalObject,
Expand All @@ -94,6 +101,13 @@ pub fn toJS(globalObject: *JSC.JSGlobalObject, comptime ValueType: type, value:
return value.toJS(globalObject);
}

// must come after toJS check in case this enum implements its own serializer.
if (@typeInfo(Type) == .Enum) {
// FIXME: creates non-normalized integers (e.g. u2), which
// aren't handled by `jsNumberWithType` rn
return JSC.JSValue.jsNumberWithType(u32, @as(u32, @intFromEnum(value)));
}

@compileError("dont know how to convert " ++ @typeName(ValueType) ++ " to JS");
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/bindings/ErrorCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ WTF::String ERR_INVALID_ARG_TYPE(JSC::ThrowScope& scope, JSC::JSGlobalObject* gl
} else {
for (unsigned i = 0; i < length - 1; i++) {
JSValue expected_type = expected_types.at(i);
if (i > 0) result.append(", "_s);
result.append(expected_type.toWTFString(globalObject));
result.append(", "_s);
}
result.append("or "_s);
result.append(" or "_s);
result.append(expected_types.at(length - 1).toWTFString(globalObject));
}

Expand Down
13 changes: 8 additions & 5 deletions src/bun.js/bindings/ErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ export default [
["ERR_INVALID_ARG_VALUE", TypeError],
["ERR_INVALID_PROTOCOL", TypeError],
["ERR_INVALID_THIS", TypeError],
["ERR_INVALID_RETURN_VALUE", TypeError],
["ERR_IPC_CHANNEL_CLOSED", Error],
["ERR_IPC_DISCONNECTED", Error],
["ERR_MISSING_ARGS", TypeError],
["ERR_AMBIGUOUS_ARGUMENT", TypeError],
["ERR_OUT_OF_RANGE", RangeError],
["ERR_PARSE_ARGS_INVALID_OPTION_VALUE", TypeError],
["ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL", TypeError],
Expand All @@ -47,11 +49,12 @@ export default [
["ERR_UNKNOWN_SIGNAL", TypeError],
["ERR_SOCKET_BAD_PORT", RangeError],
["ERR_STREAM_RELEASE_LOCK", Error, "AbortError"],
["ERR_INCOMPATIBLE_OPTION_PAIR", TypeError, "TypeError"],
["ERR_INVALID_URI", URIError, "URIError"],
["ERR_INVALID_IP_ADDRESS", TypeError, "TypeError"],
["ERR_SCRIPT_EXECUTION_TIMEOUT", Error, "Error"],
["ERR_SCRIPT_EXECUTION_INTERRUPTED", Error, "Error"],
["ERR_INCOMPATIBLE_OPTION_PAIR", TypeError],
["ERR_INVALID_IP_ADDRESS", TypeError],
["ERR_UNAVAILABLE_DURING_EXIT", Error],
["ERR_INVALID_URI", URIError],
["ERR_SCRIPT_EXECUTION_TIMEOUT", Error],
["ERR_SCRIPT_EXECUTION_INTERRUPTED", Error],
["ERR_UNHANDLED_ERROR", Error],
["ERR_UNKNOWN_CREDENTIAL", Error],
["ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET", Error],
Expand Down
2 changes: 2 additions & 0 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2970,6 +2970,7 @@ pub const JSGlobalObject = opaque {
return this.ERR_INVALID_ARG_VALUE("The \"{s}\" argument is invalid. Received {}", .{ argname, value.toFmt(&formatter) }).throw();
}

/// "The <argname> argument must be of type <typename>. Received <value>"
pub fn throwInvalidArgumentTypeValue(
this: *JSGlobalObject,
argname: []const u8,
Expand Down Expand Up @@ -3009,6 +3010,7 @@ pub const JSGlobalObject = opaque {
return JSC.toTypeError(.ERR_MISSING_ARGS, "Not enough arguments to '" ++ name_ ++ "'. Expected {d}, got {d}.", .{ expected, got }, this);
}

/// Not enough arguments passed to function named `name_`
pub fn throwNotEnoughArguments(
this: *JSGlobalObject,
comptime name_: []const u8,
Expand Down
Loading

0 comments on commit 0372ca5

Please sign in to comment.