Skip to content

Commit

Permalink
fix: fixed .not.toBeInTheConsole matcher usage
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Dec 13, 2021
1 parent 02120e2 commit b34b0b0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
20 changes: 19 additions & 1 deletion src/__tests__/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ test('toBeInTheConsole should fail when something is not console', async () => {
expect(() => expect(queryByText('NotHere')).toBeInTheConsole()).toThrow(/value must be a TestInstance/)
})

test('not.toBeInTheConsole should pass something is not console', async () => {
const {queryByText} = await render('node', [
resolve(__dirname, './execute-scripts/list-args.js'),
'--version',
])

expect(() => expect(queryByText('NotHere')).not.toBeInTheConsole()).not.toThrow()
})

test('not.toBeInTheConsole should fail something is console', async () => {
const {queryByText} = await render('node', [
resolve(__dirname, './execute-scripts/list-args.js'),
'--version',
])

expect(() => expect(queryByText('--version')).not.toBeInTheConsole()).toThrow(/Expected not to find the instance in the console/)
})

test('toHaveErrorMessage should pass during stderr when no string passed', async () => {
const instance = await render('node', [
resolve(__dirname, './execute-scripts/throw.js'),
Expand Down Expand Up @@ -48,7 +66,7 @@ test('toHaveErrorMessage should fail when something is not in stderr', async ()
])

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

test('toHaveErrorMessage should fail when null is passed', async () => {
Expand Down
25 changes: 21 additions & 4 deletions src/matchers/to-be-in-the-console.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
/* eslint-disable @babel/no-invalid-this */
import {checkCliInstance} from "./utils";
import {getDefaultNormalizer} from "../matches";
import {checkCliInstance, getMessage} from "./utils";

export function toBeInTheConsole(instance) {
if (instance !== null || !this.isNot) {
checkCliInstance(instance, toBeInTheConsole, this)
}

// Assuming it passed above, it must have passed
const errormessage = instance ? getDefaultNormalizer()(instance.stderrArr.join('\n')) : null

return {
pass: true,
message: () => []
// Does not change based on `.not`, and as a result, we must confirm if it _actually_ is there
pass: !!instance,
message: () => {
const to = this.isNot ? 'not to' : 'to'
return getMessage(
this,
this.utils.matcherHint(
`${this.isNot ? '.not' : ''}.toBeInTheConsole`,
'instance',
'',
),
`Expected ${to} find the instance in the console`,
"",
'Received',
this.utils.printReceived(errormessage),
)
}
}
}
4 changes: 2 additions & 2 deletions src/matchers/to-have-errormessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export function toHaveErrorMessage(testInstance, checkWith) {
this,
this.utils.matcherHint(
`${this.isNot ? '.not' : ''}.toHaveErrorMessage`,
'element',
'instance',
'',
),
`Expected the element ${to} have error message`,
`Expected the instance ${to} have error message`,
this.utils.printExpected(checkWith),
'Received',
this.utils.printReceived(errormessage),
Expand Down

0 comments on commit b34b0b0

Please sign in to comment.