Skip to content

Commit 2df64e9

Browse files
committed
chore: add in "toBeInTheConsole" matcher
Closes #4
1 parent de043f6 commit 2df64e9

File tree

8 files changed

+65
-0
lines changed

8 files changed

+65
-0
lines changed

extend-expect.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line
2+
require('./dist/extend-expect')

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@babel/code-frame": "^7.10.4",
4141
"@babel/runtime": "^7.12.5",
4242
"chalk": "^4.1.0",
43+
"jest-matcher-utils": "^27.4.2",
4344
"lz-string": "^1.4.4",
4445
"pretty-format": "^27.0.2",
4546
"strip-ansi": "^6.0.1",

src/__tests__/matchers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const {resolve} = require('path')
2+
const {render} = require('../pure')
3+
4+
test('Should handle argument passing', async () => {
5+
const {findByText} = await render('node', [
6+
resolve(__dirname, './execute-scripts/list-args.js'),
7+
'--version',
8+
])
9+
10+
expect(await findByText('--version')).toBeInTheConsole()
11+
})

src/extend-expect.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as extensions from './matchers'
2+
3+
expect.extend(extensions)

src/matchers/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {toBeInTheConsole} from './to-be-in-the-console'
2+
3+
export {
4+
toBeInTheConsole
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
ensureNoExpected,
3+
matcherHint,
4+
printReceived,
5+
} from 'jest-matcher-utils';
6+
7+
export function toBeInTheConsole(instance, expected) {
8+
// This code is 1:1 with Jest's built-in "ToBeTruthy"
9+
// @see https://github.com/facebook/jest/blob/main/packages/expect/src/matchers.ts#L398-L414
10+
const matcherName = 'toBeTruthy';
11+
const options = {
12+
// eslint-disable-next-line @babel/no-invalid-this
13+
isNot: this.isNot,
14+
// eslint-disable-next-line @babel/no-invalid-this
15+
promise: this.promise,
16+
};
17+
ensureNoExpected(expected, matcherName, options);
18+
19+
const pass = !!instance;
20+
21+
const message = () =>
22+
`${matcherHint(matcherName, undefined, '', options)
23+
}\n\n` +
24+
`Received: ${printReceived(instance)}`;
25+
26+
return {message, pass};
27+
}

tests/setup-env.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import jestSnapshotSerializerAnsi from 'jest-snapshot-serializer-ansi'
2+
import '../src/extend-expect';
23

34
expect.addSnapshotSerializer(jestSnapshotSerializerAnsi)
45
// add serializer for MutationRecord

types/extend-expect.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// TypeScript Version: 3.8
2+
3+
/// <reference types="jest" />
4+
5+
declare namespace jest {
6+
interface Matchers<R, ignored> {
7+
/**
8+
* @description
9+
* Assert whether an element is present in the console or not.
10+
* @example
11+
* expect(queryByText('Hello world')).toBeInTheDocument()
12+
*/
13+
toBeInTheConsole(): R;
14+
}
15+
}

0 commit comments

Comments
 (0)