Skip to content
Merged
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
13 changes: 7 additions & 6 deletions packages/utils/src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,14 @@ export function parseStacktrace(
? parseFFOrSafariStackTrace(stack)
: parseV8Stacktrace(stack)

// remove assertion helper's internal stacks
// remove vi.defineHelper's internal stacks
const helperIndex = stacks.findLastIndex(s =>
s.method === '__VITEST_HELPER__'
// firefox
|| s.method === 'async*__VITEST_HELPER__'
// webkit
|| s.method === 'async __VITEST_HELPER__',
// this covers cases such as
// "__VITEST_HELPER__"
// "__VITEST_HELPER__ [as <object method name>]"
// "async*__VITEST_HELPER__" (firefox)
// "async __VITEST_HELPER__" (webkit)
s.method.includes('__VITEST_HELPER__'),
)
if (helperIndex >= 0) {
stacks = stacks.slice(helperIndex + 1)
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/integrations/vi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,8 @@ function createVitest(): VitestUtils {
waitFor,
waitUntil,
defineHelper: (fn) => {
return function __VITEST_HELPER__(...args: any[]): any {
const result = fn(...args)
return function __VITEST_HELPER__(this: any, ...args: any[]): any {
const result = fn.apply(this, args)
if (result && typeof result === 'object' && typeof result.then === 'function') {
return (async function __VITEST_HELPER__() {
return await result
Expand Down
27 changes: 27 additions & 0 deletions test/cli/fixtures/assertion-helper/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,30 @@ const myHelperWithLogs = vi.defineHelper(() => {
test("helper with logs", () => {
myHelperWithLogs();
});

const helperObject = {
prop: 1234,
assertEqualProp: vi.defineHelper(function (this: any, actual: any) {
expect(actual).toEqual(this.prop);
}),
assertEqualPropAsync: vi.defineHelper(async function (this: any, actual: any) {
await new Promise((r) => setTimeout(r, 1));
expect(actual).toEqual(this.prop);
}),
}

test("helper with context pass", () => {
helperObject.assertEqualProp(1234);
});

test("helper with context fail", () => {
helperObject.assertEqualProp(4321);
});

test("async helper with context pass", async () => {
await helperObject.assertEqualPropAsync(1234);
});

test("async helper with context fail", async () => {
await helperObject.assertEqualPropAsync(4321);
});
66 changes: 56 additions & 10 deletions test/cli/test/assertion-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ it('assertion helper', async () => {
❯ basic.test.ts:105:3


⎯⎯⎯⎯⎯⎯ Failed Tests 8 ⎯⎯⎯⎯⎯⎯⎯
⎯⎯⎯⎯⎯⎯ Failed Tests 10 ⎯⎯⎯⎯⎯⎯⎯

FAIL basic.test.ts > sync
AssertionError: expected 'sync' to deeply equal 'x'
Expand All @@ -29,7 +29,7 @@ it('assertion helper', async () => {
23| });
24|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/9]⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/11]⎯

FAIL basic.test.ts > async
AssertionError: expected 'async' to deeply equal 'x'
Expand All @@ -45,7 +45,7 @@ it('assertion helper', async () => {
27| });
28|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/9]⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/11]⎯

FAIL basic.test.ts > soft
AssertionError: expected 'soft' to deeply equal 'x'
Expand All @@ -61,7 +61,7 @@ it('assertion helper', async () => {
31| });
32|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/9]⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/11]⎯

FAIL basic.test.ts > soft async
AssertionError: expected 'soft async' to deeply equal 'x'
Expand All @@ -77,7 +77,7 @@ it('assertion helper', async () => {
35| });
36|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/9]⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/11]⎯

FAIL basic.test.ts > nested
AssertionError: expected 'nested' to deeply equal 'x'
Expand All @@ -93,7 +93,7 @@ it('assertion helper', async () => {
47| });
48|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/9]⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/11]⎯

FAIL basic.test.ts > multiple soft
AssertionError: expected 'first' to deeply equal 'x'
Expand All @@ -109,7 +109,7 @@ it('assertion helper', async () => {
78| myEqualSoft("second", "y");
79| });

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/9]⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/11]⎯

FAIL basic.test.ts > multiple soft
AssertionError: expected 'second' to deeply equal 'y'
Expand All @@ -125,7 +125,7 @@ it('assertion helper', async () => {
79| });
80|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[7/9]⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[7/11]⎯

FAIL basic.test.ts > custom error
Error: custom error from helper
Expand All @@ -137,7 +137,7 @@ it('assertion helper', async () => {
88| });
89|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[8/9]⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[8/11]⎯

FAIL basic.test.ts > non-helper wrapper
AssertionError: expected 'wrapper' to deeply equal 'x'
Expand All @@ -154,7 +154,45 @@ it('assertion helper', async () => {
94|
❯ basic.test.ts:96:3

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[9/9]⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[9/11]⎯

FAIL basic.test.ts > helper with context fail
AssertionError: expected 4321 to deeply equal 1234

- Expected
+ Received

- 1234
+ 4321

❯ basic.test.ts:124:16
122|
123| test("helper with context fail", () => {
124| helperObject.assertEqualProp(4321);
| ^
125| });
126|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[10/11]⎯

FAIL basic.test.ts > async helper with context fail
AssertionError: expected 4321 to deeply equal 1234

- Expected
+ Received

- 1234
+ 4321

❯ basic.test.ts:132:3
130|
131| test("async helper with context fail", async () => {
132| await helperObject.assertEqualPropAsync(4321);
| ^
133| });
134|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[11/11]⎯

"
`)
Expand All @@ -164,9 +202,17 @@ it('assertion helper', async () => {
"async": [
"expected 'async' to deeply equal 'x'",
],
"async helper with context fail": [
"expected 4321 to deeply equal 1234",
],
"async helper with context pass": "passed",
"custom error": [
"custom error from helper",
],
"helper with context fail": [
"expected 4321 to deeply equal 1234",
],
"helper with context pass": "passed",
"helper with logs": "passed",
"multiple soft": [
"expected 'first' to deeply equal 'x'",
Expand Down
Loading