Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 122c25b

Browse filesBrowse files
authored
fix: fix vi.defineHelper called as object method (#10163)
1 parent f622716 commit 122c25b
Copy full SHA for 122c25b

4 files changed

+92-18Lines changed: 92 additions & 18 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/utils/src/source-map.ts‎

Copy file name to clipboardExpand all lines: packages/utils/src/source-map.ts
+7-6Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,14 @@ export function parseStacktrace(
232232
? parseFFOrSafariStackTrace(stack)
233233
: parseV8Stacktrace(stack)
234234

235-
// remove assertion helper's internal stacks
235+
// remove vi.defineHelper's internal stacks
236236
const helperIndex = stacks.findLastIndex(s =>
237-
s.method === '__VITEST_HELPER__'
238-
// firefox
239-
|| s.method === 'async*__VITEST_HELPER__'
240-
// webkit
241-
|| s.method === 'async __VITEST_HELPER__',
237+
// this covers cases such as
238+
// "__VITEST_HELPER__"
239+
// "__VITEST_HELPER__ [as <object method name>]"
240+
// "async*__VITEST_HELPER__" (firefox)
241+
// "async __VITEST_HELPER__" (webkit)
242+
s.method.includes('__VITEST_HELPER__'),
242243
)
243244
if (helperIndex >= 0) {
244245
stacks = stacks.slice(helperIndex + 1)
Collapse file

‎packages/vitest/src/integrations/vi.ts‎

Copy file name to clipboardExpand all lines: packages/vitest/src/integrations/vi.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ function createVitest(): VitestUtils {
612612
waitFor,
613613
waitUntil,
614614
defineHelper: (fn) => {
615-
return function __VITEST_HELPER__(...args: any[]): any {
616-
const result = fn(...args)
615+
return function __VITEST_HELPER__(this: any, ...args: any[]): any {
616+
const result = fn.apply(this, args)
617617
if (result && typeof result === 'object' && typeof result.then === 'function') {
618618
return (async function __VITEST_HELPER__() {
619619
return await result
Collapse file

‎test/cli/fixtures/assertion-helper/basic.test.ts‎

Copy file name to clipboardExpand all lines: test/cli/fixtures/assertion-helper/basic.test.ts
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,30 @@ const myHelperWithLogs = vi.defineHelper(() => {
104104
test("helper with logs", () => {
105105
myHelperWithLogs();
106106
});
107+
108+
const helperObject = {
109+
prop: 1234,
110+
assertEqualProp: vi.defineHelper(function (this: any, actual: any) {
111+
expect(actual).toEqual(this.prop);
112+
}),
113+
assertEqualPropAsync: vi.defineHelper(async function (this: any, actual: any) {
114+
await new Promise((r) => setTimeout(r, 1));
115+
expect(actual).toEqual(this.prop);
116+
}),
117+
}
118+
119+
test("helper with context pass", () => {
120+
helperObject.assertEqualProp(1234);
121+
});
122+
123+
test("helper with context fail", () => {
124+
helperObject.assertEqualProp(4321);
125+
});
126+
127+
test("async helper with context pass", async () => {
128+
await helperObject.assertEqualPropAsync(1234);
129+
});
130+
131+
test("async helper with context fail", async () => {
132+
await helperObject.assertEqualPropAsync(4321);
133+
});
Collapse file

‎test/cli/test/assertion-helper.test.ts‎

Copy file name to clipboardExpand all lines: test/cli/test/assertion-helper.test.ts
+56-10Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ it('assertion helper', async () => {
1313
❯ basic.test.ts:105:3
1414
1515
16-
⎯⎯⎯⎯⎯⎯ Failed Tests 8 ⎯⎯⎯⎯⎯⎯⎯
16+
⎯⎯⎯⎯⎯⎯ Failed Tests 10 ⎯⎯⎯⎯⎯⎯⎯
1717
1818
FAIL basic.test.ts > sync
1919
AssertionError: expected 'sync' to deeply equal 'x'
@@ -29,7 +29,7 @@ it('assertion helper', async () => {
2929
23| });
3030
24|
3131
32-
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/9]⎯
32+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/11]⎯
3333
3434
FAIL basic.test.ts > async
3535
AssertionError: expected 'async' to deeply equal 'x'
@@ -45,7 +45,7 @@ it('assertion helper', async () => {
4545
27| });
4646
28|
4747
48-
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/9]⎯
48+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/11]⎯
4949
5050
FAIL basic.test.ts > soft
5151
AssertionError: expected 'soft' to deeply equal 'x'
@@ -61,7 +61,7 @@ it('assertion helper', async () => {
6161
31| });
6262
32|
6363
64-
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/9]⎯
64+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/11]⎯
6565
6666
FAIL basic.test.ts > soft async
6767
AssertionError: expected 'soft async' to deeply equal 'x'
@@ -77,7 +77,7 @@ it('assertion helper', async () => {
7777
35| });
7878
36|
7979
80-
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/9]⎯
80+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/11]⎯
8181
8282
FAIL basic.test.ts > nested
8383
AssertionError: expected 'nested' to deeply equal 'x'
@@ -93,7 +93,7 @@ it('assertion helper', async () => {
9393
47| });
9494
48|
9595
96-
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/9]⎯
96+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/11]⎯
9797
9898
FAIL basic.test.ts > multiple soft
9999
AssertionError: expected 'first' to deeply equal 'x'
@@ -109,7 +109,7 @@ it('assertion helper', async () => {
109109
78| myEqualSoft("second", "y");
110110
79| });
111111
112-
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/9]⎯
112+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/11]⎯
113113
114114
FAIL basic.test.ts > multiple soft
115115
AssertionError: expected 'second' to deeply equal 'y'
@@ -125,7 +125,7 @@ it('assertion helper', async () => {
125125
79| });
126126
80|
127127
128-
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[7/9]⎯
128+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[7/11]⎯
129129
130130
FAIL basic.test.ts > custom error
131131
Error: custom error from helper
@@ -137,7 +137,7 @@ it('assertion helper', async () => {
137137
88| });
138138
89|
139139
140-
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[8/9]⎯
140+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[8/11]⎯
141141
142142
FAIL basic.test.ts > non-helper wrapper
143143
AssertionError: expected 'wrapper' to deeply equal 'x'
@@ -154,7 +154,45 @@ it('assertion helper', async () => {
154154
94|
155155
❯ basic.test.ts:96:3
156156
157-
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[9/9]⎯
157+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[9/11]⎯
158+
159+
FAIL basic.test.ts > helper with context fail
160+
AssertionError: expected 4321 to deeply equal 1234
161+
162+
- Expected
163+
+ Received
164+
165+
- 1234
166+
+ 4321
167+
168+
❯ basic.test.ts:124:16
169+
122|
170+
123| test("helper with context fail", () => {
171+
124| helperObject.assertEqualProp(4321);
172+
| ^
173+
125| });
174+
126|
175+
176+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[10/11]⎯
177+
178+
FAIL basic.test.ts > async helper with context fail
179+
AssertionError: expected 4321 to deeply equal 1234
180+
181+
- Expected
182+
+ Received
183+
184+
- 1234
185+
+ 4321
186+
187+
❯ basic.test.ts:132:3
188+
130|
189+
131| test("async helper with context fail", async () => {
190+
132| await helperObject.assertEqualPropAsync(4321);
191+
| ^
192+
133| });
193+
134|
194+
195+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[11/11]⎯
158196
159197
"
160198
`)
@@ -164,9 +202,17 @@ it('assertion helper', async () => {
164202
"async": [
165203
"expected 'async' to deeply equal 'x'",
166204
],
205+
"async helper with context fail": [
206+
"expected 4321 to deeply equal 1234",
207+
],
208+
"async helper with context pass": "passed",
167209
"custom error": [
168210
"custom error from helper",
169211
],
212+
"helper with context fail": [
213+
"expected 4321 to deeply equal 1234",
214+
],
215+
"helper with context pass": "passed",
170216
"helper with logs": "passed",
171217
"multiple soft": [
172218
"expected 'first' to deeply equal 'x'",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.