Skip to content

Commit ff54db2

Browse files
authored
polish: add expectToThrow test helper (#4693)
extracted from #4670 (where this was originally called `catchThrownError`)
1 parent 23cfd3b commit ff54db2

5 files changed

Lines changed: 58 additions & 35 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect } from 'chai';
2+
import { describe, it } from 'mocha';
3+
4+
import { expectToThrow } from '../expectToThrow.js';
5+
6+
describe('expectToThrow', () => {
7+
it('returns the thrown error', () => {
8+
const error = new Error('oops');
9+
expect(
10+
expectToThrow(() => {
11+
throw error;
12+
}),
13+
).to.equal(error);
14+
});
15+
16+
it('returns the same thrown error instance', () => {
17+
const error = new Error('oops');
18+
expect(
19+
expectToThrow(() => {
20+
throw error;
21+
}),
22+
).to.equal(error);
23+
});
24+
25+
it('throws if callback does not throw', () => {
26+
expect(() => expectToThrow(() => 123)).to.throw(
27+
'Expected function to throw, but it completed successfully.',
28+
);
29+
});
30+
});

src/__testUtils__/expectToThrow.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { assert } from 'chai';
2+
3+
export function expectToThrow(fn: () => unknown): unknown {
4+
try {
5+
fn();
6+
} catch (error) {
7+
return error;
8+
}
9+
10+
assert.fail('Expected function to throw, but it completed successfully.');
11+
}

src/language/__tests__/lexer-test.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, it } from 'mocha';
33

44
import { dedent } from '../../__testUtils__/dedent.js';
55
import { expectToThrowJSON } from '../../__testUtils__/expectJSON.js';
6+
import { expectToThrow } from '../../__testUtils__/expectToThrow.js';
67

78
import { inspect } from '../../jsutils/inspect.js';
89

@@ -173,12 +174,9 @@ describe('Lexer', () => {
173174
});
174175

175176
it('errors respect whitespace', () => {
176-
let caughtError;
177-
try {
178-
lexOne(['', '', ' ~', ''].join('\n'));
179-
} catch (error) {
180-
caughtError = error;
181-
}
177+
const caughtError = expectToThrow(() =>
178+
lexOne(['', '', ' ~', ''].join('\n')),
179+
);
182180
expect(String(caughtError)).to.equal(dedent`
183181
Syntax Error: Unexpected character: "~".
184182
@@ -191,14 +189,11 @@ describe('Lexer', () => {
191189
});
192190

193191
it('updates line numbers in error for file context', () => {
194-
let caughtError;
195-
try {
192+
const caughtError = expectToThrow(() => {
196193
const str = ['', '', ' ~', ''].join('\n');
197194
const source = new Source(str, 'foo.js', { line: 11, column: 12 });
198195
new Lexer(source).advance();
199-
} catch (error) {
200-
caughtError = error;
201-
}
196+
});
202197
expect(String(caughtError)).to.equal(dedent`
203198
Syntax Error: Unexpected character: "~".
204199
@@ -211,13 +206,10 @@ describe('Lexer', () => {
211206
});
212207

213208
it('updates column numbers in error for file context', () => {
214-
let caughtError;
215-
try {
209+
const caughtError = expectToThrow(() => {
216210
const source = new Source('~', 'foo.js', { line: 1, column: 5 });
217211
new Lexer(source).advance();
218-
} catch (error) {
219-
caughtError = error;
220-
}
212+
});
221213
expect(String(caughtError)).to.equal(dedent`
222214
Syntax Error: Unexpected character: "~".
223215

src/language/__tests__/parser-test.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
expectJSON,
77
expectToThrowJSON,
88
} from '../../__testUtils__/expectJSON.js';
9+
import { expectToThrow } from '../../__testUtils__/expectToThrow.js';
910
import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js';
1011

1112
import { inspect } from '../../jsutils/inspect.js';
@@ -27,12 +28,7 @@ function expectSyntaxError(text: string) {
2728

2829
describe('Parser', () => {
2930
it('parse provides useful errors', () => {
30-
let caughtError;
31-
try {
32-
parse('{');
33-
} catch (error) {
34-
caughtError = error;
35-
}
31+
const caughtError = expectToThrow(() => parse('{'));
3632

3733
expect(caughtError).to.deep.contain({
3834
message: 'Syntax Error: Expected Name, found <EOF>.',
@@ -78,12 +74,9 @@ describe('Parser', () => {
7874
});
7975

8076
it('parse provides useful error when using source', () => {
81-
let caughtError;
82-
try {
83-
parse(new Source('query', 'MyQuery.graphql'));
84-
} catch (error) {
85-
caughtError = error;
86-
}
77+
const caughtError = expectToThrow(() =>
78+
parse(new Source('query', 'MyQuery.graphql')),
79+
);
8780
expect(String(caughtError)).to.equal(dedent`
8881
Syntax Error: Expected "{", found <EOF>.
8982

src/utilities/__tests__/stripIgnoredCharacters-test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

44
import { dedent } from '../../__testUtils__/dedent.js';
5+
import { expectToThrow } from '../../__testUtils__/expectToThrow.js';
56
import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js';
67
import { kitchenSinkSDL } from '../../__testUtils__/kitchenSinkSDL.js';
78

@@ -78,13 +79,9 @@ describe('stripIgnoredCharacters', () => {
7879
});
7980

8081
it('report document with invalid token', () => {
81-
let caughtError;
82-
83-
try {
84-
stripIgnoredCharacters('{ foo(arg: "\n"');
85-
} catch (e) {
86-
caughtError = e;
87-
}
82+
const caughtError = expectToThrow(() =>
83+
stripIgnoredCharacters('{ foo(arg: "\n"'),
84+
);
8885

8986
expect(String(caughtError)).to.equal(dedent`
9087
Syntax Error: Unterminated string.

0 commit comments

Comments
 (0)