diff --git a/extend-expect.js b/extend-expect.js new file mode 100644 index 0000000..e7d19c1 --- /dev/null +++ b/extend-expect.js @@ -0,0 +1,2 @@ +// eslint-disable-next-line +require('./dist/extend-expect') diff --git a/package.json b/package.json index 546bda2..309519d 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "@babel/code-frame": "^7.10.4", "@babel/runtime": "^7.12.5", "chalk": "^4.1.0", + "jest-matcher-utils": "^27.4.2", "lz-string": "^1.4.4", "pretty-format": "^27.0.2", "strip-ansi": "^6.0.1", diff --git a/src/__tests__/matchers.js b/src/__tests__/matchers.js new file mode 100644 index 0000000..c2c9883 --- /dev/null +++ b/src/__tests__/matchers.js @@ -0,0 +1,11 @@ +const {resolve} = require('path') +const {render} = require('../pure') + +test('Should handle argument passing', async () => { + const {findByText} = await render('node', [ + resolve(__dirname, './execute-scripts/list-args.js'), + '--version', + ]) + + expect(await findByText('--version')).toBeInTheConsole() +}) diff --git a/src/extend-expect.js b/src/extend-expect.js new file mode 100644 index 0000000..3801a1d --- /dev/null +++ b/src/extend-expect.js @@ -0,0 +1,3 @@ +import * as extensions from './matchers' + +expect.extend(extensions) diff --git a/src/matchers/index.js b/src/matchers/index.js new file mode 100644 index 0000000..37a4816 --- /dev/null +++ b/src/matchers/index.js @@ -0,0 +1,5 @@ +import {toBeInTheConsole} from './to-be-in-the-console' + +export { + toBeInTheConsole +} diff --git a/src/matchers/to-be-in-the-console.js b/src/matchers/to-be-in-the-console.js new file mode 100644 index 0000000..b4af0a4 --- /dev/null +++ b/src/matchers/to-be-in-the-console.js @@ -0,0 +1,27 @@ +import { + ensureNoExpected, + matcherHint, + printReceived, +} from 'jest-matcher-utils'; + +export function toBeInTheConsole(instance, expected) { + // This code is 1:1 with Jest's built-in "ToBeTruthy" + // @see https://github.com/facebook/jest/blob/main/packages/expect/src/matchers.ts#L398-L414 + const matcherName = 'toBeTruthy'; + const options = { + // eslint-disable-next-line @babel/no-invalid-this + isNot: this.isNot, + // eslint-disable-next-line @babel/no-invalid-this + promise: this.promise, + }; + ensureNoExpected(expected, matcherName, options); + + const pass = !!instance; + + const message = () => + `${matcherHint(matcherName, undefined, '', options) + }\n\n` + + `Received: ${printReceived(instance)}`; + + return {message, pass}; +} diff --git a/tests/setup-env.js b/tests/setup-env.js index e67b4d9..fdd4d05 100644 --- a/tests/setup-env.js +++ b/tests/setup-env.js @@ -1,4 +1,5 @@ import jestSnapshotSerializerAnsi from 'jest-snapshot-serializer-ansi' +import '../src/extend-expect'; expect.addSnapshotSerializer(jestSnapshotSerializerAnsi) // add serializer for MutationRecord diff --git a/types/extend-expect.d.ts b/types/extend-expect.d.ts new file mode 100644 index 0000000..71c737a --- /dev/null +++ b/types/extend-expect.d.ts @@ -0,0 +1,15 @@ +// TypeScript Version: 3.8 + +/// + +declare namespace jest { + interface Matchers { + /** + * @description + * Assert whether an element is present in the console or not. + * @example + * expect(queryByText('Hello world')).toBeInTheDocument() + */ + toBeInTheConsole(): R; + } +}