diff --git a/integration-tests/__tests__/__snapshots__/globals.test.js.snap b/integration-tests/__tests__/__snapshots__/globals.test.js.snap index 30496da2c554..4e61b01522ec 100644 --- a/integration-tests/__tests__/__snapshots__/globals.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/globals.test.js.snap @@ -32,8 +32,7 @@ exports[`cannot test with no implementation 1`] = ` 4 | test('test, no implementation'); 5 | - at packages/jest-jasmine2/build/jasmine/Env.js:<>:<> - at __tests__/only-constructs.test.js:<>:<> + at __tests__/only-constructs.test.js:3:5 " `; @@ -60,8 +59,7 @@ exports[`cannot test with no implementation with expand arg 1`] = ` 4 | test('test, no implementation'); 5 | - at packages/jest-jasmine2/build/jasmine/Env.js:<>:<> - at __tests__/only-constructs.test.js:<>:<> + at __tests__/only-constructs.test.js:3:5 " `; diff --git a/integration-tests/__tests__/globals.test.js b/integration-tests/__tests__/globals.test.js index 22a8e60f6041..47617b9ea181 100644 --- a/integration-tests/__tests__/globals.test.js +++ b/integration-tests/__tests__/globals.test.js @@ -21,6 +21,11 @@ const TEST_DIR = path.resolve(DIR, '__tests__'); SkipOnWindows.suite(); +function cleanStderr(stderr) { + const {rest} = extractSummary(stderr); + return rest.replace(/.*(jest-jasmine2|jest-circus).*\n/g, ''); +} + beforeEach(() => { cleanup(DIR); createEmptyPackage(DIR); @@ -122,8 +127,8 @@ test('cannot test with no implementation', () => { const {stderr, status} = runJest(DIR); expect(status).toBe(1); - const {summary, rest} = extractSummary(stderr, {stripLocation: true}); - expect(rest).toMatchSnapshot(); + const {summary} = extractSummary(stderr); + expect(cleanStderr(stderr)).toMatchSnapshot(); expect(summary).toMatchSnapshot(); }); @@ -200,8 +205,8 @@ test('cannot test with no implementation with expand arg', () => { const {stderr, status} = runJest(DIR, ['--expand']); expect(status).toBe(1); - const {summary, rest} = extractSummary(stderr, {stripLocation: true}); - expect(rest).toMatchSnapshot(); + const {summary} = extractSummary(stderr); + expect(cleanStderr(stderr)).toMatchSnapshot(); expect(summary).toMatchSnapshot(); }); diff --git a/packages/jest-circus/package.json b/packages/jest-circus/package.json index 0cef9d7a31a1..d3f53a3c6b1e 100644 --- a/packages/jest-circus/package.json +++ b/packages/jest-circus/package.json @@ -13,7 +13,8 @@ "jest-diff": "^23.0.0", "jest-matcher-utils": "^23.0.0", "jest-message-util": "^23.0.0", - "jest-snapshot": "^23.0.0" + "jest-snapshot": "^23.0.0", + "jest-util": "^23.0.0" }, "devDependencies": { "jest-runtime": "^23.0.0" diff --git a/packages/jest-circus/src/event_handler.js b/packages/jest-circus/src/event_handler.js index 86ba0e705a61..8352b8004ce3 100644 --- a/packages/jest-circus/src/event_handler.js +++ b/packages/jest-circus/src/event_handler.js @@ -35,7 +35,7 @@ const handler: EventHandler = (event, state): void => { } case 'finish_describe_definition': { const {currentDescribeBlock} = state; - invariant(currentDescribeBlock, `currentDescribeBlock mest to be there`); + invariant(currentDescribeBlock, `currentDescribeBlock must be there`); if (currentDescribeBlock.parent) { state.currentDescribeBlock = currentDescribeBlock.parent; } diff --git a/packages/jest-circus/src/utils.js b/packages/jest-circus/src/utils.js index b28d6d51152f..0b418a3f7f05 100644 --- a/packages/jest-circus/src/utils.js +++ b/packages/jest-circus/src/utils.js @@ -21,13 +21,14 @@ import type { TestName, TestResults, } from 'types/Circus'; +import {convertDescriptorToString} from 'jest-util'; export const makeDescribe = ( name: BlockName, parent: ?DescribeBlock, mode?: BlockMode, ): DescribeBlock => { - let _mode; + let _mode = mode; if (parent && !mode) { // If not set explicitly, inherit from the parent describe. _mode = parent.mode; @@ -37,7 +38,7 @@ export const makeDescribe = ( children: [], hooks: [], mode: _mode, - name, + name: convertDescriptorToString(name), parent, tests: [], }; @@ -50,10 +51,8 @@ export const makeTest = ( parent: DescribeBlock, timeout: ?number, ): TestEntry => { - let _mode; - if (!fn) { - _mode = 'skip'; // skip test if no fn passed - } else if (!mode) { + let _mode = mode; + if (!mode) { // if not set explicitly, inherit from its parent describe _mode = parent.mode; } @@ -63,7 +62,7 @@ export const makeTest = ( errors: [], fn, mode: _mode, - name, + name: convertDescriptorToString(name), parent, startedAt: null, status: null, diff --git a/packages/jest-jasmine2/src/jasmine/Suite.js b/packages/jest-jasmine2/src/jasmine/Suite.js index e37ee5e99c80..307840c2dec6 100644 --- a/packages/jest-jasmine2/src/jasmine/Suite.js +++ b/packages/jest-jasmine2/src/jasmine/Suite.js @@ -31,8 +31,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. /* @flow */ /* eslint-disable sort-keys */ +import {convertDescriptorToString} from 'jest-util'; import ExpectationFailed from '../expectation_failed'; - import expectationResultFactory from '../expectation_result_factory'; export default function Suite(attrs: Object) { @@ -182,33 +182,6 @@ Suite.prototype.addExpectationResult = function() { } }; -function convertDescriptorToString(descriptor) { - if ( - typeof descriptor === 'string' || - typeof descriptor === 'number' || - descriptor === undefined - ) { - return descriptor; - } - - if (typeof descriptor !== 'function') { - throw new Error('describe expects a class, function, number, or string.'); - } - - if (descriptor.name !== undefined) { - return descriptor.name; - } - - const stringified = descriptor.toString(); - const typeDescriptorMatch = stringified.match(/class|function/); - const indexOfNameSpace = - typeDescriptorMatch.index + typeDescriptorMatch[0].length; - const indexOfNameAfterSpace = stringified.search(/\(|\{/, indexOfNameSpace); - const name = stringified.substring(indexOfNameSpace, indexOfNameAfterSpace); - - return name.trim(); -} - function isAfterAll(children) { return children && children[0] && children[0].result.status; } diff --git a/packages/jest-util/src/convert_descriptor_to_string.js b/packages/jest-util/src/convert_descriptor_to_string.js new file mode 100644 index 000000000000..c77c8eac592e --- /dev/null +++ b/packages/jest-util/src/convert_descriptor_to_string.js @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2017-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +// See: https://github.com/facebook/jest/pull/5154 +export default function convertDescriptorToString( + descriptor: string | Function, +) { + if ( + typeof descriptor === 'string' || + typeof descriptor === 'number' || + descriptor === undefined + ) { + return descriptor; + } + + if (typeof descriptor !== 'function') { + throw new Error('describe expects a class, function, number, or string.'); + } + + if (descriptor.name !== undefined) { + return descriptor.name; + } + + // Fallback for old browsers, pardon Flow + const stringified = descriptor.toString(); + const typeDescriptorMatch = stringified.match(/class|function/); + const indexOfNameSpace = + // $FlowFixMe + typeDescriptorMatch.index + typeDescriptorMatch[0].length; + // $FlowFixMe + const indexOfNameAfterSpace = stringified.search(/\(|\{/, indexOfNameSpace); + const name = stringified.substring(indexOfNameSpace, indexOfNameAfterSpace); + return name.trim(); +} diff --git a/packages/jest-util/src/index.js b/packages/jest-util/src/index.js index be4389bfa810..2b00ae975dca 100644 --- a/packages/jest-util/src/index.js +++ b/packages/jest-util/src/index.js @@ -22,6 +22,7 @@ import isInteractive from './is_interative'; import getCallsite from './get_callsite'; import setGlobal from './set_global'; import deepCyclicCopy from './deep_cyclic_copy'; +import convertDescriptorToString from './convert_descriptor_to_string'; const createDirectory = (path: string) => { try { @@ -39,6 +40,7 @@ module.exports = { FakeTimers, NullConsole, clearLine, + convertDescriptorToString, createDirectory, deepCyclicCopy, formatTestResults,