Files
bun.sh/test/regression/issue/10380/spy-matchers-diff.test.ts
pfg 3652008b0d Update bun:test diff (#21158)
Fixes #6229 (Fixes BAPI-655): 

|before|<img width="806" height="84" alt="image"
src="https://github.com/user-attachments/assets/6d6c8628-40a8-4950-a7a4-8a85ee07a302"
/>|
|-|-|
|after|<img width="802" height="87" alt="image"
src="https://github.com/user-attachments/assets/c336a626-2b08-469e-aa73-676f43a0f176"
/>|

Fixes #21498 (Fixes BAPI-2240), Fixes #10852 (Fixes BAPI-743):

|before|after|
|-|-|
|<img width="474" height="147" alt="image"
src="https://github.com/user-attachments/assets/bf2225de-a573-4672-a095-f9ff359ec86c"
/>|<img width="283" height="226" alt="image"
src="https://github.com/user-attachments/assets/89cb0e45-b1b7-4dbb-9ddb-b9835baa4b74"
/>|
|<img width="279" height="176" alt="image"
src="https://github.com/user-attachments/assets/e9be7308-dc38-43d2-901c-c77ce4757a51"
/>|<img width="278" height="212" alt="image"
src="https://github.com/user-attachments/assets/8c29b385-a053-4606-9474-3e5c0e60278c"
/>|

Improves multiline string and long output

|before|after|
|-|-|
|<img width="537" height="897" alt="image"
src="https://github.com/user-attachments/assets/034800c5-ab22-4915-90d9-19831906bb2e"
/>|<img width="345" height="1016" alt="image"
src="https://github.com/user-attachments/assets/fa95339e-c136-4c7c-af94-5f11400836dd"
/>|

Improves long single line string output

|before|<img width="1903" height="191" alt="image"
src="https://github.com/user-attachments/assets/bae35c81-0566-4291-810e-e65dc0381aef"
/>|
|-|-|
|after|<img width="1905" height="123" alt="image"
src="https://github.com/user-attachments/assets/bf9f492a-1d52-4cfc-9b1b-c6544a072814"
/>|

Puts 'expected' before 'received' on object diffs. The new version
matches Jest and Vitest, and I find it more intuitive:

|before|after|
|-|-|
|<img width="344" height="221" alt="image"
src="https://github.com/user-attachments/assets/44d42655-c441-411e-9b67-c0db7a5dce08"
/>|<img width="342" height="293" alt="image"
src="https://github.com/user-attachments/assets/565e3934-a2a2-4f99-9d6f-b7df1905f933"
/>|

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-08-06 06:44:46 -07:00

130 lines
3.4 KiB
TypeScript

import { expect, mock, test } from "bun:test";
const stripAnsi = (str: string) => str.replaceAll(/\x1b\[[0-9;]*m/g, "");
test("toHaveBeenCalledWith should show diff when assertion fails", () => {
const mockedFn = mock(args => args);
const a = { a: { b: { c: { d: 1 } } } };
const b = { a: { b: { c: { d: 2 } } } };
mockedFn(a);
let error: Error | undefined;
try {
expect(mockedFn).toHaveBeenCalledWith(b);
} catch (e) {
error = e as Error;
}
expect(error).toBeDefined();
expect(error!.message).toContain("- Expected");
expect(error!.message).toContain("+ Received");
expect(stripAnsi(error!.message)).toContain('"d": 1');
expect(stripAnsi(error!.message)).toContain('"d": 2');
});
test("toHaveBeenNthCalledWith should show diff when assertion fails", () => {
const mockedFn = mock(args => args);
const a = { x: [1, 2, 3] };
const b = { x: [1, 2, 4] };
mockedFn(a);
let error: Error | undefined;
try {
expect(mockedFn).toHaveBeenNthCalledWith(1, b);
} catch (e) {
error = e as Error;
}
expect(error).toBeDefined();
expect(error!.message).toContain("- Expected");
expect(error!.message).toContain("+ Received");
});
test("toHaveBeenLastCalledWith should show diff when assertion fails", () => {
const mockedFn = mock(args => args);
const a = { nested: { value: "hello" } };
const b = { nested: { value: "world" } };
mockedFn("first");
mockedFn(a);
let error: Error | undefined;
try {
expect(mockedFn).toHaveBeenLastCalledWith(b);
} catch (e) {
error = e as Error;
}
expect(error).toBeDefined();
expect(error!.message).toContain("- Expected");
expect(error!.message).toContain("+ Received");
expect(error!.message).toContain("hello");
expect(error!.message).toContain("world");
});
test("toHaveBeenCalledWith should show diff for multiple arguments", () => {
const mockedFn = mock((a, b, c) => [a, b, c]);
mockedFn(1, { foo: "bar" }, [1, 2, 3]);
let error: Error | undefined;
try {
expect(mockedFn).toHaveBeenCalledWith(1, { foo: "baz" }, [1, 2, 4]);
} catch (e) {
error = e as Error;
}
expect(error).toBeDefined();
expect(error!.message).toContain("- Expected");
expect(error!.message).toContain("+ Received");
expect(stripAnsi(error!.message)).toContain("bar");
expect(stripAnsi(error!.message)).toContain("baz");
});
test("toHaveBeenCalledWith should show diff for complex nested structures", () => {
const mockedFn = mock(args => args);
const received = {
users: [
{ id: 1, name: "Alice", roles: ["admin", "user"] },
{ id: 2, name: "Bob", roles: ["user"] },
],
settings: {
theme: "dark",
notifications: { email: true, push: false },
},
};
const expected = {
users: [
{ id: 1, name: "Alice", roles: ["admin", "user"] },
{ id: 2, name: "Bob", roles: ["moderator", "user"] },
],
settings: {
theme: "light",
notifications: { email: true, push: false },
},
};
mockedFn(received);
let error: Error | undefined;
try {
expect(mockedFn).toHaveBeenCalledWith(expected);
} catch (e) {
error = e as Error;
}
expect(error).toBeDefined();
expect(error!.message).toContain("- Expected");
expect(error!.message).toContain("+ Received");
expect(error!.message).toContain("dark");
expect(error!.message).toContain("light");
expect(error!.message).toContain("moderator");
});