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
21 changes: 20 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,25 @@ Suite.prototype.addExpectationResult = function() {
}
};

function convertDescriptorToString(descriptor) {
if (typeof descriptor === 'string' || descriptor === undefined) {
return descriptor;
}

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