diff --git a/CHANGELOG.md b/CHANGELOG.md index 4babad50435c..920ac2e72e4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ - `[jest-jasmine2]` Fail synchronous test timeouts ([#7074](https://github.com/facebook/jest/pull/7074)) - `[jest-jasmine2]` Better error message when a describe block is empty ([#6372](https://github.com/facebook/jest/pull/6372)) - `[jest-circus]` Better error message when a describe block is empty ([#6372](https://github.com/facebook/jest/pull/6372)) +- `[expect]` Add empty string check to `toThrow` matcher ([#6836](https://github.com/facebook/jest/pull/6836)) ### Chore & Maintenance diff --git a/packages/expect/src/__tests__/__snapshots__/to_throw_matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/to_throw_matchers.test.js.snap index af577d3e0811..cddcca771f27 100644 --- a/packages/expect/src/__tests__/__snapshots__/to_throw_matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/to_throw_matchers.test.js.snap @@ -28,6 +28,16 @@ Instead, it threw: at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; +exports[`.toThrow() fails when given an empty string 1`] = ` +"expect(function).toThrow(string) + +Expected the function to throw an error matching: + \\"\\" +Instead, it threw: + Error + at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" +`; + exports[`.toThrow() invalid actual 1`] = ` "expect(function).toThrow(undefined) @@ -153,6 +163,16 @@ Instead, it threw: at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; +exports[`.toThrowError() fails when given an empty string 1`] = ` +"expect(function).toThrow(string) + +Expected the function to throw an error matching: + \\"\\" +Instead, it threw: + Error + at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" +`; + exports[`.toThrowError() invalid actual 1`] = ` "expect(function).toThrowError(undefined) diff --git a/packages/expect/src/__tests__/to_throw_matchers.test.js b/packages/expect/src/__tests__/to_throw_matchers.test.js index 3bbe89b6b933..a9a684b60024 100644 --- a/packages/expect/src/__tests__/to_throw_matchers.test.js +++ b/packages/expect/src/__tests__/to_throw_matchers.test.js @@ -28,6 +28,14 @@ class customError extends Error { class Err extends customError {} class Err2 extends customError {} + test('fails when given an empty string', () => { + expect(() => + jestExpect(() => { + throw new customError('apple'); + }).toThrow(''), + ).toThrowErrorMatchingSnapshot(); + }); + test('to throw or not to throw', () => { jestExpect(() => { throw new customError('apple'); diff --git a/packages/expect/src/to_throw_matchers.js b/packages/expect/src/to_throw_matchers.js index b5e953a10792..be02c275c093 100644 --- a/packages/expect/src/to_throw_matchers.js +++ b/packages/expect/src/to_throw_matchers.js @@ -51,7 +51,11 @@ export const createMatcher = (matcherName: string, fromPromise?: boolean) => ( } if (typeof expected === 'string') { - expected = new RegExp(escapeStrForRegex(expected)); + if (expected === '') { + expected = new RegExp('^$'); + } else { + expected = new RegExp(escapeStrForRegex(expected)); + } } if (typeof expected === 'function') { diff --git a/packages/jest-regex-util/src/__tests__/index.test.js b/packages/jest-regex-util/src/__tests__/index.test.js index 077e6a247445..57287cfbcd1c 100644 --- a/packages/jest-regex-util/src/__tests__/index.test.js +++ b/packages/jest-regex-util/src/__tests__/index.test.js @@ -1,6 +1,6 @@ jest.mock('path'); -import {replacePathSepForRegex} from '../index'; +import {replacePathSepForRegex, escapeStrForRegex} from '../index'; import path from 'path'; describe('replacePathSepForRegex()', () => { @@ -13,6 +13,12 @@ describe('replacePathSepForRegex()', () => { }); }); + describe('escapeStrForRegex', () => { + it('returns regex for given string: [0-9]', () => { + expect(escapeStrForRegex('[0-9]')).toBe('\\[0-9\\]'); + }); + }); + describe('win32', () => { beforeEach(() => (path.sep = '\\'));