You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/expect.md
+45-2Lines changed: 45 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ type Awaitable<T> = T | PromiseLike<T>
42
42
})
43
43
// At the end of the test, the above errors will be output.
44
44
```
45
-
45
+
46
46
It can also be used with `expect`. if `expect` assertion fails, the test will be terminated and all errors will be displayed.
47
47
48
48
```ts
@@ -54,7 +54,7 @@ type Awaitable<T> = T | PromiseLike<T>
54
54
expect.soft(1+2).toBe(4) // do not run
55
55
})
56
56
```
57
-
57
+
58
58
::: warning
59
59
`expect.soft` can only be used inside the [`test`](/api/#test) function.
60
60
:::
@@ -1136,6 +1136,49 @@ If the value in the error message is too truncated, you can increase [chaiConfig
1136
1136
})
1137
1137
```
1138
1138
1139
+
## expect.unreachable
1140
+
1141
+
-**Type:**`(message?: string) => never`
1142
+
1143
+
This method is used to asserting that a line should never be reached.
1144
+
1145
+
For example, if we want to test that `build()` throws due to receiving directories having no `src` folder, and also handle each error separately, we could do this:
1146
+
1147
+
```ts
1148
+
import { expect, test } from'vitest'
1149
+
1150
+
asyncfunction build(dir) {
1151
+
if (dir.includes('no-src'))
1152
+
thrownewError(`${dir}/src does not exist`)
1153
+
}
1154
+
1155
+
const errorDirs = [
1156
+
'no-src-folder',
1157
+
// ...
1158
+
]
1159
+
1160
+
test.each(errorDirs)('build fails with "%s"', async (dir) => {
1161
+
try {
1162
+
awaitbuild(dir)
1163
+
expect.unreachable('Should not pass build')
1164
+
}
1165
+
catch (err:any) {
1166
+
expect(err).toBeInstanceOf(Error)
1167
+
expect(err.stack).toContain('build')
1168
+
1169
+
switch (dir) {
1170
+
case'no-src-folder':
1171
+
expect(err.message).toBe(`${dir}/src does not exist`)
1172
+
break
1173
+
default:
1174
+
// to exhaust all error tests
1175
+
expect.unreachable('All error test must be handled')
0 commit comments