Skip to content

Commit

Permalink
.toThrowError() messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov committed Aug 30, 2016
1 parent d7316a8 commit fe06363
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 124 deletions.
5 changes: 4 additions & 1 deletion packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ const getType = (value: any): ValueType => {
const stringify = (obj: any): string => {
const set = new Set();
return JSON.stringify(obj, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (value instanceof Error) {
const name = (value.constructor && value.constructor.name) || 'Error';
return `${name}: ${value.message}`;
} else if (typeof value === 'object' && value !== null) {
if (
value &&
value.constructor &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,84 @@
exports[`.toThrowError() error class did not throw at all 1`] = `"Expected function to throw an error of type \"Err\"."`;
exports[`.toThrowError() error class did not throw at all 1`] = `
"expect(function).toThrowError(type)
exports[`.toThrowError() error class threw, but class did not match 1`] = `"Received \"Err: apple\" but expected Expected function to throw an error of type \"Err2\"."`;
Expected the function to throw an error of type:
\"Err\"
But it didn\'t throw anything."
`;

exports[`.toThrowError() error class threw, but should not have 1`] = `"Received \"Err: apple\" but expected the function not to throw an error of type \"Err\"."`;
exports[`.toThrowError() error class threw, but class did not match 1`] = `
"expect(function).toThrowError(type)
exports[`.toThrowError() regexp did not throw at all 1`] = `"the function to throw an error matching \"/apple/\"."`;
Expected the function to throw an error of type:
\"Err2\"
Instead, it threw:
\"Err: apple\""
`;

exports[`.toThrowError() regexp threw, but message did not match 1`] = `"Received \"Error: apple\" but expected the function to throw an error matching \"/banana/\"."`;
exports[`.toThrowError() error class threw, but should not have 1`] = `
"expect(function).not.toThrowError(type)
exports[`.toThrowError() regexp threw, but should not have 1`] = `"Received \"Error: apple\" but expected the function not to throw an error matching \"/apple/\"."`;
Expeced the function not to throw an error of type:
\"Err\"
But it threw:
\"Err: apple\""
`;

exports[`.toThrowError() strings did not throw at all 1`] = `"the function to throw an error matching \"apple\"."`;
exports[`.toThrowError() invalid arguments 1`] = `
"Unexpected argument passed. Expected to get
\"\\\"string\\\"\", \"\\\"Error type\\\"\" or \"\\\"regexp\\\"\".
Got:
\"number\": 111."
`;

exports[`.toThrowError() strings threw, but message did not match 1`] = `"Received \"Error: apple\" but expected the function to throw an error matching \"banana\"."`;
exports[`.toThrowError() regexp did not throw at all 1`] = `
"expect(function).toThrowError(regexp)
exports[`.toThrowError() strings threw, but should not have 1`] = `"Received \"Error: apple\" but expected the function not to throw an error matching \"apple\"."`;
Expected the function to throw an error matching:
\"/apple/\"
But it didn\'t throw anything."
`;

exports[`.toThrowError() regexp threw, but message did not match 1`] = `
"expect(function).toThrowError(regexp)
Expected the function to throw an error matching:
\"/banana/\"
Instead, it threw:
\"Error: apple\""
`;

exports[`.toThrowError() regexp threw, but should not have 1`] = `
"expect(function).not.toThrowError(regexp)
Expeced the function not to throw an error matching:
\"/apple/\"
But it threw:
\"Error: apple\""
`;

exports[`.toThrowError() strings did not throw at all 1`] = `
"expect(function).toThrowError(string)
Expected the function to throw an error matching:
\"apple\"
But it didn\'t throw anything."
`;

exports[`.toThrowError() strings threw, but message did not match 1`] = `
"expect(function).toThrowError(string)
Expected the function to throw an error matching:
\"banana\"
Instead, it threw:
\"Error: apple\""
`;

exports[`.toThrowError() strings threw, but should not have 1`] = `
"expect(function).not.toThrowError(string)
Expeced the function not to throw an error matching:
\"apple\"
But it threw:
\"Error: apple\""
`;
24 changes: 24 additions & 0 deletions packages/jest-matchers/src/__tests__/_matchErrorSnapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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';

module.exports = fn => {
let error;

try {
fn();
} catch (e) {
error = e;
}

expect(error).toBeDefined();
expect(error.message).toMatchSnapshot();
};
13 changes: 1 addition & 12 deletions packages/jest-matchers/src/__tests__/matchers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@
const jestExpect = require('../').expect;
const {stringify} = require('jest-matcher-utils');

const matchErrorSnapshot = fn => {
let error;

try {
fn();
} catch (e) {
error = e;
}

expect(error).toBeDefined();
expect(error.message).toMatchSnapshot();
};
const matchErrorSnapshot = require('./_matchErrorSnapshot');

describe('.toBe()', () => {
it('does not throw', () => {
Expand Down
128 changes: 39 additions & 89 deletions packages/jest-matchers/src/__tests__/toThrowMatchers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,38 @@

'use strict';

const jestExpect = require('../').expect;
const matchErrorSnapshot = require('./_matchErrorSnapshot');

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');
jestExpect(() => { throw new Error('apple'); }).toThrowError('apple');
jestExpect(() => { throw new Error('banana'); })
.not.toThrowError('apple');
jestExpect(() => {}).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();
matchErrorSnapshot(() => jestExpect(() => {}).toThrowError('apple'));
});

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();
matchErrorSnapshot(() =>
jestExpect(() => { throw new Error('apple'); }).toThrowError('banana'),
);
});

it('properly escapes strings when matching against errors', () => {
expect(() => { throw new TypeError('"this"? throws.'); })
jestExpect(() => { throw new TypeError('"this"? throws.'); })
.toThrowError('"this"? throws.');
});

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();
matchErrorSnapshot(() => {
jestExpect(() => { throw new Error('apple'); })
.not.toThrowError('apple');
});
});
});

Expand All @@ -68,39 +53,20 @@ describe('.toThrowError()', () => {
});

test('did not throw at all', () => {
let error;
try {
expect(() => {}).toThrowError(/apple/);
} catch (e) {
error = e;
}

expect(error).toBeDefined();
expect(error.message).toMatchSnapshot();
matchErrorSnapshot(() => jestExpect(() => {}).toThrowError(/apple/));
});

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();
matchErrorSnapshot(() => {
jestExpect(() => { throw new Error('apple'); }).toThrowError(/banana/);
});
});

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();
matchErrorSnapshot(() => {
jestExpect(() => { throw new Error('apple'); })
.not.toThrowError(/apple/);
});
});
});

Expand All @@ -109,46 +75,30 @@ describe('.toThrowError()', () => {
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);
jestExpect(() => { throw new Err(); }).toThrowError(Err);
jestExpect(() => { throw new Err(); }).toThrowError(Error);
jestExpect(() => { throw new Err(); }).not.toThrowError(Err2);
jestExpect(() => {}).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();
matchErrorSnapshot(() => expect(() => {}).toThrowError(Err));
});

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();
matchErrorSnapshot(() => {
jestExpect(() => { throw new Err('apple'); }).toThrowError(Err2);
});
});

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();
matchErrorSnapshot(() => {
jestExpect(() => { throw new Err('apple'); }).not.toThrowError(Err);
});
});
});

test('invalid arguments', () => {
matchErrorSnapshot(() => jestExpect(() => {}).toThrowError(111));
});
});
Loading

0 comments on commit fe06363

Please sign in to comment.