From 2cb57a0a874c130253af641bc571ed5e7c48569c Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 24 Dec 2017 16:48:49 -0500 Subject: [PATCH] Allowed named classes and functions as BlockNames (#5154) * 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... --- CHANGELOG.md | 2 ++ .../__snapshots__/globals.test.js.snap | 17 +++++++++++ integration_tests/__tests__/globals.test.js | 18 ++++++++++++ packages/jest-jasmine2/src/jasmine/Suite.js | 29 ++++++++++++++++++- types/Circus.js | 2 +- 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edda47358609..4f526e2ace31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/integration_tests/__tests__/__snapshots__/globals.test.js.snap b/integration_tests/__tests__/__snapshots__/globals.test.js.snap index f58cbac51190..809211cb5725 100644 --- a/integration_tests/__tests__/__snapshots__/globals.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/globals.test.js.snap @@ -156,3 +156,20 @@ Time: <> 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: <> +Ran all test suites. +" +`; diff --git a/integration_tests/__tests__/globals.test.js b/integration_tests/__tests__/globals.test.js index 314cdc460466..6c7c7b88d8db 100644 --- a/integration_tests/__tests__/globals.test.js +++ b/integration_tests/__tests__/globals.test.js @@ -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(); +}); diff --git a/packages/jest-jasmine2/src/jasmine/Suite.js b/packages/jest-jasmine2/src/jasmine/Suite.js index 498eaf1507d0..9fcd6ba9f353 100644 --- a/packages/jest-jasmine2/src/jasmine/Suite.js +++ b/packages/jest-jasmine2/src/jasmine/Suite.js @@ -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 = []; @@ -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; } diff --git a/types/Circus.js b/types/Circus.js index 40456aa0b485..441c7ac277a3 100644 --- a/types/Circus.js +++ b/types/Circus.js @@ -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;