Skip to content

Commit b34b0b0

Browse files
committed
fix: fixed .not.toBeInTheConsole matcher usage
1 parent 02120e2 commit b34b0b0

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/__tests__/matchers.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ test('toBeInTheConsole should fail when something is not console', async () => {
2121
expect(() => expect(queryByText('NotHere')).toBeInTheConsole()).toThrow(/value must be a TestInstance/)
2222
})
2323

24+
test('not.toBeInTheConsole should pass something is not console', async () => {
25+
const {queryByText} = await render('node', [
26+
resolve(__dirname, './execute-scripts/list-args.js'),
27+
'--version',
28+
])
29+
30+
expect(() => expect(queryByText('NotHere')).not.toBeInTheConsole()).not.toThrow()
31+
})
32+
33+
test('not.toBeInTheConsole should fail something is console', async () => {
34+
const {queryByText} = await render('node', [
35+
resolve(__dirname, './execute-scripts/list-args.js'),
36+
'--version',
37+
])
38+
39+
expect(() => expect(queryByText('--version')).not.toBeInTheConsole()).toThrow(/Expected not to find the instance in the console/)
40+
})
41+
2442
test('toHaveErrorMessage should pass during stderr when no string passed', async () => {
2543
const instance = await render('node', [
2644
resolve(__dirname, './execute-scripts/throw.js'),
@@ -48,7 +66,7 @@ test('toHaveErrorMessage should fail when something is not in stderr', async ()
4866
])
4967

5068
const instance = await findByText('--version')
51-
expect(() => expect(instance).toHaveErrorMessage("Error isn't here")).toThrow(/Expected the element to have error message/)
69+
expect(() => expect(instance).toHaveErrorMessage("Error isn't here")).toThrow(/Expected the instance to have error message/)
5270
})
5371

5472
test('toHaveErrorMessage should fail when null is passed', async () => {

src/matchers/to-be-in-the-console.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
/* eslint-disable @babel/no-invalid-this */
2-
import {checkCliInstance} from "./utils";
2+
import {getDefaultNormalizer} from "../matches";
3+
import {checkCliInstance, getMessage} from "./utils";
34

45
export function toBeInTheConsole(instance) {
56
if (instance !== null || !this.isNot) {
67
checkCliInstance(instance, toBeInTheConsole, this)
78
}
89

9-
// Assuming it passed above, it must have passed
10+
const errormessage = instance ? getDefaultNormalizer()(instance.stderrArr.join('\n')) : null
11+
1012
return {
11-
pass: true,
12-
message: () => []
13+
// Does not change based on `.not`, and as a result, we must confirm if it _actually_ is there
14+
pass: !!instance,
15+
message: () => {
16+
const to = this.isNot ? 'not to' : 'to'
17+
return getMessage(
18+
this,
19+
this.utils.matcherHint(
20+
`${this.isNot ? '.not' : ''}.toBeInTheConsole`,
21+
'instance',
22+
'',
23+
),
24+
`Expected ${to} find the instance in the console`,
25+
"",
26+
'Received',
27+
this.utils.printReceived(errormessage),
28+
)
29+
}
1330
}
1431
}

src/matchers/to-have-errormessage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export function toHaveErrorMessage(testInstance, checkWith) {
2525
this,
2626
this.utils.matcherHint(
2727
`${this.isNot ? '.not' : ''}.toHaveErrorMessage`,
28-
'element',
28+
'instance',
2929
'',
3030
),
31-
`Expected the element ${to} have error message`,
31+
`Expected the instance ${to} have error message`,
3232
this.utils.printExpected(checkWith),
3333
'Received',
3434
this.utils.printReceived(errormessage),

0 commit comments

Comments
 (0)