diff --git a/docs/differences.md b/docs/differences.md index a7baf9b..ce3576f 100644 --- a/docs/differences.md +++ b/docs/differences.md @@ -110,24 +110,15 @@ const {getByText} = render(/* Something */) expect(getByText('Hello World')).toBeInTheDocument() ``` -However, in today's version of `CLI Testing Library`, the same would look -something like this: +In today's version of `CLI Testing Library`, the same would look something like +this: ```javascript const {getByText} = render(/* Something */) -expect(getByText('Hello World')).toBeTruthy() +expect(getByText('Hello World')).toBeInTheConsole() ``` -This is because, unlike `DOM Testing Library`, our TestInstances do not have -parental relationships. As a result, what would be a fitting replacement for -`document`? - -That said, because `toBeInTheDocument` is implemented by `jest-dom`, we can -absolutely implement something similar in our codebase if requested. We have -some thoughts around the technical aspects, but -[we'd love to jump deeper into that discussion with others](https://github.com/crutchcorn/cli-testing-library/issues/2) - # Similarities None of this is to say that we're not dedicated to being aligned with upstream. diff --git a/docs/matchers.md b/docs/matchers.md new file mode 100644 index 0000000..83a0dca --- /dev/null +++ b/docs/matchers.md @@ -0,0 +1,103 @@ +# Matchers + +The `cli-testing-library` provides a set of custom jest matchers that you can +use to extend jest. These will make your tests more declarative, clear to read +and to maintain. + +## Table of Contents + + + + +- [Usage](#usage) + - [With TypeScript](#with-typescript) +- [Custom matchers](#custom-matchers) + - [`toBeInTheConsole`](#tobeintheconsole) + - [Examples](#examples) + - [`toHaveErrorMessage`](#tohaveerrormessage) + - [Examples](#examples-1) + + + +## Usage + +Import `cli-testing-library/extend-expect` once (for instance in your +[tests setup file](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array)) +and you're good to go: + +```javascript +// In your own jest-setup.js (or any other name) +import 'cli-testing-library/extend-expect' + +// In jest.config.js add (if you haven't already) +setupFilesAfterEnv: ['/jest-setup.js'] +``` + +### With TypeScript + +If you're using TypeScript, make sure your setup file is a `.ts` and not a `.js` +to include the necessary types. + +You will also need to include your setup file in your `tsconfig.json` if you +haven't already: + +```json + // In tsconfig.json + "include": [ + ... + "./jest-setup.ts" + ], +``` + +## Custom matchers + +### `toBeInTheConsole` + +```typescript +toBeInTheConsole() +``` + +This allows you to assert whether an instance is present or not. Useful when +combined with queries (such as `getByText` or `getByError`) that return the +`TestInstance` + +#### Examples + +```html +Input your name: +``` + +```javascript +expect(getByText(instance, 'Input your name:')).toBeInTheConsole() +``` + +
+ +### `toHaveErrorMessage` + +```typescript +toHaveErrorMessage(text: string | RegExp) +``` + +This allows you to check whether the given instance has an `stderr` message or +not. + +Whitespace is normalized. + +When a `string` argument is passed through, it will perform a whole +case-sensitive match to the error message text. + +To perform a case-insensitive match, you can use a `RegExp` with the `/i` +modifier. + +To perform a partial match, you can pass a `RegExp`. + +#### Examples + +```html +File not found in output +``` + +```javascript +expect(instance).toHaveErrorMessage('File not found in output') +``` diff --git a/src/matchers/to-be-in-the-console.js b/src/matchers/to-be-in-the-console.js index 3832f5b..22ba877 100644 --- a/src/matchers/to-be-in-the-console.js +++ b/src/matchers/to-be-in-the-console.js @@ -1,13 +1,15 @@ /* eslint-disable @babel/no-invalid-this */ -import {getDefaultNormalizer} from "../matches"; -import {checkCliInstance, getMessage} from "./utils"; +import {getDefaultNormalizer} from '../matches' +import {checkCliInstance, getMessage} from './utils' export function toBeInTheConsole(instance) { if (instance !== null || !this.isNot) { checkCliInstance(instance, toBeInTheConsole, this) } - const errormessage = instance ? getDefaultNormalizer()(instance.stderrArr.join('\n')) : null + const errormessage = instance + ? getDefaultNormalizer()(instance.stdoutArr.join('\n')) + : null return { // Does not change based on `.not`, and as a result, we must confirm if it _actually_ is there @@ -15,17 +17,17 @@ export function toBeInTheConsole(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), + this, + this.utils.matcherHint( + `${this.isNot ? '.not' : ''}.toBeInTheConsole`, + 'instance', + '', + ), + `Expected ${to} find the instance in the console`, + '', + 'Received', + this.utils.printReceived(errormessage), ) - } + }, } }