-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.toThrowError() #1301
Merged
Merged
.toThrowError() #1301
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/jest-matchers/src/__tests__/__snapshots__/toThrowMatchers-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
exports[`.toThrowError() error class did not throw at all 1`] = `"Expected the function to throw an error of [36m[1m\"Err\"[22m[39m type, but it didn\'t."`; | ||
|
||
exports[`.toThrowError() error class threw, but class did not match 1`] = ` | ||
"Expected the function to throw an error of [36m[1m\"Err2\"[22m[39m type, but it didn\'t. | ||
Actual error: | ||
Type: [36m[1m\"Err\"[22m[39m | ||
Message: [36m[1m\"apple\"[22m[39m" | ||
`; | ||
|
||
exports[`.toThrowError() error class threw, but should not have 1`] = ` | ||
"Expected the function to not throw an error of [36m[1m\"Err\"[22m[39m type, but it did. | ||
Actual error: | ||
Type: [36m[1m\"Err\"[22m[39m | ||
Message: [36m[1m\"apple\"[22m[39m" | ||
`; | ||
|
||
exports[`.toThrowError() regexp did not throw at all 1`] = `"Expected the function to throw an error matching [36m[1m\"/apple/\"[22m[39m, but it didn\'t."`; | ||
|
||
exports[`.toThrowError() regexp threw, but message did not match 1`] = ` | ||
"Expected the function to throw an error matching [36m[1m\"/banana/\"[22m[39m, but it didn\'t. | ||
Actual error: | ||
Type: [36m[1m\"Error\"[22m[39m | ||
Message: [36m[1m\"apple\"[22m[39m" | ||
`; | ||
|
||
exports[`.toThrowError() regexp threw, but should not have 1`] = ` | ||
"Expected the function to not throw an error matching [36m[1m\"/apple/\"[22m[39m, but it did. | ||
Actual error: | ||
Type: [36m[1m\"Error\"[22m[39m | ||
Message: [36m[1m\"apple\"[22m[39m" | ||
`; | ||
|
||
exports[`.toThrowError() strings did not throw at all 1`] = `"Expected the function to throw an error matching [36m[1m\"apple\"[22m[39m, but it didn\'t."`; | ||
|
||
exports[`.toThrowError() strings threw, but message did not match 1`] = ` | ||
"Expected the function to throw an error matching [36m[1m\"banana\"[22m[39m, but it didn\'t. | ||
Actual error: | ||
Type: [36m[1m\"Error\"[22m[39m | ||
Message: [36m[1m\"apple\"[22m[39m" | ||
`; | ||
|
||
exports[`.toThrowError() strings threw, but should not have 1`] = ` | ||
"Expected the function to not throw an error matching [36m[1m\"apple\"[22m[39m, but it did. | ||
Actual error: | ||
Type: [36m[1m\"Error\"[22m[39m | ||
Message: [36m[1m\"apple\"[22m[39m" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
packages/jest-matchers/src/__tests__/toThrowMatchers-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails oncall+jsinfra | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('.toThrowError()', () => { | ||
describe('strings', () => { | ||
it('passes', () => { | ||
expect(() => { throw new Error('apple'); }).toThrowError('apple'); | ||
expect(() => { throw new Error('banana'); }).not.toThrowError('apple'); | ||
expect(() => {}).not.toThrowError('apple'); | ||
}); | ||
|
||
test('did not throw at all', () => { | ||
let error; | ||
try { | ||
expect(() => {}).toThrowError('apple'); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatchSnapshot(); | ||
}); | ||
|
||
test('threw, but message did not match', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Error('apple'); }).toThrowError('banana'); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatchSnapshot(); | ||
}); | ||
|
||
test('threw, but should not have', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Error('apple'); }).not.toThrowError('apple'); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('regexp', () => { | ||
it('passes', () => { | ||
expect(() => { throw new Error('apple'); }).toThrowError(/apple/); | ||
expect(() => { throw new Error('banana'); }).not.toThrowError(/apple/); | ||
expect(() => {}).not.toThrowError(/apple/); | ||
}); | ||
|
||
test('did not throw at all', () => { | ||
let error; | ||
try { | ||
expect(() => {}).toThrowError(/apple/); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatchSnapshot(); | ||
}); | ||
|
||
test('threw, but message did not match', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Error('apple'); }).toThrowError(/banana/); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatchSnapshot(); | ||
}); | ||
|
||
test('threw, but should not have', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Error('apple'); }).not.toThrowError(/apple/); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('error class', () => { | ||
class Err extends Error {} | ||
class Err2 extends Error {} | ||
|
||
it('passes', () => { | ||
expect(() => { throw new Err(); }).toThrowError(Err); | ||
expect(() => { throw new Err(); }).toThrowError(Error); | ||
expect(() => { throw new Err(); }).not.toThrowError(Err2); | ||
expect(() => {}).not.toThrowError(Err); | ||
}); | ||
|
||
test('did not throw at all', () => { | ||
let error; | ||
try { | ||
expect(() => {}).toThrowError(Err); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatchSnapshot(); | ||
}); | ||
|
||
test('threw, but class did not match', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Err('apple'); }).toThrowError(Err2); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatchSnapshot(); | ||
}); | ||
|
||
test('threw, but should not have', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Err('apple'); }).not.toThrowError(Err); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,9 +20,15 @@ import type { | |
} from './types'; | ||
|
||
const matchers = require('./matchers'); | ||
const spyMatchers = require('./spy-matchers'); | ||
const spyMatchers = require('./spyMatchers'); | ||
const toThrowMatchers = require('./toThrowMatchers'); | ||
|
||
const {stringify} = require('jest-matcher-utils'); | ||
|
||
const GLOBAL_MATCHERS_OBJECT_SYMBOL = Symbol.for('$$jest-matchers-object'); | ||
|
||
class JestAssertionError extends Error {} | ||
|
||
if (!global[GLOBAL_MATCHERS_OBJECT_SYMBOL]) { | ||
Object.defineProperty( | ||
global, | ||
|
@@ -57,6 +63,8 @@ const makeThrowingMatcher = ( | |
{args: arguments}, | ||
); | ||
|
||
_validateResult(result); | ||
|
||
if ((result.pass && isNot) || (!result.pass && !isNot)) { // XOR | ||
let message = result.message; | ||
|
||
|
@@ -66,7 +74,7 @@ const makeThrowingMatcher = ( | |
message = message(); | ||
} | ||
|
||
const error = new Error(message); | ||
const error = new JestAssertionError(message); | ||
// Remove this function from the stack trace frame. | ||
Error.captureStackTrace(error, throwingMatcher); | ||
throw error; | ||
|
@@ -78,9 +86,29 @@ const addMatchers = (matchersObj: MatchersObject): void => { | |
Object.assign(global[GLOBAL_MATCHERS_OBJECT_SYMBOL], matchersObj); | ||
}; | ||
|
||
const _validateResult = result => { | ||
if ( | ||
typeof result !== 'object' || | ||
typeof result.pass !== 'boolean' || | ||
!( | ||
typeof result.message === 'string' || | ||
typeof result.message === 'function' | ||
) | ||
) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: codestyle typeof result.pass !== 'boolean' ||
!(
typeof result.message === 'string' ||
typeof result.message === 'function'
) |
||
throw new Error( | ||
'Unexpected return from a matcher function.\n' + | ||
'Matcher functions should ' + | ||
'return an object in the following format:\n' + | ||
' {message: string | function, pass: boolean}\n' + | ||
`'${stringify(result)}' was returned`, | ||
); | ||
} | ||
}; | ||
|
||
// add default jest matchers | ||
addMatchers(matchers); | ||
addMatchers(spyMatchers); | ||
addMatchers(toThrowMatchers); | ||
|
||
module.exports = { | ||
addMatchers, | ||
|
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using an arrow function here also causes an unnecassary
.bind(this)
call. It'd be more efficient to use a normal function hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true. i'd be interested in benchmarking this stuff. I don't think it's going to affect the overall performance that much though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There aren't any unnecessary bind calls because this is run in node 4 without compiling arrow functions to functions. Also babel doesn't bind if there is no this access iirc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:themoreyouknow: I benchmarked the arrow vs regular function in JavaScriptCore and to my surprise, arrow functions are natively faster.