Skip to content

Commit 6357b34

Browse files
committed
refactor: replace toThrowError with toThrow
1 parent 7db49bc commit 6357b34

42 files changed

Lines changed: 301 additions & 301 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/api/describe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function numberToCurrency(value: number | string) {
6262
describe('numberToCurrency', () => {
6363
describe('given an invalid number', () => {
6464
test('composed of non-numbers to throw error', () => {
65-
expect(() => numberToCurrency('abc')).toThrowError()
65+
expect(() => numberToCurrency('abc')).toThrow()
6666
})
6767
})
6868

docs/api/expect.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ expect(new Error('hi', { cause: 'x' })).toEqual(new Error('hi'))
560560
expect(new Error('hi')).toEqual(new Error('hi', { cause: 'x' }))
561561
```
562562

563-
To test if something was thrown, use [`toThrowError`](#tothrowerror) assertion.
563+
To test if something was thrown, use [`toThrow`](#tothrow) assertion.
564564
:::
565565

566566
## toStrictEqual
@@ -777,13 +777,13 @@ test('the number of elements must match exactly', () => {
777777
})
778778
```
779779

780-
## toThrowError
780+
## toThrow
781781

782782
- **Type:** `(expected?: any) => Awaitable<void>`
783783

784-
- **Alias:** `toThrow`
784+
- **Alias:** `toThrowError` <Deprecated />
785785

786-
`toThrowError` asserts if a function throws an error when it is called.
786+
`toThrow` asserts if a function throws an error when it is called.
787787

788788
You can provide an optional argument to test that a specific error is thrown:
789789

@@ -798,7 +798,7 @@ This does not apply for async calls as [rejects](#rejects) correctly unwraps the
798798
```ts
799799
test('expect rejects toThrow', async ({ expect }) => {
800800
const promise = Promise.reject(new Error('Test'))
801-
await expect(promise).rejects.toThrowError()
801+
await expect(promise).rejects.toThrow()
802802
})
803803
```
804804
:::
@@ -818,18 +818,18 @@ function getFruitStock(type: string) {
818818

819819
test('throws on pineapples', () => {
820820
// Test that the error message says "stock" somewhere: these are equivalent
821-
expect(() => getFruitStock('pineapples')).toThrowError(/stock/)
822-
expect(() => getFruitStock('pineapples')).toThrowError('stock')
821+
expect(() => getFruitStock('pineapples')).toThrow(/stock/)
822+
expect(() => getFruitStock('pineapples')).toThrow('stock')
823823

824824
// Test the exact error message
825-
expect(() => getFruitStock('pineapples')).toThrowError(
825+
expect(() => getFruitStock('pineapples')).toThrow(
826826
/^Pineapples are not in stock$/,
827827
)
828828

829-
expect(() => getFruitStock('pineapples')).toThrowError(
829+
expect(() => getFruitStock('pineapples')).toThrow(
830830
new Error('Pineapples are not in stock'),
831831
)
832-
expect(() => getFruitStock('pineapples')).toThrowError(expect.objectContaining({
832+
expect(() => getFruitStock('pineapples')).toThrow(expect.objectContaining({
833833
message: 'Pineapples are not in stock',
834834
}))
835835
})
@@ -844,7 +844,7 @@ function getAsyncFruitStock() {
844844
}
845845

846846
test('throws on pineapples', async () => {
847-
await expect(() => getAsyncFruitStock()).rejects.toThrowError('empty')
847+
await expect(() => getAsyncFruitStock()).rejects.toThrow('empty')
848848
})
849849
```
850850
:::
@@ -854,8 +854,8 @@ You can also test non-Error values that are thrown:
854854

855855
```ts
856856
test('throws non-Error values', () => {
857-
expect(() => { throw 42 }).toThrowError(42)
858-
expect(() => { throw { message: 'error' } }).toThrowError({ message: 'error' })
857+
expect(() => { throw 42 }).toThrow(42)
858+
expect(() => { throw { message: 'error' } }).toThrow({ message: 'error' })
859859
})
860860
```
861861
:::
@@ -956,13 +956,13 @@ Note that since file system operation is async, you need to use `await` with `to
956956

957957
- **Type:** `(hint?: string) => void`
958958

959-
The same as [`toMatchSnapshot`](#tomatchsnapshot), but expects the same value as [`toThrowError`](#tothrowerror).
959+
The same as [`toMatchSnapshot`](#tomatchsnapshot), but expects the same value as [`toThrow`](#tothrow).
960960

961961
## toThrowErrorMatchingInlineSnapshot
962962

963963
- **Type:** `(snapshot?: string, hint?: string) => void`
964964

965-
The same as [`toMatchInlineSnapshot`](#tomatchinlinesnapshot), but expects the same value as [`toThrowError`](#tothrowerror).
965+
The same as [`toMatchInlineSnapshot`](#tomatchinlinesnapshot), but expects the same value as [`toThrow`](#tothrow).
966966

967967
## toHaveBeenCalled
968968

docs/blog/vitest-4.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ expect(animal.bark()).toBeUndefined()
260260

261261
Vitest 4 introduces a new asymmetric matcher called `expect.schemaMatching`. It accepts a [Standard Schema v1](https://standardschema.dev/) object and validates values against it, passing the assertion when the value conforms to the schema.
262262

263-
As a reminder, asymmetric matchers can be used in all `expect` matchers that check equality, including `toEqual`, `toStrictEqual`, `toMatchObject`, `toContainEqual`, `toThrowError`, `toHaveBeenCalledWith`, `toHaveReturnedWith` and `toHaveBeenResolvedWith`.
263+
As a reminder, asymmetric matchers can be used in all `expect` matchers that check equality, including `toEqual`, `toStrictEqual`, `toMatchObject`, `toContainEqual`, `toThrow`, `toHaveBeenCalledWith`, `toHaveReturnedWith` and `toHaveBeenResolvedWith`.
264264

265265
```ts
266266
import { expect, test } from 'vitest'

test/browser/fixtures/expect-dom/toBeChecked.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('.toBeChecked', () => {
6969

7070
expect(() =>
7171
expect(queryByTestId('input-checked')).not.toBeChecked(),
72-
).toThrowError()
72+
).toThrow()
7373
})
7474

7575
test('throws when input checkbox is not checked but expected to be', () => {
@@ -79,7 +79,7 @@ describe('.toBeChecked', () => {
7979

8080
expect(() =>
8181
expect(queryByTestId('input-empty')).toBeChecked(),
82-
).toThrowError()
82+
).toThrow()
8383
})
8484

8585
test('throws when element with role="checkbox" is checked but expected not to be', () => {
@@ -89,7 +89,7 @@ describe('.toBeChecked', () => {
8989

9090
expect(() =>
9191
expect(queryByTestId('aria-checkbox-checked')).not.toBeChecked(),
92-
).toThrowError()
92+
).toThrow()
9393
})
9494

9595
test('throws when element with role="checkbox" is not checked but expected to be', () => {
@@ -99,7 +99,7 @@ describe('.toBeChecked', () => {
9999

100100
expect(() =>
101101
expect(queryByTestId('aria-checkbox-unchecked')).toBeChecked(),
102-
).toThrowError()
102+
).toThrow()
103103
})
104104

105105
test('throws when radio input is checked but expected not to be', () => {
@@ -109,7 +109,7 @@ describe('.toBeChecked', () => {
109109

110110
expect(() =>
111111
expect(queryByTestId('input-radio-checked')).not.toBeChecked(),
112-
).toThrowError()
112+
).toThrow()
113113
})
114114

115115
test('throws when input radio is not checked but expected to be', () => {
@@ -119,7 +119,7 @@ describe('.toBeChecked', () => {
119119

120120
expect(() =>
121121
expect(queryByTestId('input-radio-unchecked')).toBeChecked(),
122-
).toThrowError()
122+
).toThrow()
123123
})
124124

125125
test('throws when element with role="radio" is checked but expected not to be', () => {
@@ -129,7 +129,7 @@ describe('.toBeChecked', () => {
129129

130130
expect(() =>
131131
expect(queryByTestId('aria-radio-checked')).not.toBeChecked(),
132-
).toThrowError()
132+
).toThrow()
133133
})
134134

135135
test('throws when element with role="radio" is not checked but expected to be', () => {
@@ -139,7 +139,7 @@ describe('.toBeChecked', () => {
139139

140140
expect(() =>
141141
expect(queryByTestId('aria-checkbox-unchecked')).toBeChecked(),
142-
).toThrowError()
142+
).toThrow()
143143
})
144144

145145
test('throws when element with role="switch" is checked but expected not to be', () => {
@@ -149,7 +149,7 @@ describe('.toBeChecked', () => {
149149

150150
expect(() =>
151151
expect(queryByTestId('aria-switch-checked')).not.toBeChecked(),
152-
).toThrowError()
152+
).toThrow()
153153
})
154154

155155
test('throws when element with role="switch" is not checked but expected to be', () => {
@@ -159,7 +159,7 @@ describe('.toBeChecked', () => {
159159

160160
expect(() =>
161161
expect(queryByTestId('aria-switch-unchecked')).toBeChecked(),
162-
).toThrowError()
162+
).toThrow()
163163
})
164164

165165
test('throws when element with role="checkbox" has an invalid aria-checked attribute', () => {
@@ -169,7 +169,7 @@ describe('.toBeChecked', () => {
169169

170170
expect(() =>
171171
expect(queryByTestId('aria-checkbox-invalid')).toBeChecked(),
172-
).toThrowError(
172+
).toThrow(
173173
/only inputs with .* a valid aria-checked attribute can be used/,
174174
)
175175
})
@@ -181,7 +181,7 @@ describe('.toBeChecked', () => {
181181

182182
expect(() =>
183183
expect(queryByTestId('aria-radio-invalid')).toBeChecked(),
184-
).toThrowError(
184+
).toThrow(
185185
/only inputs with .* a valid aria-checked attribute can be used/,
186186
)
187187
})
@@ -193,14 +193,14 @@ describe('.toBeChecked', () => {
193193

194194
expect(() =>
195195
expect(queryByTestId('aria-switch-invalid')).toBeChecked(),
196-
).toThrowError(
196+
).toThrow(
197197
/only inputs with .* a valid aria-checked attribute can be used/,
198198
)
199199
})
200200

201201
test('throws when the element is not an input', () => {
202202
const {queryByTestId} = render(`<select data-testid="select"></select>`)
203-
expect(() => expect(queryByTestId('select')).toBeChecked()).toThrowError(
203+
expect(() => expect(queryByTestId('select')).toBeChecked()).toThrow(
204204
/only inputs with type="checkbox" or type="radio" or elements with.* role="checkbox".* role="menuitemcheckbox".* role="option".* role="radio".* role="switch".* role="menuitemradio".* role="treeitem" .* can be used/,
205205
)
206206
})

test/browser/fixtures/expect-dom/toBeDisabled.test.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test('.toBeDisabled', () => {
4343
expect(queryByTestId('button-element')).toBeDisabled()
4444
expect(() =>
4545
expect(queryByTestId('button-element')).not.toBeDisabled(),
46-
).toThrowError()
46+
).toThrow()
4747
expect(queryByTestId('textarea-element')).toBeDisabled()
4848
expect(queryByTestId('input-element')).toBeDisabled()
4949

@@ -65,10 +65,10 @@ test('.toBeDisabled', () => {
6565

6666
expect(queryByTestId('a-element')).not.toBeDisabled()
6767
expect(queryByTestId('deep-a-element')).not.toBeDisabled()
68-
expect(() => expect(queryByTestId('a-element')).toBeDisabled()).toThrowError()
68+
expect(() => expect(queryByTestId('a-element')).toBeDisabled()).toThrow()
6969
expect(() =>
7070
expect(queryByTestId('deep-a-element')).toBeDisabled(),
71-
).toThrowError()
71+
).toThrow()
7272
})
7373

7474
test('.toBeDisabled fieldset>legend', () => {
@@ -131,12 +131,12 @@ test('.toBeDisabled custom element', () => {
131131
expect(queryByTestId('disabled-custom-element')).toBeDisabled()
132132
expect(() => {
133133
expect(queryByTestId('disabled-custom-element')).not.toBeDisabled()
134-
}).toThrowError('element is disabled')
134+
}).toThrow('element is disabled')
135135

136136
expect(queryByTestId('enabled-custom-element')).not.toBeDisabled()
137137
expect(() => {
138138
expect(queryByTestId('enabled-custom-element')).toBeDisabled()
139-
}).toThrowError('element is not disabled')
139+
}).toThrow('element is not disabled')
140140
})
141141

142142
test('.toBeEnabled', () => {
@@ -173,47 +173,47 @@ test('.toBeEnabled', () => {
173173

174174
expect(() => {
175175
expect(queryByTestId('button-element')).toBeEnabled()
176-
}).toThrowError()
176+
}).toThrow()
177177
expect(queryByTestId('button-element')).not.toBeEnabled()
178178
expect(() => {
179179
expect(queryByTestId('textarea-element')).toBeEnabled()
180-
}).toThrowError()
180+
}).toThrow()
181181
expect(() => {
182182
expect(queryByTestId('input-element')).toBeEnabled()
183-
}).toThrowError()
183+
}).toThrow()
184184

185185
expect(() => {
186186
// fieldset elements can't be considered disabled, only their children
187187
expect(queryByTestId('fieldset-element')).toBeDisabled()
188-
}).toThrowError()
188+
}).toThrow()
189189
expect(() => {
190190
expect(queryByTestId('fieldset-child-element')).toBeEnabled()
191-
}).toThrowError()
191+
}).toThrow()
192192

193193
expect(queryByTestId('div-element')).toBeEnabled()
194194
expect(queryByTestId('div-child-element')).toBeEnabled()
195195

196196
expect(() => {
197197
expect(queryByTestId('nested-form-element')).toBeEnabled()
198-
}).toThrowError()
198+
}).toThrow()
199199
expect(() => {
200200
expect(queryByTestId('deep-select-element')).toBeEnabled()
201-
}).toThrowError()
201+
}).toThrow()
202202
expect(() => {
203203
expect(queryByTestId('deep-optgroup-element')).toBeEnabled()
204-
}).toThrowError()
204+
}).toThrow()
205205
expect(() => {
206206
expect(queryByTestId('deep-option-element')).toBeEnabled()
207-
}).toThrowError()
207+
}).toThrow()
208208

209209
expect(queryByTestId('a-element')).toBeEnabled()
210210
expect(() =>
211211
expect(queryByTestId('a-element')).not.toBeEnabled(),
212-
).toThrowError()
212+
).toThrow()
213213
expect(queryByTestId('deep-a-element')).toBeEnabled()
214214
expect(() =>
215215
expect(queryByTestId('deep-a-element')).not.toBeEnabled(),
216-
).toThrowError()
216+
).toThrow()
217217
})
218218

219219
test('.toBeEnabled fieldset>legend', () => {
@@ -259,18 +259,18 @@ test('.toBeEnabled fieldset>legend', () => {
259259

260260
expect(() => {
261261
expect(queryByTestId('inherited-element')).toBeEnabled()
262-
}).toThrowError()
262+
}).toThrow()
263263
expect(queryByTestId('inside-legend-element')).toBeEnabled()
264264
expect(queryByTestId('nested-inside-legend-element')).toBeEnabled()
265265

266266
expect(queryByTestId('first-legend-element')).toBeEnabled()
267267
expect(() => {
268268
expect(queryByTestId('second-legend-element')).toBeEnabled()
269-
}).toThrowError()
269+
}).toThrow()
270270

271271
expect(() => {
272272
expect(queryByTestId('outer-fieldset-element')).toBeEnabled()
273-
}).toThrowError()
273+
}).toThrow()
274274
})
275275

276276
test('.toBeEnabled custom element', () => {
@@ -282,10 +282,10 @@ test('.toBeEnabled custom element', () => {
282282
expect(queryByTestId('disabled-custom-element')).not.toBeEnabled()
283283
expect(() => {
284284
expect(queryByTestId('disabled-custom-element')).toBeEnabled()
285-
}).toThrowError('element is not enabled')
285+
}).toThrow('element is not enabled')
286286

287287
expect(queryByTestId('enabled-custom-element')).toBeEnabled()
288288
expect(() => {
289289
expect(queryByTestId('enabled-custom-element')).not.toBeEnabled()
290-
}).toThrowError('element is enabled')
290+
}).toThrow('element is enabled')
291291
})

0 commit comments

Comments
 (0)