Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement describe.skip #2836

Merged
merged 4 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/bun-types/bun-test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ declare module "bun:test" {
*/
export type Describe = {
(label: string, fn: () => void): void;
skip: (label: string, fn: () => void) => void
};
/**
* Describes a group of related tests.
Expand Down
8 changes: 6 additions & 2 deletions src/bun.js/builtins/cpp/BundlerPluginBuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace WebCore {
const JSC::ConstructAbility s_bundlerPluginRunOnResolvePluginsCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;
const JSC::ConstructorKind s_bundlerPluginRunOnResolvePluginsCodeConstructorKind = JSC::ConstructorKind::None;
const JSC::ImplementationVisibility s_bundlerPluginRunOnResolvePluginsCodeImplementationVisibility = JSC::ImplementationVisibility::Public;
const int s_bundlerPluginRunOnResolvePluginsCodeLength = 3593;
const int s_bundlerPluginRunOnResolvePluginsCodeLength = 3619;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and other changes in this file were auto-generated. I suppose that's expected?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this was fixed on main before you rebased last

static const JSC::Intrinsic s_bundlerPluginRunOnResolvePluginsCodeIntrinsic = JSC::NoIntrinsic;
const char* const s_bundlerPluginRunOnResolvePluginsCode =
"(function (\n" \
Expand Down Expand Up @@ -86,7 +86,9 @@ const char* const s_bundlerPluginRunOnResolvePluginsCode =
" path: inputPath,\n" \
" importer,\n" \
" namespace: inputNamespace,\n" \
" //\n" \
" kind,\n" \
" //\n" \
" });\n" \
"\n" \
" while (\n" \
Expand Down Expand Up @@ -353,7 +355,7 @@ const char* const s_bundlerPluginRunSetupFunctionCode =
const JSC::ConstructAbility s_bundlerPluginRunOnLoadPluginsCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;
const JSC::ConstructorKind s_bundlerPluginRunOnLoadPluginsCodeConstructorKind = JSC::ConstructorKind::None;
const JSC::ImplementationVisibility s_bundlerPluginRunOnLoadPluginsCodeImplementationVisibility = JSC::ImplementationVisibility::Public;
const int s_bundlerPluginRunOnLoadPluginsCodeLength = 2740;
const int s_bundlerPluginRunOnLoadPluginsCodeLength = 2766;
static const JSC::Intrinsic s_bundlerPluginRunOnLoadPluginsCodeIntrinsic = JSC::NoIntrinsic;
const char* const s_bundlerPluginRunOnLoadPluginsCode =
"(function (internalID, path, namespace, defaultLoaderId) {\n" \
Expand Down Expand Up @@ -402,6 +404,8 @@ const char* const s_bundlerPluginRunOnLoadPluginsCode =
" var result = callback({\n" \
" path,\n" \
" namespace,\n" \
" //\n" \
" //\n" \
" loader: defaultLoader,\n" \
" });\n" \
"\n" \
Expand Down
32 changes: 29 additions & 3 deletions src/bun.js/test/jest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3306,10 +3306,11 @@ pub const DescribeScope = struct {
current_test_id: TestRunner.Test.ID = 0,
value: JSValue = .zero,
done: bool = false,
skipped: bool = false,
skipped_counter: u32 = 0,

pub fn isAllSkipped(this: *const DescribeScope) bool {
return @as(usize, this.skipped_counter) >= this.tests.items.len;
return this.skipped or @as(usize, this.skipped_counter) >= this.tests.items.len;
}

pub fn push(new: *DescribeScope) void {
Expand Down Expand Up @@ -3388,6 +3389,7 @@ pub const DescribeScope = struct {
.afterEach = .{ .rfn = createCallback(.afterEach), .name = "afterEach" },
.beforeAll = .{ .rfn = createCallback(.beforeAll), .name = "beforeAll" },
.beforeEach = .{ .rfn = createCallback(.beforeEach), .name = "beforeEach" },
.skip = skip,
},
.{
.expect = .{ .get = createExpect, .name = "expect" },
Expand Down Expand Up @@ -3484,13 +3486,36 @@ pub const DescribeScope = struct {
return this.execCallback(ctx, hook);
}

pub fn skip(
this: *DescribeScope,
ctx: js.JSContextRef,
_: js.JSObjectRef,
_: js.JSObjectRef,
arguments: []const js.JSValueRef,
exception: js.ExceptionRef,
) js.JSObjectRef {
return this.runDescribe(ctx, null, null, arguments, exception, true);
}

pub fn describe(
this: *DescribeScope,
ctx: js.JSContextRef,
_: js.JSObjectRef,
_: js.JSObjectRef,
arguments: []const js.JSValueRef,
exception: js.ExceptionRef,
) js.JSObjectRef {
return runDescribe(this, ctx, null, null, arguments, exception, false);
}

fn runDescribe(
this: *DescribeScope,
ctx: js.JSContextRef,
_: js.JSObjectRef,
_: js.JSObjectRef,
arguments: []const js.JSValueRef,
exception: js.ExceptionRef,
skipped: bool,
) js.JSObjectRef {
if (arguments.len == 0 or arguments.len > 2) {
JSError(getAllocator(ctx), "describe() requires 1-2 arguments", .{}, ctx, exception);
Expand Down Expand Up @@ -3518,6 +3543,7 @@ pub const DescribeScope = struct {
.label = (label.toSlice(allocator).cloneIfNeeded(allocator) catch unreachable).slice(),
.parent = active,
.file_id = this.file_id,
.skipped = skipped or active.skipped,
};
var new_this = DescribeScope.Class.make(ctx, scope);

Expand Down Expand Up @@ -3609,7 +3635,7 @@ pub const DescribeScope = struct {
this.pending_tests.unset(test_id);

if (!skipped) {
const afterEach = this.execCallback(globalThis, .afterEach);
const afterEach = this.runCallback(globalThis, .afterEach);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incidentally resolves: #2528

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

if (!afterEach.isEmpty()) {
globalThis.bunVM().runErrorHandler(afterEach, null);
}
Expand Down Expand Up @@ -3753,7 +3779,7 @@ pub const TestRunnerTask = struct {
var test_: TestScope = this.describe.tests.items[test_id];
describe.current_test_id = test_id;
var globalThis = this.globalThis;
if (test_.skipped) {
if (test_.skipped or describe.skipped) {
this.processTestResult(globalThis, .{ .skip = {} }, test_, test_id, describe);
this.deinit();
return false;
Expand Down
148 changes: 147 additions & 1 deletion test/js/bun/test/test-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ it("shouldn't crash when async test runner callback throws", async () => {
await 1;
throw "##123##";
});

afterEach(async () => {
await 1;
console.error("#[Test passed successfully]");
Expand Down Expand Up @@ -2612,3 +2612,149 @@ it("should return non-zero exit code for invalid syntax", async () => {
await rm(test_dir, { force: true, recursive: true });
}
});

describe("skip test inner", () => {
it("should pass", () => {
expect(2 + 2).toBe(4);
});

describe.skip("skip", () => {
it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});

describe("skip non-skipped inner", () => {
it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});
});
});
});

describe.skip("skip test outer", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test that checks we also skip beforeAll, afterAll, beforeEach, and afterEach (assuming Jest does that too)?

it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});

describe("skip non-skipped inner", () => {
it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});
});

describe("skip nested non-skipped inner", () => {
describe("skip", () => {
it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});
});
});
});

describe("skip test inner 2", () => {
it("should pass", () => {
expect(2 + 2).toBe(4);
});

describe.skip("skip", () => {
it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});
});
});

describe.skip("skip beforeEach", () => {
beforeEach(() => {
throw new Error("should not run `beforeEach`");
});

it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});
});

describe("nested beforeEach and afterEach", () => {
let value = 0;

beforeEach(() => {
value += 1;
});

afterEach(() => {
value += 1;
});

describe("runs beforeEach", () => {
it("should update value", () => {
expect(value).toBe(1);
});
});

describe.skip("skips", () => {
it("should throw", async () => {
throw new Error("This should not throw. `.skip` is broken");
});
});

describe.skip("skips async", () => {
it("should throw", async () => {
throw new Error("This should not throw. `.skip` is broken");
});
});

describe("runs beforeEach again", () => {
it("should have value as 3", () => {
expect(value).toBe(3);
});
});
});

describe.skip("skip afterEach", () => {
afterEach(() => {
throw new Error("should not run `afterEach`");
});

it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});
});

describe.skip("skip beforeAll", () => {
beforeAll(() => {
throw new Error("should not run `beforeAll`");
});

it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});
});

describe.skip("skip afterAll", () => {
afterAll(() => {
throw new Error("should not run `afterAll`");
});

it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});
});

// no labels

describe.skip(() => {
it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});
});

describe(() => {
it("should pass", () => {
expect(2 + 2).toBe(4);
});

describe.skip("skip", () => {
it("should throw", () => {
throw new Error("This should not throw. `.skip` is broken");
});
});
});