Compare commits

...

2 Commits

Author SHA1 Message Date
Dylan Conway
d6349c4e0d wip, done only called once 2023-02-01 20:01:56 -08:00
Dylan Conway
45dfc72a5d set done callback state to fulfilled 2023-01-30 19:23:31 -08:00
2 changed files with 65 additions and 43 deletions

View File

@@ -1295,9 +1295,9 @@ pub const TestScope = struct {
callback: js.JSValueRef,
id: TestRunner.Test.ID = 0,
promise: ?*JSInternalPromise = null,
ran: bool = false,
task: ?*TestRunnerTask = null,
skipped: bool = false,
done_called: ?bool = null,
pub const Class = NewClass(
void,
@@ -1437,6 +1437,8 @@ pub const TestScope = struct {
if (JSC.getFunctionData(function)) |data| {
var task = bun.cast(*TestRunnerTask, data);
var test_scope = task.describe.tests.items[task.test_id];
test_scope.done_called = true;
JSC.setFunctionData(function, null);
if (args.len > 0) {
const err = args.ptr[0];
@@ -1449,6 +1451,10 @@ pub const TestScope = struct {
} else {
task.handleResult(.{ .pass = active_test_expectation_counter.actual }, .callback);
}
} else {
var err = globalThis.createErrorInstance("Expected done to be called once, but it was called multiple times.", .{});
globalThis.throwValue(err);
return .zero;
}
return JSValue.jsUndefined();
@@ -1482,6 +1488,7 @@ pub const TestScope = struct {
task,
);
task.done_callback_state = .pending;
this.done_called = false;
initial_value = JSValue.fromRef(callback.?).call(vm.global, &.{callback_func});
} else {
initial_value = js.JSObjectCallAsFunctionReturnValue(vm.global, callback, null, 0, null);
@@ -1533,8 +1540,10 @@ pub const TestScope = struct {
}
}
if (callback_length > 0) {
return .{ .pending = {} };
if (this.done_called) |called| {
if (!called) {
return .{ .pending = {} };
}
}
this.callback = null;
@@ -2089,6 +2098,8 @@ pub const TestRunnerTask = struct {
},
.callback => {
std.debug.assert(this.done_callback_state == .pending);
// done was called. Test can finish remaining expect calls, but
// cannot call done again.
this.done_callback_state = .fulfilled;
if (this.promise_state == .pending and result == .pass) {
@@ -2097,9 +2108,12 @@ pub const TestRunnerTask = struct {
},
.sync => {
std.debug.assert(this.sync_state == .pending);
this.sync_state = .fulfilled;
},
.unhandledRejection => {},
.unhandledRejection => {
this.done_callback_state = .fulfilled;
},
else => @compileError("Bad from"),
}

View File

@@ -318,47 +318,55 @@ describe("child_process cwd", () => {
// // }
// });
it("should work for valid given cwd", (done) => {
const tmpdir = { path: platformTmpDir };
const createDone = createDoneDotAll(done);
// Assume these exist, and 'pwd' gives us the right directory back
testCwd(
{ cwd: tmpdir.path },
{
expectPidType: "number",
expectCode: 0,
expectData: platformTmpDir,
},
createDone(1500),
);
const shouldExistDir = "/dev";
testCwd(
{ cwd: shouldExistDir },
{
expectPidType: "number",
expectCode: 0,
expectData: shouldExistDir,
},
createDone(1500),
);
testCwd(
{ cwd: Bun.pathToFileURL(tmpdir.path) },
{
expectPidType: "number",
expectCode: 0,
expectData: platformTmpDir,
},
createDone(1500),
);
describe("it should work for valid given cwd", () => {
it("temp dir", (done) => {
const tmpdir = { path: platformTmpDir };
testCwd(
{ cwd: tmpdir.path },
{
expectPidType: "number",
expectCode: 0,
expectData: platformTmpDir,
},
done,
);
});
it("/dev dir", (done) => {
const shouldExistDir = "/dev";
testCwd(
{ cwd: shouldExistDir },
{
expectPidType: "number",
expectCode: 0,
expectData: shouldExistDir,
},
done,
);
});
it("file url", (done) => {
const tmpdir = { path: platformTmpDir };
testCwd(
{ cwd: Bun.pathToFileURL(tmpdir.path) },
{
expectPidType: "number",
expectCode: 0,
expectData: platformTmpDir,
},
done,
);
});
});
it.skip("shouldn't try to chdir to an invalid cwd", (done) => {
const createDone = createDoneDotAll(done);
// Spawn() shouldn't try to chdir() to invalid arg, so this should just work
testCwd({ cwd: "" }, { expectPidType: "number" }, createDone(1500));
testCwd({ cwd: undefined }, { expectPidType: "number" }, createDone(1500));
testCwd({ cwd: null }, { expectPidType: "number" }, createDone(1500));
describe("it shouldn't try to chdir to an invalid cwd", () => {
it("empty string", (done) => {
testCwd({ cwd: "" }, { expectPidType: "number" }, done);
});
it("undefined cwd", (done) => {
testCwd({ cwd: undefined }, { expectPidType: "number" }, done);
});
it("null cwd", (done) => {
testCwd({ cwd: null }, { expectPidType: "number" }, done);
});
});
});