Skip to content
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

Allowed named classes and functions as BlockNames #5154

Merged
merged 13 commits into from
Dec 24, 2017
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

### Features

* `[jest-jasmine]` Allowed classes and functions as `describe` names
([#5154](https://github.com/facebook/jest/pull/5154))
* `[jest-jasmine2]` Support generator functions as specs.
([#5166](https://github.com/facebook/jest/pull/5166))

Expand Down
17 changes: 17 additions & 0 deletions integration_tests/__tests__/__snapshots__/globals.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,20 @@ Time: <<REPLACED>>
Ran all test suites.
"
`;

exports[`function as descriptor 1`] = `
"PASS __tests__/function-as-descriptor.test.js
Foo
✓ it

"
`;

exports[`function as descriptor 2`] = `
"Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
18 changes: 18 additions & 0 deletions integration_tests/__tests__/globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,21 @@ test('tests with no implementation with expand arg', () => {
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});

test('function as descriptor', () => {
const filename = 'function-as-descriptor.test.js';
const content = `
function Foo() {}
describe(Foo, () => {
it('it', () => {});
});
`;

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, status} = runJest(DIR);
expect(status).toBe(0);

const {summary, rest} = extractSummary(stderr);
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});
29 changes: 28 additions & 1 deletion packages/jest-jasmine2/src/jasmine/Suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import expectationResultFactory from '../expectation_result_factory';
export default function Suite(attrs: Object) {
this.id = attrs.id;
this.parentSuite = attrs.parentSuite;
this.description = attrs.description;
this.description = convertDescriptorToString(attrs.description);
this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;

this.beforeFns = [];
Expand Down Expand Up @@ -181,6 +181,33 @@ 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/);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If something silly, such as a number or { toString() { return "nope"; } is passed here, it'll crash. How would you like this function to deal with those error handling cases?

Copy link
Member

Choose a reason for hiding this comment

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

Can you just make the function throw in that case and ask people to provide a string, class or function instead?

const indexOfNameSpace =
typeDescriptorMatch.index + typeDescriptorMatch[0].length;
const indexOfNameAfterSpace = stringified.search(/\(|\{/, indexOfNameSpace);
Copy link
Member

Choose a reason for hiding this comment

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

@JoshuaKGoldberg I'm looking at converting this to TS, and it's not happy about the second argument here. It's also not documented at MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search

Should I just remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@SimenB ha, sure.

Looks like yet another "bug" fixed by TypeScript! 😍

const name = stringified.substring(indexOfNameSpace, indexOfNameAfterSpace);

return name.trim();
}

function isAfterAll(children) {
return children && children[0].result.status;
}
Expand Down
2 changes: 1 addition & 1 deletion types/Circus.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

export type DoneFn = (reason?: string | Error) => void;
export type BlockFn = () => void;
export type BlockName = string;
export type BlockName = string | Function;
export type BlockMode = void | 'skip' | 'only';
export type TestMode = BlockMode;
export type TestName = string;
Expand Down