Skip to content

Commit

Permalink
implement toThrowErrorMatchingSnapshot, toThrowErrorMatchingInlineSna…
Browse files Browse the repository at this point in the history
…pshot (#15607)
  • Loading branch information
pfgithub authored Dec 6, 2024
1 parent eacf89e commit 1476e4c
Show file tree
Hide file tree
Showing 11 changed files with 399 additions and 150 deletions.
4 changes: 2 additions & 2 deletions docs/test/writing.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,12 @@ Bun implements the following matchers. Full Jest compatibility is on the roadmap

---

-
-
- [`.toThrowErrorMatchingSnapshot()`](https://jestjs.io/docs/expect#tothrowerrormatchingsnapshothint)

---

-
-
- [`.toThrowErrorMatchingInlineSnapshot()`](https://jestjs.io/docs/expect#tothrowerrormatchinginlinesnapshotinlinesnapshot)

{% /table %}
34 changes: 31 additions & 3 deletions packages/bun-types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1299,25 +1299,53 @@ declare module "bun:test" {
* Asserts that a value matches the most recent inline snapshot.
*
* @example
* expect("Hello").toMatchInlineSnapshot();
* expect("Hello").toMatchInlineSnapshot(`"Hello"`);
* @param value The latest snapshot value.
*
* @param value The latest automatically-updated snapshot value.
*/
toMatchInlineSnapshot(value?: string): void;
/**
* Asserts that a value matches the most recent inline snapshot.
*
* @example
* expect("Hello").toMatchInlineSnapshot(`"Hello"`);
* expect({ c: new Date() }).toMatchInlineSnapshot({ c: expect.any(Date) });
* expect({ c: new Date() }).toMatchInlineSnapshot({ c: expect.any(Date) }, `
* {
* "v": Any<Date>,
* }
* `);
*
* @param propertyMatchers Object containing properties to match against the value.
* @param hint Hint used to identify the snapshot in the snapshot file.
* @param value The latest automatically-updated snapshot value.
*/
toMatchInlineSnapshot(propertyMatchers?: object, value?: string): void;
/**
* Asserts that a function throws an error matching the most recent snapshot.
*
* @example
* function fail() {
* throw new Error("Oops!");
* }
* expect(fail).toThrowErrorMatchingSnapshot();
* expect(fail).toThrowErrorMatchingSnapshot("This one should say Oops!");
*
* @param value The latest automatically-updated snapshot value.
*/
toThrowErrorMatchingSnapshot(hint?: string): void;
/**
* Asserts that a function throws an error matching the most recent snapshot.
*
* @example
* function fail() {
* throw new Error("Oops!");
* }
* expect(fail).toThrowErrorMatchingInlineSnapshot();
* expect(fail).toThrowErrorMatchingInlineSnapshot(`"Oops!"`);
*
* @param value The latest automatically-updated snapshot value.
*/
toThrowErrorMatchingInlineSnapshot(value?: string): void;
/**
* Asserts that an object matches a subset of properties.
*
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/bindings/InternalModuleRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ JSC::JSValue generateModule(JSC::JSGlobalObject* globalObject, JSC::VM& vm, cons
return result;
}

#if BUN_DYNAMIC_JS_LOAD_PATH
#ifdef BUN_DYNAMIC_JS_LOAD_PATH
JSValue initializeInternalModuleFromDisk(
JSGlobalObject* globalObject,
VM& vm,
Expand Down
8 changes: 3 additions & 5 deletions src/bun.js/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6032,11 +6032,10 @@ CPP_DECL bool Bun__CallFrame__isFromBunMain(JSC::CallFrame* callFrame, JSC::VM*
return source.string() == "builtin://bun/main"_s;
}

CPP_DECL void Bun__CallFrame__getCallerSrcLoc(JSC::CallFrame* callFrame, JSC::JSGlobalObject* globalObject, unsigned int* outSourceID, unsigned int* outLine, unsigned int* outColumn)
CPP_DECL void Bun__CallFrame__getCallerSrcLoc(JSC::CallFrame* callFrame, JSC::JSGlobalObject* globalObject, BunString* outSourceURL, unsigned int* outLine, unsigned int* outColumn)
{
JSC::VM& vm = globalObject->vm();
JSC::LineColumn lineColumn;
JSC::SourceID sourceID = 0;
String sourceURL;

ZigStackFrame remappedFrame = {};
Expand All @@ -6046,6 +6045,7 @@ CPP_DECL void Bun__CallFrame__getCallerSrcLoc(JSC::CallFrame* callFrame, JSC::JS
return WTF::IterationStatus::Continue;

if (visitor->hasLineAndColumnInfo()) {

lineColumn = visitor->computeLineAndColumn();

String sourceURLForFrame = visitor->sourceURL();
Expand All @@ -6071,8 +6071,6 @@ CPP_DECL void Bun__CallFrame__getCallerSrcLoc(JSC::CallFrame* callFrame, JSC::JS
sourceURLForFrame = origin.string();
}
}

sourceID = provider->asID();
}
}

Expand All @@ -6099,7 +6097,7 @@ CPP_DECL void Bun__CallFrame__getCallerSrcLoc(JSC::CallFrame* callFrame, JSC::JS
lineColumn.column = OrdinalNumber::fromZeroBasedInt(remappedFrame.position.column_zero_based).oneBasedInt();
}

*outSourceID = sourceID;
*outSourceURL = Bun::toStringRef(sourceURL);
*outLine = lineColumn.line;
*outColumn = lineColumn.column;
}
10 changes: 5 additions & 5 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6666,19 +6666,19 @@ pub const CallFrame = opaque {
return value;
}

extern fn Bun__CallFrame__getCallerSrcLoc(*const CallFrame, *JSGlobalObject, *c_uint, *c_uint, *c_uint) void;
extern fn Bun__CallFrame__getCallerSrcLoc(*const CallFrame, *JSGlobalObject, *bun.String, *c_uint, *c_uint) void;
pub const CallerSrcLoc = struct {
source_file_id: c_uint,
str: bun.String,
line: c_uint,
column: c_uint,
};
pub fn getCallerSrcLoc(call_frame: *const CallFrame, globalThis: *JSGlobalObject) CallerSrcLoc {
var source_id: c_uint = undefined;
var str: bun.String = undefined;
var line: c_uint = undefined;
var column: c_uint = undefined;
Bun__CallFrame__getCallerSrcLoc(call_frame, globalThis, &source_id, &line, &column);
Bun__CallFrame__getCallerSrcLoc(call_frame, globalThis, &str, &line, &column);
return .{
.source_file_id = source_id,
.str = str,
.line = line,
.column = column,
};
Expand Down
Loading

0 comments on commit 1476e4c

Please sign in to comment.