From bd2df3a228a94cd8dfe4030bba43ddf2ab61c530 Mon Sep 17 00:00:00 2001 From: Roman Usherenko Date: Mon, 11 Feb 2019 14:10:57 +0200 Subject: [PATCH] fix: toBeInTheDocument unclear error message on null element (#79) --- .all-contributorsrc | 10 ++++++++++ README.md | 8 ++++---- src/__tests__/to-be-in-the-document.js | 27 ++++++++++++++++++++------ src/to-be-in-the-document.js | 2 +- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index a522155a..629fee00 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -209,6 +209,16 @@ "contributions": [ "bug" ] + }, + { + "login": "dreyks", + "name": "Roman Usherenko", + "avatar_url": "https://avatars3.githubusercontent.com/u/1481264?v=4", + "profile": "https://github.com/dreyks", + "contributions": [ + "code", + "test" + ] } ] } diff --git a/README.md b/README.md index 8519d41d..e4de56f9 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ [![downloads][downloads-badge]][npmtrends] [![MIT License][license-badge]][license] -[![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-21-orange.svg?style=flat-square)](#contributors) [![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc] @@ -746,10 +746,10 @@ Thanks goes to these people ([emoji key][emojis]): -| [
Kent C. Dodds](https://kentcdodds.com)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Documentation") [πŸš‡](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Tests") | [
Ryan Castner](http://audiolion.github.io)
[πŸ“–](https://github.com/gnapse/jest-dom/commits?author=audiolion "Documentation") | [
Daniel Sandiego](https://www.dnlsandiego.com)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=dnlsandiego "Code") | [
PaweΕ‚ MikoΕ‚ajczyk](https://github.com/Miklet)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=Miklet "Code") | [
Alejandro ÑÑñez Ortiz](http://co.linkedin.com/in/alejandronanez/)
[πŸ“–](https://github.com/gnapse/jest-dom/commits?author=alejandronanez "Documentation") | [
Matt Parrish](https://github.com/pbomb)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Apbomb "Bug reports") [πŸ’»](https://github.com/gnapse/jest-dom/commits?author=pbomb "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=pbomb "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=pbomb "Tests") | [
Justin Hall](https://github.com/wKovacs64)
[πŸ“¦](#platform-wKovacs64 "Packaging/porting to new platform") | +| [Kent C. Dodds
Kent C. Dodds](https://kentcdodds.com)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Documentation") [πŸš‡](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Tests") | [Ryan Castner
Ryan Castner](http://audiolion.github.io)
[πŸ“–](https://github.com/gnapse/jest-dom/commits?author=audiolion "Documentation") | [Daniel Sandiego
Daniel Sandiego](https://www.dnlsandiego.com)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=dnlsandiego "Code") | [PaweΕ‚ MikoΕ‚ajczyk
PaweΕ‚ MikoΕ‚ajczyk](https://github.com/Miklet)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=Miklet "Code") | [Alejandro ÑÑñez Ortiz
Alejandro ÑÑñez Ortiz](http://co.linkedin.com/in/alejandronanez/)
[πŸ“–](https://github.com/gnapse/jest-dom/commits?author=alejandronanez "Documentation") | [Matt Parrish
Matt Parrish](https://github.com/pbomb)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Apbomb "Bug reports") [πŸ’»](https://github.com/gnapse/jest-dom/commits?author=pbomb "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=pbomb "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=pbomb "Tests") | [Justin Hall
Justin Hall](https://github.com/wKovacs64)
[πŸ“¦](#platform-wKovacs64 "Packaging/porting to new platform") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | -| [
Anto Aravinth](https://github.com/antoaravinth)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Tests") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Documentation") | [
Jonah Moses](https://github.com/JonahMoses)
[πŸ“–](https://github.com/gnapse/jest-dom/commits?author=JonahMoses "Documentation") | [
Łukasz Gandecki](http://team.thebrain.pro)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Tests") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Documentation") | [
Ivan Babak](https://sompylasar.github.io)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Asompylasar "Bug reports") [πŸ€”](#ideas-sompylasar "Ideas, Planning, & Feedback") | [
Jesse Day](https://github.com/jday3)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=jday3 "Code") | [
Ernesto GarcΓ­a](http://gnapse.github.io)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=gnapse "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=gnapse "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=gnapse "Tests") | [
Mark Volkmann](http://ociweb.com/mark/)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Amvolkmann "Bug reports") [πŸ’»](https://github.com/gnapse/jest-dom/commits?author=mvolkmann "Code") | -| [
smacpherson64](https://github.com/smacpherson64)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Tests") | [
John Gozde](https://github.com/jgoz)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Ajgoz "Bug reports") [πŸ’»](https://github.com/gnapse/jest-dom/commits?author=jgoz "Code") | [
Iwona](https://github.com/callada)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=callada "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=callada "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=callada "Tests") | [
Lewis](https://github.com/6ewis)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=6ewis "Code") | [
Leandro Lourenci](https://blog.lourenci.com/)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Alourenci "Bug reports") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=lourenci "Documentation") [πŸ’»](https://github.com/gnapse/jest-dom/commits?author=lourenci "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=lourenci "Tests") | [
Shukhrat Mukimov](https://github.com/mufasa71)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Amufasa71 "Bug reports") | +| [Anto Aravinth
Anto Aravinth](https://github.com/antoaravinth)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Tests") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Documentation") | [Jonah Moses
Jonah Moses](https://github.com/JonahMoses)
[πŸ“–](https://github.com/gnapse/jest-dom/commits?author=JonahMoses "Documentation") | [Łukasz Gandecki
Łukasz Gandecki](http://team.thebrain.pro)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Tests") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Documentation") | [Ivan Babak
Ivan Babak](https://sompylasar.github.io)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Asompylasar "Bug reports") [πŸ€”](#ideas-sompylasar "Ideas, Planning, & Feedback") | [Jesse Day
Jesse Day](https://github.com/jday3)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=jday3 "Code") | [Ernesto GarcΓ­a
Ernesto GarcΓ­a](http://gnapse.github.io)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=gnapse "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=gnapse "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=gnapse "Tests") | [Mark Volkmann
Mark Volkmann](http://ociweb.com/mark/)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Amvolkmann "Bug reports") [πŸ’»](https://github.com/gnapse/jest-dom/commits?author=mvolkmann "Code") | +| [smacpherson64
smacpherson64](https://github.com/smacpherson64)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Tests") | [John Gozde
John Gozde](https://github.com/jgoz)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Ajgoz "Bug reports") [πŸ’»](https://github.com/gnapse/jest-dom/commits?author=jgoz "Code") | [Iwona
Iwona](https://github.com/callada)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=callada "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=callada "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=callada "Tests") | [Lewis
Lewis](https://github.com/6ewis)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=6ewis "Code") | [Leandro Lourenci
Leandro Lourenci](https://blog.lourenci.com/)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Alourenci "Bug reports") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=lourenci "Documentation") [πŸ’»](https://github.com/gnapse/jest-dom/commits?author=lourenci "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=lourenci "Tests") | [Shukhrat Mukimov
Shukhrat Mukimov](https://github.com/mufasa71)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Amufasa71 "Bug reports") | [Roman Usherenko
Roman Usherenko](https://github.com/dreyks)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=dreyks "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=dreyks "Tests") | diff --git a/src/__tests__/to-be-in-the-document.js b/src/__tests__/to-be-in-the-document.js index e1a47c91..d3a5488c 100644 --- a/src/__tests__/to-be-in-the-document.js +++ b/src/__tests__/to-be-in-the-document.js @@ -1,4 +1,5 @@ import document from './helpers/document' +import {HtmlElementTypeError} from '../utils' test('.toBeInTheDocument', () => { document.body.innerHTML = ` @@ -18,10 +19,24 @@ test('.toBeInTheDocument', () => { expect(nullElement).not.toBeInTheDocument() // negative test cases wrapped in throwError assertions for coverage. - expect(() => expect(htmlElement).not.toBeInTheDocument()).toThrowError() - expect(() => expect(svgElement).not.toBeInTheDocument()).toThrowError() - expect(() => expect(detachedElement).toBeInTheDocument()).toThrowError() - expect(() => expect(fakeElement).toBeInTheDocument()).toThrowError() - expect(() => expect(undefinedElement).toBeInTheDocument()).toThrowError() - expect(() => expect(nullElement).toBeInTheDocument()).toThrowError() + const expectToBe = /expect.*\.toBeInTheDocument/ + const expectNotToBe = /expect.*not\.toBeInTheDocument/ + expect(() => expect(htmlElement).not.toBeInTheDocument()).toThrowError( + expectNotToBe, + ) + expect(() => expect(svgElement).not.toBeInTheDocument()).toThrowError( + expectNotToBe, + ) + expect(() => expect(detachedElement).toBeInTheDocument()).toThrowError( + expectToBe, + ) + expect(() => expect(fakeElement).toBeInTheDocument()).toThrowError( + HtmlElementTypeError, + ) + expect(() => expect(undefinedElement).toBeInTheDocument()).toThrowError( + HtmlElementTypeError, + ) + expect(() => expect(nullElement).toBeInTheDocument()).toThrowError( + HtmlElementTypeError, + ) }) diff --git a/src/to-be-in-the-document.js b/src/to-be-in-the-document.js index 9f0320a2..3a9c323a 100644 --- a/src/to-be-in-the-document.js +++ b/src/to-be-in-the-document.js @@ -6,7 +6,7 @@ import { import {checkHtmlElement} from './utils' export function toBeInTheDocument(element) { - if (element !== null) { + if (element !== null || !this.isNot) { checkHtmlElement(element, toBeInTheDocument, this) }