-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99f6fa7
commit e3e9c3a
Showing
8 changed files
with
285 additions
and
24 deletions.
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
File renamed without changes.
161 changes: 161 additions & 0 deletions
161
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,161 @@ | ||
/** | ||
* 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('lol'); }).toThrowError('lol'); | ||
expect(() => { throw new Error('lmao'); }).not.toThrowError('lol'); | ||
expect(() => {}).not.toThrowError('lol'); | ||
}); | ||
|
||
test('did not throw at all', () => { | ||
let error; | ||
try { | ||
expect(() => {}).toThrowError('lol'); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatch( | ||
/Expected.*matching.*lol.*but it didn't\.$/, | ||
); | ||
}); | ||
|
||
test('threw, but message did not match', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Error('lol'); }).toThrowError('lmao'); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatch( | ||
/Expected.*matching.*\nActual error.*\n.*Error.*\n.*lol/, | ||
); | ||
}); | ||
|
||
test('threw, but should not have', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Error('lol'); }).not.toThrowError('lol'); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatch(/but it did\./); | ||
}); | ||
}); | ||
|
||
describe('regexp', () => { | ||
it('passes', () => { | ||
expect(() => { throw new Error('lol'); }).toThrowError(/lol/); | ||
expect(() => { throw new Error('lmao'); }).not.toThrowError(/lol/); | ||
expect(() => {}).not.toThrowError(/lol/); | ||
}); | ||
|
||
test('did not throw at all', () => { | ||
let error; | ||
try { | ||
expect(() => {}).toThrowError(/lol/); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatch( | ||
/Expected.*matching.*lol.*but it didn't\.$/, | ||
); | ||
}); | ||
|
||
test('threw, but message did not match', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Error('lol'); }).toThrowError(/lmao/); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatch( | ||
/Expected.*matching.*\nActual error.*\n.*Error.*\n.*lol/, | ||
); | ||
}); | ||
|
||
test('threw, but should not have', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Error('lol'); }).not.toThrowError(/lol/); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatch(/but it did\./); | ||
}); | ||
}); | ||
|
||
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).toMatch( | ||
/Expected.*of.*Err.*class.*but it didn't\.$/, | ||
); | ||
}); | ||
|
||
test('threw, but class did not match', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Err('lol'); }).toThrowError(Err2); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatch( | ||
/Expected.*of.*Err.*class.*\nActual error.*\n.*Err.*\n.*lol/, | ||
); | ||
}); | ||
|
||
test('threw, but should not have', () => { | ||
let error; | ||
try { | ||
expect(() => { throw new Err('lol'); }).not.toThrowError(Err); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBeDefined(); | ||
expect(error.message).toMatch(/but it did\./); | ||
}); | ||
}); | ||
}); |
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
File renamed without changes.
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,83 @@ | ||
/** | ||
* Copyright (c) 2014, 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. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {MatchersObject} from './types'; | ||
|
||
const {stringify, getType} = require('jest-matcher-utils'); | ||
|
||
const matchers: MatchersObject = { | ||
toThrowError(actual: Function, expected: string | Error | RegExp) { | ||
let error; | ||
|
||
try { | ||
actual(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
if (typeof expected === 'string') { | ||
return _toThrowMatchingStringOrRegexp(error, expected); | ||
} else if (typeof expected === 'function') { | ||
return _toThrowMatchingError(error, expected); | ||
} else if (expected instanceof RegExp) { | ||
return _toThrowMatchingStringOrRegexp(error, expected); | ||
} else { | ||
throw new Error( | ||
'Unexpected argument passed. Expected to get ' + | ||
`'string', 'Error class' or 'regexp'. Got: '${getType(expected)}' ` + | ||
`'${stringify(expected)}'`, | ||
); | ||
} | ||
}, | ||
}; | ||
|
||
const _toThrowMatchingStringOrRegexp = ( | ||
error, | ||
strOrRegExp: string | RegExp, | ||
) => { | ||
const pass = !!(error && error.message.match(strOrRegExp)); | ||
let message = pass | ||
? 'Expected the function to not throw error matching ' + | ||
`'${stringify(strOrRegExp)}', but it did.` | ||
: 'Expected the function to throw error matching ' + | ||
`'${stringify(strOrRegExp)}', but it didn't.`; | ||
|
||
if (error) { | ||
message += _printThrownError(error); | ||
} | ||
|
||
return {pass, message}; | ||
}; | ||
|
||
const _toThrowMatchingError = (error, ErrorClass) => { | ||
const pass = !!(error && error instanceof ErrorClass); | ||
let message = pass | ||
? `Expected the function to not throw error of '${ErrorClass.name}' ` + | ||
'class, but it did.' | ||
: `Expected the function to throw error of '${ErrorClass.name}' ` + | ||
`class, but it didn't.`; | ||
|
||
if (error) { | ||
message += _printThrownError(error); | ||
} | ||
|
||
return {pass, message}; | ||
}; | ||
|
||
const _printThrownError = error => { | ||
return '\n' + | ||
'Actual error:\n' + | ||
`class: '${error.constructor.name}'\n` + | ||
`message: '${error.message}'`; | ||
}; | ||
|
||
module.exports = matchers; |
This file was deleted.
Oops, something went wrong.