diff --git a/.all-contributorsrc b/.all-contributorsrc
index 2a1e3a19..6b0ccf55 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -238,6 +238,15 @@
"bug",
"code"
]
+ },
+ {
+ "login": "revathskumar",
+ "name": "Revath S Kumar",
+ "avatar_url": "https://avatars3.githubusercontent.com/u/463904?v=4",
+ "profile": "https://blog.revathskumar.com",
+ "contributions": [
+ "code"
+ ]
}
]
}
diff --git a/README.md b/README.md
index 0ee33c31..26226363 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-23-orange.svg?style=flat-square)](#contributors)
+[![All Contributors](https://img.shields.io/badge/all_contributors-24-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Code of Conduct][coc-badge]][coc]
@@ -381,11 +381,11 @@ expect(getByTestId(container, 'parent')).toContainHTML(
### `toHaveAttribute`
```typescript
-toHaveAttribute(attr: string, value?: string)
+toHaveAttribute(attr: string, value?: any)
```
This allows you to check whether the given element has an attribute or not. You
-can also optionally check that the attribute has a specific expected value.
+can also optionally check that the attribute has a specific expected value or partial match using [expect.stringContaining](https://jestjs.io/docs/en/expect.html#expectnotstringcontainingstring)/[expect.stringMatching](https://jestjs.io/docs/en/expect.html#expectstringmatchingstring-regexp)
#### Examples
@@ -411,6 +411,9 @@ const button = getByTestId(container, 'ok-button')
expect(button).toHaveAttribute('disabled')
expect(button).toHaveAttribute('type', 'submit')
expect(button).not.toHaveAttribute('type', 'button')
+
+expect(button).toHaveAttribute('type', expect.stringContaining('sub'))
+expect(button).toHaveAttribute('type', expect.not.stringContaining('but'))
```
@@ -760,8 +763,7 @@ Thanks goes to these people ([emoji key][emojis]):
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [
Anto Aravinth](https://github.com/antoaravinth)
[π»](https://github.com/testing-library/jest-dom/commits?author=antoaravinth "Code") [β οΈ](https://github.com/testing-library/jest-dom/commits?author=antoaravinth "Tests") [π](https://github.com/testing-library/jest-dom/commits?author=antoaravinth "Documentation") | [
Jonah Moses](https://github.com/JonahMoses)
[π](https://github.com/testing-library/jest-dom/commits?author=JonahMoses "Documentation") | [
Εukasz Gandecki](http://team.thebrain.pro)
[π»](https://github.com/testing-library/jest-dom/commits?author=lgandecki "Code") [β οΈ](https://github.com/testing-library/jest-dom/commits?author=lgandecki "Tests") [π](https://github.com/testing-library/jest-dom/commits?author=lgandecki "Documentation") | [
Ivan Babak](https://sompylasar.github.io)
[π](https://github.com/testing-library/jest-dom/issues?q=author%3Asompylasar "Bug reports") [π€](#ideas-sompylasar "Ideas, Planning, & Feedback") | [
Jesse Day](https://github.com/jday3)
[π»](https://github.com/testing-library/jest-dom/commits?author=jday3 "Code") | [
Ernesto GarcΓa](http://gnapse.github.io)
[π»](https://github.com/testing-library/jest-dom/commits?author=gnapse "Code") [π](https://github.com/testing-library/jest-dom/commits?author=gnapse "Documentation") [β οΈ](https://github.com/testing-library/jest-dom/commits?author=gnapse "Tests") | [
Mark Volkmann](http://ociweb.com/mark/)
[π](https://github.com/testing-library/jest-dom/issues?q=author%3Amvolkmann "Bug reports") [π»](https://github.com/testing-library/jest-dom/commits?author=mvolkmann "Code") |
| [
smacpherson64](https://github.com/smacpherson64)
[π»](https://github.com/testing-library/jest-dom/commits?author=smacpherson64 "Code") [π](https://github.com/testing-library/jest-dom/commits?author=smacpherson64 "Documentation") [β οΈ](https://github.com/testing-library/jest-dom/commits?author=smacpherson64 "Tests") | [
John Gozde](https://github.com/jgoz)
[π](https://github.com/testing-library/jest-dom/issues?q=author%3Ajgoz "Bug reports") [π»](https://github.com/testing-library/jest-dom/commits?author=jgoz "Code") | [
Iwona](https://github.com/callada)
[π»](https://github.com/testing-library/jest-dom/commits?author=callada "Code") [π](https://github.com/testing-library/jest-dom/commits?author=callada "Documentation") [β οΈ](https://github.com/testing-library/jest-dom/commits?author=callada "Tests") | [
Lewis](https://github.com/6ewis)
[π»](https://github.com/testing-library/jest-dom/commits?author=6ewis "Code") | [
Leandro Lourenci](https://blog.lourenci.com/)
[π](https://github.com/testing-library/jest-dom/issues?q=author%3Alourenci "Bug reports") [π](https://github.com/testing-library/jest-dom/commits?author=lourenci "Documentation") [π»](https://github.com/testing-library/jest-dom/commits?author=lourenci "Code") [β οΈ](https://github.com/testing-library/jest-dom/commits?author=lourenci "Tests") | [
Shukhrat Mukimov](https://github.com/mufasa71)
[π](https://github.com/testing-library/jest-dom/issues?q=author%3Amufasa71 "Bug reports") | [
Roman Usherenko](https://github.com/dreyks)
[π»](https://github.com/testing-library/jest-dom/commits?author=dreyks "Code") [β οΈ](https://github.com/testing-library/jest-dom/commits?author=dreyks "Tests") |
-| [
Joe Hsu](http://josephhsu.com)
[π](https://github.com/testing-library/jest-dom/commits?author=jhsu "Documentation") | [
Haz](https://twitter.com/diegohaz)
[π](https://github.com/testing-library/jest-dom/issues?q=author%3Adiegohaz "Bug reports") [π»](https://github.com/testing-library/jest-dom/commits?author=diegohaz "Code") |
-
+| [
Joe Hsu](http://josephhsu.com)
[π](https://github.com/testing-library/jest-dom/commits?author=jhsu "Documentation") | [
Haz](https://twitter.com/diegohaz)
[π](https://github.com/testing-library/jest-dom/issues?q=author%3Adiegohaz "Bug reports") [π»](https://github.com/testing-library/jest-dom/commits?author=diegohaz "Code") | [
Revath S Kumar](https://blog.revathskumar.com)
[π»](https://github.com/testing-library/jest-dom/commits?author=revathskumar "Code") |
This project follows the [all-contributors][all-contributors] specification.
diff --git a/extend-expect.d.ts b/extend-expect.d.ts
index 5115b2a5..a65bf554 100644
--- a/extend-expect.d.ts
+++ b/extend-expect.d.ts
@@ -1,4 +1,24 @@
declare namespace jest {
+ interface InverseStringAsymmetricMatchers {
+ stringMatching(str: string | RegExp): any
+ stringContaining(str: string): any
+ }
+ interface Expect {
+ stringMatching(str: string | RegExp): any
+ stringContaining(str: string): any
+ anything(): any
+ any(classType: any): any
+ not: InverseStringAsymmetricMatchers
+ }
+
+ type attributeValueType =
+ | string
+ | Expect.stringContaining
+ | Expect.stringMatching
+ | Expect.any
+ | Expect.anything
+ | Expect.not
+
interface Matchers {
/**
* @deprecated
@@ -11,7 +31,7 @@ declare namespace jest {
toBeEnabled(): R
toContainElement(element: HTMLElement | SVGElement | null): R
toContainHTML(htmlText: string): R
- toHaveAttribute(attr: string, value?: string): R
+ toHaveAttribute(attr: string, value?: attributeValueType): R
toHaveClass(...classNames: string[]): R
toHaveFocus(): R
toHaveFormValues(expectedValues: {[name: string]: any}): R
diff --git a/package.json b/package.json
index f8f704e0..ef5f1162 100644
--- a/package.json
+++ b/package.json
@@ -36,6 +36,7 @@
"chalk": "^2.4.1",
"css": "^2.2.3",
"css.escape": "^1.5.1",
+ "expect": "^24.7.1",
"jest-diff": "^24.0.0",
"jest-matcher-utils": "^24.0.0",
"lodash": "^4.17.11",
diff --git a/src/__tests__/to-have-attribute.js b/src/__tests__/to-have-attribute.js
index 8110b0c7..0530f3a6 100644
--- a/src/__tests__/to-have-attribute.js
+++ b/src/__tests__/to-have-attribute.js
@@ -41,4 +41,22 @@ test('.toHaveAttribute', () => {
expect(() =>
expect({thisIsNot: 'an html element'}).not.toHaveAttribute(),
).toThrowError()
+
+ // Asymettic matchers
+ expect(queryByTestId('ok-button')).toHaveAttribute(
+ 'type',
+ expect.stringContaining('sub'),
+ )
+ expect(queryByTestId('ok-button')).toHaveAttribute(
+ 'type',
+ expect.stringMatching(/sub*/),
+ )
+ expect(queryByTestId('ok-button')).toHaveAttribute('type', expect.anything())
+
+ expect(() =>
+ expect(queryByTestId('ok-button')).toHaveAttribute(
+ 'type',
+ expect.not.stringContaining('sub'),
+ ),
+ ).toThrowError()
})
diff --git a/src/to-have-attribute.js b/src/to-have-attribute.js
index 0dbba11f..0627cf35 100644
--- a/src/to-have-attribute.js
+++ b/src/to-have-attribute.js
@@ -1,4 +1,5 @@
import {matcherHint, stringify, printExpected} from 'jest-matcher-utils'
+import {equals} from 'expect/build/jasmineUtils'
import {checkHtmlElement, getMessage} from './utils'
function printAttribute(name, value) {
@@ -18,7 +19,7 @@ export function toHaveAttribute(htmlElement, name, expectedValue) {
const receivedValue = htmlElement.getAttribute(name)
return {
pass: isExpectedValuePresent
- ? hasAttribute && receivedValue === expectedValue
+ ? hasAttribute && equals(receivedValue, expectedValue)
: hasAttribute,
message: () => {
const to = this.isNot ? 'not to' : 'to'