Skip to content

jest-circus: support globals #6283

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

Merged
merged 4 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:<<LINE>>:<<COLUMN>>
at __tests__/only-constructs.test.js:<<LINE>>:<<COLUMN>>
at __tests__/only-constructs.test.js:3:5

"
`;
Expand All @@ -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:<<LINE>>:<<COLUMN>>
at __tests__/only-constructs.test.js:<<LINE>>:<<COLUMN>>
at __tests__/only-constructs.test.js:3:5

"
`;
Expand Down
13 changes: 9 additions & 4 deletions integration-tests/__tests__/globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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();
});

Expand Down
3 changes: 2 additions & 1 deletion packages/jest-circus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/src/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const handler: EventHandler = (event, state): void => {
}
case 'finish_describe_definition': {
const {currentDescribeBlock} = state;
invariant(currentDescribeBlock, `currentDescribeBlock mest to be there`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what i was even trying to say :)

invariant(currentDescribeBlock, `currentDescribeBlock must be there`);
if (currentDescribeBlock.parent) {
state.currentDescribeBlock = currentDescribeBlock.parent;
}
Expand Down
13 changes: 6 additions & 7 deletions packages/jest-circus/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,7 +38,7 @@ export const makeDescribe = (
children: [],
hooks: [],
mode: _mode,
name,
name: convertDescriptorToString(name),
parent,
tests: [],
};
Expand All @@ -50,10 +51,8 @@ export const makeTest = (
parent: DescribeBlock,
timeout: ?number,
): TestEntry => {
let _mode;
if (!fn) {
_mode = 'skip'; // skip test if no fn passed
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We throw if fn is not defined

} else if (!mode) {
let _mode = mode;
if (!mode) {
// if not set explicitly, inherit from its parent describe
_mode = parent.mode;
}
Expand All @@ -63,7 +62,7 @@ export const makeTest = (
errors: [],
fn,
mode: _mode,
name,
name: convertDescriptorToString(name),
parent,
startedAt: null,
status: null,
Expand Down
29 changes: 1 addition & 28 deletions packages/jest-jasmine2/src/jasmine/Suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
40 changes: 40 additions & 0 deletions packages/jest-util/src/convert_descriptor_to_string.js
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 2 additions & 0 deletions packages/jest-util/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -39,6 +40,7 @@ module.exports = {
FakeTimers,
NullConsole,
clearLine,
convertDescriptorToString,
createDirectory,
deepCyclicCopy,
formatTestResults,
Expand Down