Skip to content

Commit 0f717df

Browse files
committed
chore: drastically improve logic of toBeInTheConsole
1 parent 2df64e9 commit 0f717df

File tree

3 files changed

+82
-26
lines changed

3 files changed

+82
-26
lines changed

src/__tests__/matchers.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
const {resolve} = require('path')
22
const {render} = require('../pure')
33

4-
test('Should handle argument passing', async () => {
4+
test('toBeInTheConsole should pass when something is in console', async () => {
55
const {findByText} = await render('node', [
66
resolve(__dirname, './execute-scripts/list-args.js'),
77
'--version',
88
])
99

10-
expect(await findByText('--version')).toBeInTheConsole()
10+
await expect(
11+
(async () => expect(await findByText('--version')).toBeInTheConsole())()
12+
).resolves.not.toThrow()
13+
})
14+
15+
test('toBeInTheConsole should fail when something is not console', async () => {
16+
const {queryByText} = await render('node', [
17+
resolve(__dirname, './execute-scripts/list-args.js'),
18+
'--version',
19+
])
20+
21+
expect(() => expect(queryByText('NotHere')).toBeInTheConsole()).toThrow(/value must be a TestInstance/)
1122
})
Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
1-
import {
2-
ensureNoExpected,
3-
matcherHint,
4-
printReceived,
5-
} from 'jest-matcher-utils';
1+
import {checkCliInstance} from "./utils";
62

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 = {
3+
export function toBeInTheConsole(instance) {
4+
// eslint-disable-next-line @babel/no-invalid-this
5+
if (instance !== null || !this.isNot) {
126
// 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};
7+
checkCliInstance(instance, toBeInTheConsole, this)
8+
}
9+
10+
// Assuming it passed above, it must have passed
11+
return {
12+
pass: true,
13+
message: () => []
14+
}
2715
}

src/matchers/utils.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class GenericTypeError extends Error {
2+
constructor(expectedString, received, matcherFn, context) {
3+
super()
4+
5+
/* istanbul ignore next */
6+
if (Error.captureStackTrace) {
7+
Error.captureStackTrace(this, matcherFn)
8+
}
9+
let withType = ''
10+
try {
11+
withType = context.utils.printWithType(
12+
'Received',
13+
received,
14+
context.utils.printReceived,
15+
)
16+
} catch (e) {
17+
// Can throw for Document:
18+
// https://github.com/jsdom/jsdom/issues/2304
19+
}
20+
this.message = [
21+
context.utils.matcherHint(
22+
`${context.isNot ? '.not' : ''}.${matcherFn.name}`,
23+
'received',
24+
'',
25+
),
26+
'',
27+
// eslint-disable-next-line @babel/new-cap
28+
`${context.utils.RECEIVED_COLOR(
29+
'received',
30+
)} value must ${expectedString}.`,
31+
withType,
32+
].join('\n')
33+
}
34+
}
35+
36+
class CliInstanceTypeError extends GenericTypeError {
37+
constructor(...args) {
38+
super('be a TestInstance', ...args)
39+
}
40+
}
41+
42+
/**
43+
* @param {TestInstance} cliInstance
44+
* @param args
45+
*/
46+
function checkCliInstance(cliInstance, ...args) {
47+
if (
48+
!(cliInstance && cliInstance.process && cliInstance.process.stdout)
49+
) {
50+
throw new CliInstanceTypeError(cliInstance, ...args)
51+
}
52+
}
53+
54+
export {
55+
CliInstanceTypeError,
56+
checkCliInstance,
57+
}

0 commit comments

Comments
 (0)