Skip to content

Commit fd2df8d

Browse files
antsmartianKent C. Dodds
authored and
Kent C. Dodds
committed
fix(jest-matchers): message should be function and added test cases for the coverage as well (#32)
1 parent 6f01eeb commit fd2df8d

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/__tests__/element-queries.js

+14
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ test('using jest helpers to assert element states', () => {
9595
expect(() =>
9696
expect(queryByTestId('count-value2')).toHaveTextContent('2'),
9797
).toThrowError()
98+
99+
// negative test cases wrapped in throwError assertions for coverage.
100+
expect(() =>
101+
expect(queryByTestId('count-value')).not.toBeInTheDOM(),
102+
).toThrowError()
103+
expect(() =>
104+
expect(queryByTestId('count-value1')).toBeInTheDOM(),
105+
).toThrowError()
106+
expect(() =>
107+
expect(queryByTestId('count-value')).toHaveTextContent('3'),
108+
).toThrowError()
109+
expect(() =>
110+
expect(queryByTestId('count-value')).not.toHaveTextContent('2'),
111+
).toThrowError()
98112
})
99113

100114
/* eslint jsx-a11y/label-has-for:0 */

src/jest-extensions.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const extensions = {
1818
getDisplayName(received)
1919
if (received) {
2020
return {
21-
message:
21+
message: () =>
2222
`${matcherHint(
2323
'.not.toBeInTheDOM',
2424
'received',
@@ -29,7 +29,7 @@ const extensions = {
2929
}
3030
} else {
3131
return {
32-
message:
32+
message: () =>
3333
`${matcherHint(
3434
'.toBeInTheDOM',
3535
'received',
@@ -53,22 +53,24 @@ const extensions = {
5353
const pass = matches(textContent, htmlElement, checkWith)
5454
if (pass) {
5555
return {
56-
message: assertMessage(
57-
'.not.toHaveTextContent',
58-
'Expected value not equals to',
59-
htmlElement,
60-
checkWith,
61-
),
56+
message: () =>
57+
assertMessage(
58+
'.not.toHaveTextContent',
59+
'Expected value not equals to',
60+
htmlElement,
61+
checkWith,
62+
),
6263
pass: true,
6364
}
6465
} else {
6566
return {
66-
message: assertMessage(
67-
'.toHaveTextContent',
68-
'Expected value equals to',
69-
htmlElement,
70-
checkWith,
71-
),
67+
message: () =>
68+
assertMessage(
69+
'.toHaveTextContent',
70+
'Expected value equals to',
71+
htmlElement,
72+
checkWith,
73+
),
7274
pass: false,
7375
}
7476
}

0 commit comments

Comments
 (0)