TypeScript Team | 64a6173 | 2018-02-05 20:36:51 +0100 | [diff] [blame] | 1 | <!-- FIXME(alexeagle): generate the docs from the sources --> |
| 2 | |
| 3 | ## Don't expect promises toBeTruthy() |
| 4 | |
| 5 | Don't write expectations like this: |
| 6 | |
| 7 | expect(returnsPromise()).toBeTruthy(); |
| 8 | |
| 9 | Promises are always truthy, so this assertion will never fail. Usually, the |
| 10 | intention was to match the result of the promise. If that's the case, simply |
| 11 | add an `await`. |
| 12 | |
| 13 | expect(await returnsPromise()).toBeTruthy(); |
| 14 | |
| 15 | ### In Protractor tests |
| 16 | |
| 17 | If you're not writing a Protractor test, you can safely ignore this section. |
| 18 | |
| 19 | In the past, Protractor tests have patched `expect()` to automatically unwrap |
| 20 | promises, which made these assertions work as expected without needing an |
| 21 | `await`. However, the [control flow is deprecated][1] and will be removed in |
| 22 | Selenium WebDriver 4.0, and these assertions are now a bug. You'll |
| 23 | need to `await` the promise as above. |
| 24 | |
| 25 | If you can't `await` the promise because your tests need the control flow, you |
| 26 | can use a more specific matcher. |
| 27 | |
| 28 | expect(returnsPromise()).toBe(true); |
| 29 | |
| 30 | This assertion will work as expected for now and will fail when the control flow |
| 31 | is finally removed. |
| 32 | |
| 33 | [1]: https://github.com/SeleniumHQ/selenium/issues/2969 |