Skip to content

Commit

Permalink
only console.warn for now
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Mar 2, 2019
1 parent 5f5fd06 commit 9831585
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions 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/);
});
7 changes: 0 additions & 7 deletions e2e/__tests__/jasmineAsync.test.ts
Expand Up @@ -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;
Expand Down
Expand Up @@ -9,12 +9,12 @@
'use strict';

// TODO after dropping Node 6: Convert to async-await
// describe('Promise describe fails', async () => {
// describe('Promise describe warns', async () => {
// it('t', () => {});
// await Promise.resolve();
// it('not declared', () => {});
// });

describe('Promise describe fails', () =>
Promise.resolve().then(() => {
it('not declared', () => {});
}));
describe('Promise describe warns', () => {
it('t', () => {});
return Promise.resolve();
});
14 changes: 14 additions & 0 deletions 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;
});
5 changes: 5 additions & 0 deletions e2e/declaration-errors/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
14 changes: 11 additions & 3 deletions packages/jest-circus/src/index.ts
Expand Up @@ -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'});
};

Expand Down
11 changes: 9 additions & 2 deletions packages/jest-jasmine2/src/jasmine/Env.js
Expand Up @@ -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.',
);
}

Expand Down
10 changes: 1 addition & 9 deletions yarn.lock
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 9831585

Please sign in to comment.