From 2ec0c8b02f5f23fb9ea832077f2525a0ef32a3ef Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Thu, 21 Dec 2017 11:25:17 -0500 Subject: [PATCH 01/12] Allowed named classes and functions as BlockNames describe can now take in classes and functions as the names. --- packages/jest-jasmine2/src/jasmine/Suite.js | 21 ++++++++++++++++++++- types/Circus.js | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/jest-jasmine2/src/jasmine/Suite.js b/packages/jest-jasmine2/src/jasmine/Suite.js index 498eaf1507d0..541507cc829b 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,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/); + 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; From 79a11aaf807571e87fb5e1e0631e6d633618618f Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 22 Dec 2017 20:52:45 -0500 Subject: [PATCH 02/12] Added integration test for describe with a class or function --- .../__snapshots__/globals.test.js.snap | 8 ++++++++ integration_tests/__tests__/globals.test.js | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/integration_tests/__tests__/__snapshots__/globals.test.js.snap b/integration_tests/__tests__/__snapshots__/globals.test.js.snap index f58cbac51190..8fe3f9782675 100644 --- a/integration_tests/__tests__/__snapshots__/globals.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/globals.test.js.snap @@ -156,3 +156,11 @@ Time: <> Ran all test suites. " `; + +exports[`function as descriptor`] = ` +"PASS __tests__/function-as-descriptor.test.js + Foo + ✓ it + +" +`; 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(); +}); From b335f411fa79f8411eaeb4506f75eace661002d7 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 23 Dec 2017 19:15:48 -0500 Subject: [PATCH 03/12] Corrected globals.test.js.snap --- .../__tests__/__snapshots__/globals.test.js.snap | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/integration_tests/__tests__/__snapshots__/globals.test.js.snap b/integration_tests/__tests__/__snapshots__/globals.test.js.snap index 8fe3f9782675..809211cb5725 100644 --- a/integration_tests/__tests__/__snapshots__/globals.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/globals.test.js.snap @@ -157,10 +157,19 @@ Ran all test suites. " `; -exports[`function as descriptor`] = ` +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. +" +`; From db2fc7dfe2b05e6181a7f999994ea73fc7e2fbab Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 23 Dec 2017 19:26:44 -0500 Subject: [PATCH 04/12] Mentioned changes in CHANGELOG.md --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e4503e4dfeb..d169e140efc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,13 @@ None for now ### Chore & Maintenance +## jest 22.0.5 + +### Features + +* `[jest-jasmine]` Allowed classes and functions as `describe` names +([#5154](https://github.com/facebook/jest/pull/5154)) + ## jest 22.0.1 ### Fixes From 0bae63c84399b8e8705141b0c33fccff55cbdedf Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 23 Dec 2017 19:44:33 -0500 Subject: [PATCH 05/12] Master! --- CHANGELOG.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d169e140efc9..0a87f5f53a84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,15 +6,11 @@ None for now ### Features -### Chore & Maintenance - -## jest 22.0.5 - -### Features - * `[jest-jasmine]` Allowed classes and functions as `describe` names ([#5154](https://github.com/facebook/jest/pull/5154)) +### Chore & Maintenance + ## jest 22.0.1 ### Fixes From 8cd85054722e94e83e1a6d9dc5bdf209a0fc0e38 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 24 Dec 2017 13:12:37 -0500 Subject: [PATCH 06/12] Added failure for non-string/class/function --- .../__snapshots__/globals.test.js.snap | 16 ++++++++++++++++ integration_tests/__tests__/globals.test.js | 17 +++++++++++++++++ packages/jest-jasmine2/src/jasmine/Suite.js | 4 ++++ 3 files changed, 37 insertions(+) diff --git a/integration_tests/__tests__/__snapshots__/globals.test.js.snap b/integration_tests/__tests__/__snapshots__/globals.test.js.snap index 809211cb5725..a80bea07f6b8 100644 --- a/integration_tests/__tests__/__snapshots__/globals.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/globals.test.js.snap @@ -173,3 +173,19 @@ Time: <> Ran all test suites. " `; + +exports[`array as descriptor 1`] = ` +"FAIL __tests__/function-as-descriptor.test.js + +describe expects a string, class, or function. +" +`; + +exports[`function as descriptor 2`] = ` +"Test Suites: 0 passed, 0 total +Tests: 0 passed, 0 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 6c7c7b88d8db..868ce54b9ca0 100644 --- a/integration_tests/__tests__/globals.test.js +++ b/integration_tests/__tests__/globals.test.js @@ -222,3 +222,20 @@ test('function as descriptor', () => { expect(rest).toMatchSnapshot(); expect(summary).toMatchSnapshot(); }); + +test('array as descriptor', () => { + const filename = 'array-as-descriptor.test.js'; + const content = ` + describe([], () => { + it('it', () => {}); + }); + `; + + writeFiles(TEST_DIR, {[filename]: content}); + const {stderr, status} = runJest(DIR); + expect(status).toBe(1); + + 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 541507cc829b..02f32af27e0d 100644 --- a/packages/jest-jasmine2/src/jasmine/Suite.js +++ b/packages/jest-jasmine2/src/jasmine/Suite.js @@ -186,6 +186,10 @@ function convertDescriptorToString(descriptor) { return descriptor; } + if (typeof descriptor !== "function") { + throw new Error("describe expects a string, class, or function."); + } + if (descriptor.name !== undefined) { return descriptor.name; } From 74ca5755c39f171f9efa3e29b032b106e53c9a81 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 24 Dec 2017 13:15:30 -0500 Subject: [PATCH 07/12] I am become pretty --- packages/jest-jasmine2/src/jasmine/Suite.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-jasmine2/src/jasmine/Suite.js b/packages/jest-jasmine2/src/jasmine/Suite.js index 02f32af27e0d..db62ea3477b7 100644 --- a/packages/jest-jasmine2/src/jasmine/Suite.js +++ b/packages/jest-jasmine2/src/jasmine/Suite.js @@ -186,8 +186,8 @@ function convertDescriptorToString(descriptor) { return descriptor; } - if (typeof descriptor !== "function") { - throw new Error("describe expects a string, class, or function."); + if (typeof descriptor !== 'function') { + throw new Error('describe expects a string, class, or function.'); } if (descriptor.name !== undefined) { From c4b83438c33f6c6d22eef3336192b218e70c485d Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 24 Dec 2017 13:23:33 -0500 Subject: [PATCH 08/12] Fixed failure message for globals.test.js.snap --- .../__tests__/__snapshots__/globals.test.js.snap | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/integration_tests/__tests__/__snapshots__/globals.test.js.snap b/integration_tests/__tests__/__snapshots__/globals.test.js.snap index a80bea07f6b8..bc75984486d5 100644 --- a/integration_tests/__tests__/__snapshots__/globals.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/globals.test.js.snap @@ -175,9 +175,20 @@ Ran all test suites. `; exports[`array as descriptor 1`] = ` -"FAIL __tests__/function-as-descriptor.test.js +"FAIL __tests__/array-as-descriptor.test.js + ● Test suite failed to run describe expects a string, class, or function. + + 188 | + 189 | if (typeof descriptor !== 'function') { + > 190 | throw new Error('describe expects a string, class, or function.'); + 191 | } + 192 | + 193 | if (descriptor.name !== undefined) { + + at __tests__/array-as-descriptor.test.js:2:5 + " `; From e1f6c7fdc7773081fb0d5fe0a443169b7d1edfb6 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 24 Dec 2017 13:44:04 -0500 Subject: [PATCH 09/12] Removed error test case for array-as-descriptor --- .../__snapshots__/globals.test.js.snap | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/integration_tests/__tests__/__snapshots__/globals.test.js.snap b/integration_tests/__tests__/__snapshots__/globals.test.js.snap index bc75984486d5..809211cb5725 100644 --- a/integration_tests/__tests__/__snapshots__/globals.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/globals.test.js.snap @@ -173,30 +173,3 @@ Time: <> Ran all test suites. " `; - -exports[`array as descriptor 1`] = ` -"FAIL __tests__/array-as-descriptor.test.js - ● Test suite failed to run - -describe expects a string, class, or function. - - 188 | - 189 | if (typeof descriptor !== 'function') { - > 190 | throw new Error('describe expects a string, class, or function.'); - 191 | } - 192 | - 193 | if (descriptor.name !== undefined) { - - at __tests__/array-as-descriptor.test.js:2:5 - -" -`; - -exports[`function as descriptor 2`] = ` -"Test Suites: 0 passed, 0 total -Tests: 0 passed, 0 total -Snapshots: 0 total -Time: <> -Ran all test suites. -" -`; From 9c6b2c52d3d779753e8b76711adc0c3ba66c86f5 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 24 Dec 2017 13:48:42 -0500 Subject: [PATCH 10/12] Removed error test for array-as-descriptor --- integration_tests/__tests__/globals.test.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/integration_tests/__tests__/globals.test.js b/integration_tests/__tests__/globals.test.js index 868ce54b9ca0..6c7c7b88d8db 100644 --- a/integration_tests/__tests__/globals.test.js +++ b/integration_tests/__tests__/globals.test.js @@ -222,20 +222,3 @@ test('function as descriptor', () => { expect(rest).toMatchSnapshot(); expect(summary).toMatchSnapshot(); }); - -test('array as descriptor', () => { - const filename = 'array-as-descriptor.test.js'; - const content = ` - describe([], () => { - it('it', () => {}); - }); - `; - - writeFiles(TEST_DIR, {[filename]: content}); - const {stderr, status} = runJest(DIR); - expect(status).toBe(1); - - const {summary, rest} = extractSummary(stderr); - expect(rest).toMatchSnapshot(); - expect(summary).toMatchSnapshot(); -}); From 0744cf904abc08c5b546849545e2203a9d55960f Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 24 Dec 2017 13:49:28 -0500 Subject: [PATCH 11/12] Added allowance for numbers as test descriptors --- packages/jest-jasmine2/src/jasmine/Suite.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-jasmine2/src/jasmine/Suite.js b/packages/jest-jasmine2/src/jasmine/Suite.js index db62ea3477b7..6263ad5ecbaf 100644 --- a/packages/jest-jasmine2/src/jasmine/Suite.js +++ b/packages/jest-jasmine2/src/jasmine/Suite.js @@ -182,12 +182,12 @@ Suite.prototype.addExpectationResult = function() { }; function convertDescriptorToString(descriptor) { - if (typeof descriptor === 'string' || descriptor === undefined) { + if (typeof descriptor === 'string' || typeof descriptor === 'number' || descriptor === undefined) { return descriptor; } if (typeof descriptor !== 'function') { - throw new Error('describe expects a string, class, or function.'); + throw new Error('describe expects a class, function, number, or string.'); } if (descriptor.name !== undefined) { From e547ef4eea36591fcc9c7aa4fd52ea684fda8137 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 24 Dec 2017 13:55:17 -0500 Subject: [PATCH 12/12] Prettier... --- packages/jest-jasmine2/src/jasmine/Suite.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/jest-jasmine2/src/jasmine/Suite.js b/packages/jest-jasmine2/src/jasmine/Suite.js index 6263ad5ecbaf..9fcd6ba9f353 100644 --- a/packages/jest-jasmine2/src/jasmine/Suite.js +++ b/packages/jest-jasmine2/src/jasmine/Suite.js @@ -182,7 +182,11 @@ Suite.prototype.addExpectationResult = function() { }; function convertDescriptorToString(descriptor) { - if (typeof descriptor === 'string' || typeof descriptor === 'number' || descriptor === undefined) { + if ( + typeof descriptor === 'string' || + typeof descriptor === 'number' || + descriptor === undefined + ) { return descriptor; }