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

bun:test: implement test.failing and the retry option #14829

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
63 changes: 27 additions & 36 deletions packages/bun-types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,21 @@ declare module "bun:test" {
* @param milliseconds the number of milliseconds for the default timeout
*/
export function setDefaultTimeout(milliseconds: number): void;

// TODO: the usages below can be replaced with `Test` once https://github.com/oven-sh/bun/issues/10885 is resolved.
type TestFunc = (
label: string,
fn: (() => void | Promise<unknown>) | ((done: (err?: unknown) => void) => void),
/**
* - If a `number`, sets the timeout for the test in milliseconds.
* - If an `object`, sets the options for the test.
* - `timeout` sets the timeout for the test in milliseconds.
* - `retry` sets the number of times to retry the test if it fails.
* - `repeats` sets the number of times to repeat the test, regardless of whether it passed or failed.
*/
options?: number | TestOptions,
) => void;

export interface TestOptions {
/**
* Sets the timeout for the test in milliseconds.
Expand All @@ -326,6 +341,7 @@ declare module "bun:test" {
*/
repeats?: number;
}

/**
* Runs a test.
*
Expand Down Expand Up @@ -367,23 +383,15 @@ declare module "bun:test" {
* @param fn the test function
* @param options the test timeout or options
*/
only(
label: string,
fn: (() => void | Promise<unknown>) | ((done: (err?: unknown) => void) => void),
options?: number | TestOptions,
): void;
only: TestFunc;
/**
* Skips this test.
*
* @param label the label for the test
* @param fn the test function
* @param options the test timeout or options
*/
skip(
label: string,
fn: (() => void | Promise<unknown>) | ((done: (err?: unknown) => void) => void),
options?: number | TestOptions,
): void;
skip: TestFunc;
/**
* Marks this test as to be written or to be fixed.
*
Expand All @@ -396,49 +404,27 @@ declare module "bun:test" {
* @param fn the test function
* @param options the test timeout or options
*/
todo(
label: string,
fn?: (() => void | Promise<unknown>) | ((done: (err?: unknown) => void) => void),
options?: number | TestOptions,
): void;
todo: TestFunc;
/**
* Runs this test, if `condition` is true.
*
* This is the opposite of `test.skipIf()`.
*
* @param condition if the test should run
*/
if(
condition: boolean,
): (
label: string,
fn: (() => void | Promise<unknown>) | ((done: (err?: unknown) => void) => void),
options?: number | TestOptions,
) => void;
if(condition: boolean): TestFunc;
/**
* Skips this test, if `condition` is true.
*
* @param condition if the test should be skipped
*/
skipIf(
condition: boolean,
): (
label: string,
fn: (() => void | Promise<unknown>) | ((done: (err?: unknown) => void) => void),
options?: number | TestOptions,
) => void;
skipIf(condition: boolean): TestFunc;
/**
* Marks this test as to be written or to be fixed, if `condition` is true.
*
* @param condition if the test should be marked TODO
*/
todoIf(
condition: boolean,
): (
label: string,
fn: (() => void | Promise<unknown>) | ((done: (err?: unknown) => void) => void),
options?: number | TestOptions,
) => void;
todoIf(condition: boolean): TestFunc;
/**
* Returns a function that runs for each item in `table`.
*
Expand All @@ -453,6 +439,11 @@ declare module "bun:test" {
each<T>(
table: T[],
): (label: string, fn: (...args: T[]) => void | Promise<unknown>, options?: number | TestOptions) => void;
/**
* Use `test.failing` when you are writing a test and expecting it to fail.
* If `failing` test will throw any errors then it will pass. If it does not throw it will fail.
*/
failing: TestFunc;
}
/**
* Runs a test.
Expand Down
7 changes: 2 additions & 5 deletions src/bun.js/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ threadlocal var stdout_lock_count: u16 = 0;

/// https://console.spec.whatwg.org/#formatter
pub fn messageWithTypeAndLevel(
//console_: ConsoleObject.Type,
_: ConsoleObject.Type,
console: *ConsoleObject,
message_type: MessageType,
//message_level: u32,
level: MessageLevel,
Expand All @@ -92,8 +91,6 @@ pub fn messageWithTypeAndLevel(
return;
}

var console = global.bunVM().console;

// Lock/unlock a mutex incase two JS threads are console.log'ing at the same time
// We do this the slightly annoying way to avoid assigning a pointer
if (level == .Warning or level == .Error or message_type == .Assert) {
Expand Down Expand Up @@ -3439,7 +3436,7 @@ pub fn takeHeapSnapshot(
) callconv(JSC.conv) void {
// TODO: this does an extra JSONStringify and we don't need it to!
var snapshot: [1]JSValue = .{globalThis.generateHeapSnapshot()};
ConsoleObject.messageWithTypeAndLevel(undefined, MessageType.Log, MessageLevel.Debug, globalThis, &snapshot, 1);
globalThis.bunVM().console.messageWithTypeAndLevel(.Log, .Debug, globalThis, &snapshot, 1);
}
pub fn timeStamp(
// console
Expand Down
3 changes: 1 addition & 2 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4597,8 +4597,7 @@ pub const JSValue = enum(JSValueReprInt) {
message_type: ConsoleObject.MessageType,
message_level: ConsoleObject.MessageLevel,
) void {
JSC.ConsoleObject.messageWithTypeAndLevel(
undefined,
globalObject.bunVM().console.messageWithTypeAndLevel(
message_type,
message_level,
globalObject,
Expand Down
Loading