Skip to content

Commit 06f8e28

Browse files
feat: support async functions in toBe (#15714)
Co-authored-by: Christoph Nakazawa <[email protected]>
1 parent fceea7a commit 06f8e28

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
### Features
44

5-
- `[expect]` Have `Inverse` exportable ([#15704](https://github.com/jestjs/jest/pull/15704))
5+
- `[expect]` Have `Inverse` exportable ([#15714](https://github.com/jestjs/jest/pull/15714))
6+
- `[expect]` feat: support `async functions` in `toBe` ([#15704](https://github.com/jestjs/jest/pull/15704))
67
- `[jest-snapshot]` Handle line endings in snapshots ([#15708](https://github.com/jestjs/jest/pull/15708))
78

89
## 30.0.3

packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Rejected to value: <r>4</>
8787
exports[`.resolves fails non-promise value "a" 1`] = `
8888
<d>expect(</><r>received</><d>).</>resolves<d>.</>toBeDefined<d>()</>
8989

90-
<b>Matcher error</>: <r>received</> value must be a promise
90+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
9191

9292
Received has type: string
9393
Received has value: <r>"a"</>
@@ -96,7 +96,7 @@ Received has value: <r>"a"</>
9696
exports[`.resolves fails non-promise value "a" synchronously 1`] = `
9797
<d>expect(</><r>received</><d>).</>resolves<d>.</>toBeDefined<d>()</>
9898

99-
<b>Matcher error</>: <r>received</> value must be a promise
99+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
100100

101101
Received has type: string
102102
Received has value: <r>"a"</>
@@ -105,7 +105,7 @@ Received has value: <r>"a"</>
105105
exports[`.resolves fails non-promise value [1] 1`] = `
106106
<d>expect(</><r>received</><d>).</>resolves<d>.</>toBeDefined<d>()</>
107107

108-
<b>Matcher error</>: <r>received</> value must be a promise
108+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
109109

110110
Received has type: array
111111
Received has value: <r>[1]</>
@@ -114,7 +114,7 @@ Received has value: <r>[1]</>
114114
exports[`.resolves fails non-promise value [1] synchronously 1`] = `
115115
<d>expect(</><r>received</><d>).</>resolves<d>.</>toBeDefined<d>()</>
116116

117-
<b>Matcher error</>: <r>received</> value must be a promise
117+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
118118

119119
Received has type: array
120120
Received has value: <r>[1]</>
@@ -123,7 +123,7 @@ Received has value: <r>[1]</>
123123
exports[`.resolves fails non-promise value [Function anonymous] 1`] = `
124124
<d>expect(</><r>received</><d>).</>resolves<d>.</>toBeDefined<d>()</>
125125

126-
<b>Matcher error</>: <r>received</> value must be a promise
126+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
127127

128128
Received has type: function
129129
Received has value: <r>[Function anonymous]</>
@@ -132,7 +132,7 @@ Received has value: <r>[Function anonymous]</>
132132
exports[`.resolves fails non-promise value [Function anonymous] synchronously 1`] = `
133133
<d>expect(</><r>received</><d>).</>resolves<d>.</>toBeDefined<d>()</>
134134

135-
<b>Matcher error</>: <r>received</> value must be a promise
135+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
136136

137137
Received has type: function
138138
Received has value: <r>[Function anonymous]</>
@@ -141,7 +141,7 @@ Received has value: <r>[Function anonymous]</>
141141
exports[`.resolves fails non-promise value {"a": 1} 1`] = `
142142
<d>expect(</><r>received</><d>).</>resolves<d>.</>toBeDefined<d>()</>
143143

144-
<b>Matcher error</>: <r>received</> value must be a promise
144+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
145145

146146
Received has type: object
147147
Received has value: <r>{"a": 1}</>
@@ -150,7 +150,7 @@ Received has value: <r>{"a": 1}</>
150150
exports[`.resolves fails non-promise value {"a": 1} synchronously 1`] = `
151151
<d>expect(</><r>received</><d>).</>resolves<d>.</>toBeDefined<d>()</>
152152

153-
<b>Matcher error</>: <r>received</> value must be a promise
153+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
154154

155155
Received has type: object
156156
Received has value: <r>{"a": 1}</>
@@ -159,7 +159,7 @@ Received has value: <r>{"a": 1}</>
159159
exports[`.resolves fails non-promise value 4 1`] = `
160160
<d>expect(</><r>received</><d>).</>resolves<d>.</>not<d>.</>toBeDefined<d>()</>
161161

162-
<b>Matcher error</>: <r>received</> value must be a promise
162+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
163163

164164
Received has type: number
165165
Received has value: <r>4</>
@@ -168,7 +168,7 @@ Received has value: <r>4</>
168168
exports[`.resolves fails non-promise value 4 synchronously 1`] = `
169169
<d>expect(</><r>received</><d>).</>resolves<d>.</>not<d>.</>toBeDefined<d>()</>
170170

171-
<b>Matcher error</>: <r>received</> value must be a promise
171+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
172172

173173
Received has type: number
174174
Received has value: <r>4</>
@@ -177,23 +177,23 @@ Received has value: <r>4</>
177177
exports[`.resolves fails non-promise value null 1`] = `
178178
<d>expect(</><r>received</><d>).</>resolves<d>.</>not<d>.</>toBeDefined<d>()</>
179179

180-
<b>Matcher error</>: <r>received</> value must be a promise
180+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
181181

182182
Received has value: <r>null</>
183183
`;
184184

185185
exports[`.resolves fails non-promise value null synchronously 1`] = `
186186
<d>expect(</><r>received</><d>).</>resolves<d>.</>not<d>.</>toBeDefined<d>()</>
187187

188-
<b>Matcher error</>: <r>received</> value must be a promise
188+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
189189

190190
Received has value: <r>null</>
191191
`;
192192

193193
exports[`.resolves fails non-promise value true 1`] = `
194194
<d>expect(</><r>received</><d>).</>resolves<d>.</>not<d>.</>toBeDefined<d>()</>
195195

196-
<b>Matcher error</>: <r>received</> value must be a promise
196+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
197197

198198
Received has type: boolean
199199
Received has value: <r>true</>
@@ -202,7 +202,7 @@ Received has value: <r>true</>
202202
exports[`.resolves fails non-promise value true synchronously 1`] = `
203203
<d>expect(</><r>received</><d>).</>resolves<d>.</>not<d>.</>toBeDefined<d>()</>
204204

205-
<b>Matcher error</>: <r>received</> value must be a promise
205+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
206206

207207
Received has type: boolean
208208
Received has value: <r>true</>
@@ -211,15 +211,15 @@ Received has value: <r>true</>
211211
exports[`.resolves fails non-promise value undefined 1`] = `
212212
<d>expect(</><r>received</><d>).</>resolves<d>.</>not<d>.</>toBeDefined<d>()</>
213213

214-
<b>Matcher error</>: <r>received</> value must be a promise
214+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
215215

216216
Received has value: <r>undefined</>
217217
`;
218218

219219
exports[`.resolves fails non-promise value undefined synchronously 1`] = `
220220
<d>expect(</><r>received</><d>).</>resolves<d>.</>not<d>.</>toBeDefined<d>()</>
221221

222-
<b>Matcher error</>: <r>received</> value must be a promise
222+
<b>Matcher error</>: <r>received</> value must be a promise or a function returning a promise
223223

224224
Received has value: <r>undefined</>
225225
`;

packages/expect/src/__tests__/matchers.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ describe('.resolves', () => {
143143
).resolves.toThrow();
144144
});
145145

146+
it('should resolve async function to toBe', async () => {
147+
async function fn() {
148+
return 'Test';
149+
}
150+
await jestExpect(fn).resolves.toBe('Test');
151+
});
152+
146153
for (const value of ['a', [1], () => {}, {a: 1}]) {
147154
it(`fails non-promise value ${stringify(value)} synchronously`, () => {
148155
let error;

packages/expect/src/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ const makeResolveMatcher =
165165
matcherName: string,
166166
matcher: RawMatcherFn,
167167
isNot: boolean,
168-
actual: Promise<any>,
168+
actual: Promise<any> | (() => Promise<any>),
169169
outerErr: JestAssertionError,
170170
): PromiseMatcherFn =>
171171
(...args) => {
@@ -174,11 +174,16 @@ const makeResolveMatcher =
174174
promise: 'resolves',
175175
};
176176

177-
if (!isPromise(actual)) {
177+
const actualWrapper: Promise<any> =
178+
typeof actual === 'function' ? actual() : actual;
179+
180+
if (!isPromise(actualWrapper)) {
178181
throw new JestAssertionError(
179182
matcherUtils.matcherErrorMessage(
180183
matcherUtils.matcherHint(matcherName, undefined, '', options),
181-
`${matcherUtils.RECEIVED_COLOR('received')} value must be a promise`,
184+
`${matcherUtils.RECEIVED_COLOR(
185+
'received',
186+
)} value must be a promise or a function returning a promise`,
182187
matcherUtils.printWithType(
183188
'Received',
184189
actual,
@@ -190,7 +195,7 @@ const makeResolveMatcher =
190195

191196
const innerErr = new JestAssertionError();
192197

193-
return actual.then(
198+
return actualWrapper.then(
194199
result =>
195200
makeThrowingMatcher(matcher, isNot, 'resolves', result, innerErr).apply(
196201
null,

0 commit comments

Comments
 (0)