Skip to content

Commit

Permalink
Allowed named classes and functions as BlockNames (jestjs#5154)
Browse files Browse the repository at this point in the history
* Allowed named classes and functions as BlockNames

describe can now take in classes and functions as the names.

* Added integration test for describe with a class or function

* Corrected globals.test.js.snap

* Mentioned changes in CHANGELOG.md

* Master!

* Added failure for non-string/class/function

* I am become pretty

* Fixed failure message for globals.test.js.snap

* Removed error test case for array-as-descriptor

* Removed error test for array-as-descriptor

* Added allowance for numbers as test descriptors

* Prettier...
  • Loading branch information
Josh Goldberg authored and cpojer committed Dec 24, 2017
1 parent 23b7e98 commit 2cb57a0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
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/);
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].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

0 comments on commit 2cb57a0

Please sign in to comment.