From 862d35d832acadebd05f80fc0c248ae71b7f6cb0 Mon Sep 17 00:00:00 2001 From: argosphil Date: Sun, 11 Feb 2024 16:39:27 +0000 Subject: [PATCH] fix: `bun test -t` (#8845) --- src/bun.js/bindings/RegularExpression.zig | 2 +- src/bun.js/test/jest.zig | 16 +++++++- test/cli/test/bun-test.test.ts | 46 +++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/bun.js/bindings/RegularExpression.zig b/src/bun.js/bindings/RegularExpression.zig index 9c3da4fa27..d878135d47 100644 --- a/src/bun.js/bindings/RegularExpression.zig +++ b/src/bun.js/bindings/RegularExpression.zig @@ -40,7 +40,7 @@ pub const RegularExpression = opaque { // Simple boolean matcher pub inline fn matches(this: *RegularExpression, str: bun.String) bool { - return Yarr__RegularExpression__matches(this, str) > 0; + return Yarr__RegularExpression__matches(this, str) >= 0; } pub inline fn searchRev(this: *RegularExpression, str: bun.String) i32 { diff --git a/src/bun.js/test/jest.zig b/src/bun.js/test/jest.zig index 023d0ef247..747e50e22f 100644 --- a/src/bun.js/test/jest.zig +++ b/src/bun.js/test/jest.zig @@ -1857,7 +1857,21 @@ fn eachBind( (description.toSlice(globalThis, allocator).cloneIfNeeded(allocator) catch unreachable).slice(); const formattedLabel = formatLabel(globalThis, label, function_args, test_idx) catch return .zero; - if (each_data.is_test) { + var is_skip = false; + + if (Jest.runner.?.filter_regex) |regex| { + var buffer: bun.MutableString = Jest.runner.?.filter_buffer; + buffer.reset(); + appendParentLabel(&buffer, parent) catch @panic("Bun ran out of memory while filtering tests"); + buffer.append(formattedLabel) catch unreachable; + const str = bun.String.fromBytes(buffer.toOwnedSliceLeaky()); + is_skip = !regex.matches(str); + } + + if (is_skip) { + parent.skip_count += 1; + function.unprotect(); + } else if (each_data.is_test) { function.protect(); parent.tests.append(allocator, TestScope{ .label = formattedLabel, diff --git a/test/cli/test/bun-test.test.ts b/test/cli/test/bun-test.test.ts index 0c0e6fc61d..036bc74632 100644 --- a/test/cli/test/bun-test.test.ts +++ b/test/cli/test/bun-test.test.ts @@ -603,6 +603,52 @@ describe("bun test", () => { expect(stderr).toContain(`${numbers[0]} + ${numbers[1]} = ${numbers[2]}`); }); }); + test("should allow tests run with test.each to be skipped", () => { + const numbers = [ + [1, 2, 3], + [1, 1, 2], + [3, 4, 7], + ]; + + const stderr = runTest({ + args: ["-t", "$a"], + input: ` + import { test, expect } from "bun:test"; + + test.each(${JSON.stringify(numbers)})("%i + %i = %i", (a, b, e) => { + expect(a + b).toBe(e); + }); + `, + }); + numbers.forEach(numbers => { + expect(stderr).not.toContain(`${numbers[0]} + ${numbers[1]} = ${numbers[2]}`); + }); + }); + test("should allow tests run with test.each to be matched", () => { + const numbers = [ + [1, 2, 3], + [1, 1, 2], + [3, 4, 7], + ]; + + const stderr = runTest({ + args: ["-t", "1 \\+"], + input: ` + import { test, expect } from "bun:test"; + + test.each(${JSON.stringify(numbers)})("%i + %i = %i", (a, b, e) => { + expect(a + b).toBe(e); + }); + `, + }); + numbers.forEach(numbers => { + if (numbers[0] === 1) { + expect(stderr).toContain(`${numbers[0]} + ${numbers[1]} = ${numbers[2]}`); + } else { + expect(stderr).not.toContain(`${numbers[0]} + ${numbers[1]} = ${numbers[2]}`); + } + }); + }); test("should run tests with describe.each", () => { const numbers = [ [1, 2, 3],