Skip to content

Commit a49d2de

Browse files
authored
docs: improve exception matching (#13407)
1 parent 07c4168 commit a49d2de

File tree

7 files changed

+42
-14
lines changed

7 files changed

+42
-14
lines changed

docs/UsingMatchers.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,20 @@ If you want to test whether a particular function throws an error when it's call
142142

143143
```js
144144
function compileAndroidCode() {
145-
throw new Error('you are using the wrong JDK');
145+
throw new Error('you are using the wrong JDK!');
146146
}
147147

148148
test('compiling android goes as expected', () => {
149149
expect(() => compileAndroidCode()).toThrow();
150150
expect(() => compileAndroidCode()).toThrow(Error);
151151

152-
// You can also use the exact error message or a regexp
152+
// You can also use a string that must be contained in the error message or a regexp
153153
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
154154
expect(() => compileAndroidCode()).toThrow(/JDK/);
155+
156+
// Or you can match an exact error mesage using a regexp like below
157+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
158+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
155159
});
156160
```
157161

website/versioned_docs/version-25.x/UsingMatchers.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,20 @@ If you want to test whether a particular function throws an error when it's call
142142

143143
```js
144144
function compileAndroidCode() {
145-
throw new Error('you are using the wrong JDK');
145+
throw new Error('you are using the wrong JDK!');
146146
}
147147

148148
test('compiling android goes as expected', () => {
149149
expect(() => compileAndroidCode()).toThrow();
150150
expect(() => compileAndroidCode()).toThrow(Error);
151151

152-
// You can also use the exact error message or a regexp
152+
// You can also use a string that must be contained in the error message or a regexp
153153
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
154154
expect(() => compileAndroidCode()).toThrow(/JDK/);
155+
156+
// Or you can match an exact error mesage using a regexp like below
157+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
158+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
155159
});
156160
```
157161

website/versioned_docs/version-26.x/UsingMatchers.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,20 @@ If you want to test whether a particular function throws an error when it's call
142142

143143
```js
144144
function compileAndroidCode() {
145-
throw new Error('you are using the wrong JDK');
145+
throw new Error('you are using the wrong JDK!');
146146
}
147147

148148
test('compiling android goes as expected', () => {
149149
expect(() => compileAndroidCode()).toThrow();
150150
expect(() => compileAndroidCode()).toThrow(Error);
151151

152-
// You can also use the exact error message or a regexp
152+
// You can also use a string that must be contained in the error message or a regexp
153153
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
154154
expect(() => compileAndroidCode()).toThrow(/JDK/);
155+
156+
// Or you can match an exact error mesage using a regexp like below
157+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
158+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
155159
});
156160
```
157161

website/versioned_docs/version-27.x/UsingMatchers.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,20 @@ If you want to test whether a particular function throws an error when it's call
142142

143143
```js
144144
function compileAndroidCode() {
145-
throw new Error('you are using the wrong JDK');
145+
throw new Error('you are using the wrong JDK!');
146146
}
147147

148148
test('compiling android goes as expected', () => {
149149
expect(() => compileAndroidCode()).toThrow();
150150
expect(() => compileAndroidCode()).toThrow(Error);
151151

152-
// You can also use the exact error message or a regexp
152+
// You can also use a string that must be contained in the error message or a regexp
153153
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
154154
expect(() => compileAndroidCode()).toThrow(/JDK/);
155+
156+
// Or you can match an exact error mesage using a regexp like below
157+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
158+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
155159
});
156160
```
157161

website/versioned_docs/version-28.x/UsingMatchers.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,20 @@ If you want to test whether a particular function throws an error when it's call
142142

143143
```js
144144
function compileAndroidCode() {
145-
throw new Error('you are using the wrong JDK');
145+
throw new Error('you are using the wrong JDK!');
146146
}
147147

148148
test('compiling android goes as expected', () => {
149149
expect(() => compileAndroidCode()).toThrow();
150150
expect(() => compileAndroidCode()).toThrow(Error);
151151

152-
// You can also use the exact error message or a regexp
152+
// You can also use a string that must be contained in the error message or a regexp
153153
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
154154
expect(() => compileAndroidCode()).toThrow(/JDK/);
155+
156+
// Or you can match an exact error mesage using a regexp like below
157+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
158+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
155159
});
156160
```
157161

website/versioned_docs/version-29.0/UsingMatchers.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,20 @@ If you want to test whether a particular function throws an error when it's call
142142

143143
```js
144144
function compileAndroidCode() {
145-
throw new Error('you are using the wrong JDK');
145+
throw new Error('you are using the wrong JDK!');
146146
}
147147

148148
test('compiling android goes as expected', () => {
149149
expect(() => compileAndroidCode()).toThrow();
150150
expect(() => compileAndroidCode()).toThrow(Error);
151151

152-
// You can also use the exact error message or a regexp
152+
// You can also use a string that must be contained in the error message or a regexp
153153
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
154154
expect(() => compileAndroidCode()).toThrow(/JDK/);
155+
156+
// Or you can match an exact error mesage using a regexp like below
157+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
158+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
155159
});
156160
```
157161

website/versioned_docs/version-29.1/UsingMatchers.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,20 @@ If you want to test whether a particular function throws an error when it's call
142142

143143
```js
144144
function compileAndroidCode() {
145-
throw new Error('you are using the wrong JDK');
145+
throw new Error('you are using the wrong JDK!');
146146
}
147147

148148
test('compiling android goes as expected', () => {
149149
expect(() => compileAndroidCode()).toThrow();
150150
expect(() => compileAndroidCode()).toThrow(Error);
151151

152-
// You can also use the exact error message or a regexp
152+
// You can also use a string that must be contained in the error message or a regexp
153153
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
154154
expect(() => compileAndroidCode()).toThrow(/JDK/);
155+
156+
// Or you can match an exact error mesage using a regexp like below
157+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
158+
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
155159
});
156160
```
157161

0 commit comments

Comments
 (0)