diff --git a/CHANGELOG.md b/CHANGELOG.md index fed2c012e488..14be7779741c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ - `[jest-config]` Print error information on preset normalization error ([#7935](https://github.com/facebook/jest/pull/7935)) - `[jest-haste-map]` Add `skipPackageJson` option ([#7778](https://github.com/facebook/jest/pull/7778)) - `[jest-get-type]` Add `isPrimitive` function ([#7708](https://github.com/facebook/jest/pull/7708)) -- `[jest-circus/jest-jasmine2]` Fail test suite if describe returns a Promise ([#7852](https://github.com/facebook/jest/pull/7852)) +- `[jest-circus/jest-jasmine2]` Warn if describe returns a value ([#7852](https://github.com/facebook/jest/pull/7852)) - `[jest-util]` Add `isPromise` ([#7852](https://github.com/facebook/jest/pull/7852)) ### Fixes diff --git a/e2e/__tests__/declarationErrors.test.ts b/e2e/__tests__/declarationErrors.test.ts new file mode 100644 index 000000000000..5aa52099d538 --- /dev/null +++ b/e2e/__tests__/declarationErrors.test.ts @@ -0,0 +1,26 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import runJest from '../runJest'; + +it('warns if describe returns a Promise', () => { + const result = runJest('declaration-errors', [ + 'describeReturnPromise.test.js', + ]); + + expect(result.status).toBe(0); + expect(result.stdout).toMatch(/Tests must be defined synchronously/); +}); + +it('warns if describe returns something', () => { + const result = runJest('declaration-errors', [ + 'describeReturnSomething.test.js', + ]); + + expect(result.status).toBe(0); + expect(result.stdout).toMatch(/"describe" callback must not return a value/); +}); diff --git a/e2e/__tests__/jasmineAsync.test.ts b/e2e/__tests__/jasmineAsync.test.ts index 32cc2e73552d..28b4aa827d0d 100644 --- a/e2e/__tests__/jasmineAsync.test.ts +++ b/e2e/__tests__/jasmineAsync.test.ts @@ -83,13 +83,6 @@ describe('async jasmine', () => { expect(json.numPendingTests).toBe(1); }); - it('fails if describe returns a Promise', () => { - const result = runJest('jasmine-async', ['promiseDescribeFails.test.js']); - - expect(result.status).toBe(1); - expect(result.stderr).toMatch(/Tests must be defined synchronously/); - }); - it('throws when not a promise is returned', () => { const result = runWithJson('jasmine-async', ['returningValues.test.js']); const json = result.json; diff --git a/e2e/declaration-errors/__tests__/describeReturnPromise.test.js b/e2e/declaration-errors/__tests__/describeReturnPromise.test.js new file mode 100644 index 000000000000..f40120ab34a1 --- /dev/null +++ b/e2e/declaration-errors/__tests__/describeReturnPromise.test.js @@ -0,0 +1,14 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + +describe('Promise describe warns', () => { + it('t', () => {}); + return Promise.resolve(); +}); diff --git a/e2e/declaration-errors/__tests__/describeReturnSomething.test.js b/e2e/declaration-errors/__tests__/describeReturnSomething.test.js new file mode 100644 index 000000000000..d1e5e158ae52 --- /dev/null +++ b/e2e/declaration-errors/__tests__/describeReturnSomething.test.js @@ -0,0 +1,14 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + +describe('describe return warns', () => { + it('t', () => {}); + return 42; +}); diff --git a/e2e/declaration-errors/package.json b/e2e/declaration-errors/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/declaration-errors/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/e2e/jasmine-async/__tests__/promiseDescribeFails.test.js b/e2e/jasmine-async/__tests__/promiseDescribeFails.test.js deleted file mode 100644 index f9703543a858..000000000000 --- a/e2e/jasmine-async/__tests__/promiseDescribeFails.test.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - */ - -'use strict'; - -// TODO after dropping Node 6: Convert to async-await -// describe('Promise describe fails', async () => { -// await Promise.resolve(); -// it('not declared', () => {}); -// }); - -describe('Promise describe fails', () => - Promise.resolve().then(() => { - it('not declared', () => {}); - })); diff --git a/packages/jest-circus/src/index.ts b/packages/jest-circus/src/index.ts index 9a111cd29645..eaf0dd64d10a 100644 --- a/packages/jest-circus/src/index.ts +++ b/packages/jest-circus/src/index.ts @@ -64,12 +64,20 @@ const _dispatchDescribe = ( name: 'start_describe_definition', }); const describeReturn = blockFn(); + + // TODO throw in Jest 25 if (isPromise(describeReturn)) { - throw new ErrorWithStack( - 'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.', - describeFn, + console.warn( + 'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.\n' + + 'Returning a value from "describe" will fail the test in a future version of Jest.', + ); + } else if (describeReturn !== undefined) { + console.warn( + 'A "describe" callback must not return a value.\n' + + 'Returning a value from "describe" will fail the test in a future version of Jest.', ); } + dispatch({blockName, mode, name: 'finish_describe_definition'}); }; diff --git a/packages/jest-jasmine2/src/jasmine/Env.js b/packages/jest-jasmine2/src/jasmine/Env.js index fe9728418c54..bf48b0346870 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.js +++ b/packages/jest-jasmine2/src/jasmine/Env.js @@ -376,9 +376,16 @@ export default function(j$) { declarationError = e; } + // TODO throw in Jest 25: declarationError = new Error if (isPromise(describeReturnValue)) { - declarationError = new Error( - 'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.', + console.warn( + 'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.\n' + + 'Returning a value from "describe" will fail the test in a future version of Jest.', + ); + } else if (describeReturnValue !== undefined) { + console.warn( + 'A "describe" callback must not return a value.\n' + + 'Returning a value from "describe" will fail the test in a future version of Jest.', ); } diff --git a/yarn.lock b/yarn.lock index ea115684f057..3462a92ca17b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1784,14 +1784,6 @@ "@types/prop-types" "*" csstype "^2.2.0" -"@types/readable-stream@^2.3.0": - version "2.3.1" - resolved "https://registry.yarnpkg.com/@types/readable-stream/-/readable-stream-2.3.1.tgz#59d458b51c84c585caea06e296e2225057c9ea8e" - integrity sha512-Dp6t95yGEOm2y669mQrSl0kUg+oL+bJEiCWMyDv0Yq+FcVvjzNRLTAqJks2LDBYYrazZXNI7lZXq3lp7MOvt4A== - dependencies: - "@types/node" "*" - safe-buffer "*" - "@types/resolve@*": version "0.0.8" resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-0.0.8.tgz#f26074d238e02659e323ce1a13d041eee280e194" @@ -11516,7 +11508,7 @@ rxjs@^6.4.0: dependencies: tslib "^1.9.0" -safe-buffer@*, safe-buffer@5.1.2, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: +safe-buffer@5.1.2, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: version "5.1.2" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==