-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn if describe returns a value (#7852)
* Fail test suite if describe returns a Promise * copyright header * fix for overwritten global.Promise * fix e2e test for Node 6 * only console.warn for now * stack trace * remove circus stack entry * snapshot test * console.warn => console.log * remove Env.js after rebase * fix jasmine2 after rebase
- Loading branch information
Showing
11 changed files
with
218 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
e2e/__tests__/__snapshots__/declarationErrors.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`warns if describe returns a Promise 1`] = ` | ||
" console.log | ||
● Test suite failed to run | ||
Returning a Promise from \\"describe\\" is not supported. Tests must be defined synchronously. | ||
Returning a value from \\"describe\\" will fail the test in a future version of Jest. | ||
9 | 'use strict'; | ||
10 | | ||
> 11 | describe('Promise describe warns', () => { | ||
| ^ | ||
12 | it('t', () => {}); | ||
13 | return Promise.resolve(); | ||
14 | }); | ||
at Object.describe (__tests__/describeReturnPromise.test.js:11:1) | ||
" | ||
`; | ||
|
||
exports[`warns if describe returns something 1`] = ` | ||
" console.log | ||
● Test suite failed to run | ||
A \\"describe\\" callback must not return a value. | ||
Returning a value from \\"describe\\" will fail the test in a future version of Jest. | ||
9 | 'use strict'; | ||
10 | | ||
> 11 | describe('describe return warns', () => { | ||
| ^ | ||
12 | it('t', () => {}); | ||
13 | return 42; | ||
14 | }); | ||
at Object.describe (__tests__/describeReturnSomething.test.js:11:1) | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 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'; | ||
|
||
const normalizeCircusJasmine = (str: string) => | ||
str | ||
.replace(/console\.log .+:\d+/, 'console.log') | ||
.replace(/.+addSpecsToSuite (.+:\d+:\d+).+\n/, ''); | ||
|
||
it('warns if describe returns a Promise', () => { | ||
const result = runJest('declaration-errors', [ | ||
'describeReturnPromise.test.js', | ||
]); | ||
|
||
expect(result.status).toBe(0); | ||
expect(normalizeCircusJasmine(result.stdout)).toMatchSnapshot(); | ||
}); | ||
|
||
it('warns if describe returns something', () => { | ||
const result = runJest('declaration-errors', [ | ||
'describeReturnSomething.test.js', | ||
]); | ||
|
||
expect(result.status).toBe(0); | ||
expect(normalizeCircusJasmine(result.stdout)).toMatchSnapshot(); | ||
}); |
14 changes: 14 additions & 0 deletions
14
e2e/declaration-errors/__tests__/describeReturnPromise.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); |
14 changes: 14 additions & 0 deletions
14
e2e/declaration-errors/__tests__/describeReturnSomething.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* 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 isPromise from '../isPromise'; | ||
|
||
describe('not a Promise: ', () => { | ||
test.each([undefined, null, true, 42, '1337', Symbol(), [], {}])( | ||
'%p', | ||
value => { | ||
expect(isPromise(value)).toBe(false); | ||
}, | ||
); | ||
}); | ||
|
||
test('a resolved Promise', () => { | ||
expect(isPromise(Promise.resolve(42))).toBe(true); | ||
}); | ||
|
||
test('a rejected Promise', () => { | ||
expect(isPromise(Promise.reject().catch(() => {}))).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
*/ | ||
|
||
// capture global.Promise before it may potentially be overwritten | ||
const Promise: any = global.Promise; | ||
|
||
// see ES2015 spec 25.4.4.5, https://stackoverflow.com/a/38339199 | ||
const isPromise = (candidate: unknown): candidate is Promise<unknown> => | ||
Promise.resolve(candidate) === candidate; | ||
export default isPromise; |