|
| 1 | +# API |
| 2 | + |
| 3 | +`CLI Testing Library`, despite taking clear inspiration from, does not re-export |
| 4 | +anything from |
| 5 | +[`DOM Testing Library`](https://testing-library.com/docs/dom-testing-library/). |
| 6 | +Likewise, while we've done our best to match the API names of |
| 7 | +[Testing Library's Core API](https://testing-library.com/docs/), because of the |
| 8 | +inherent differences between CLI apps and web apps, we're unable to match all of |
| 9 | +them. |
| 10 | + |
| 11 | +> Know of a Testing Library Core API that you think would fit here that isn't |
| 12 | +> present? |
| 13 | +> [Let us know!](https://github.com/crutchcorn/cli-testing-library/issues) |
| 14 | +
|
| 15 | +Instead, the following API is what `CLI Testing Library` provides the following. |
| 16 | + |
| 17 | +<!-- START doctoc generated TOC please keep comment here to allow auto update --> |
| 18 | +<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> |
| 19 | + |
| 20 | +- [`render`](#render) |
| 21 | +- [`render` Options](#render-options) |
| 22 | + - [`cwd`](#cwd) |
| 23 | + - [`spawnOpts`](#spawnopts) |
| 24 | +- [`render` Result](#render-result) |
| 25 | + - [`...queries`](#queries) |
| 26 | + - [ByText](#bytext) |
| 27 | + - [`userEvent[eventName]`](#usereventeventname) |
| 28 | + - [`debug`](#debug) |
| 29 | + - [`hasExit`](#hasexit) |
| 30 | + - [`process`](#process) |
| 31 | + - [`stdoutArr`/`stderrArr`](#stdoutarrstderrarr) |
| 32 | + - [`clear`](#clear) |
| 33 | + |
| 34 | +<!-- END doctoc generated TOC please keep comment here to allow auto update --> |
| 35 | + |
| 36 | +# `render` |
| 37 | + |
| 38 | +```typescript |
| 39 | +function render( |
| 40 | + command: string, |
| 41 | + args: string[], |
| 42 | + options?: { |
| 43 | + /* You won't often use this, expand below for docs on options */ |
| 44 | + }, |
| 45 | +): RenderResult |
| 46 | +``` |
| 47 | + |
| 48 | +Run the CLI application in a newly spawned process. |
| 49 | + |
| 50 | +```javascript |
| 51 | +import {render} from 'cli-testing-library' |
| 52 | +
|
| 53 | +render('node', ['./path/to/script.js']) |
| 54 | +``` |
| 55 | + |
| 56 | +```javascript |
| 57 | +import {render} from 'cli-testing-library' |
| 58 | +
|
| 59 | +test('renders a message', () => { |
| 60 | + const {getByText} = render('node', ['./console-out.js']) |
| 61 | + expect(getByText('Hello, world!')).toBeTruthy() |
| 62 | +}) |
| 63 | +``` |
| 64 | + |
| 65 | +# `render` Options |
| 66 | + |
| 67 | +You won't often need to specify options, but if you ever do, here are the |
| 68 | +available options which you could provide as a third argument to `render`. |
| 69 | + |
| 70 | +## `cwd` |
| 71 | + |
| 72 | +By default, `CLI Testing Library` will run the new process in the working |
| 73 | +directory of your project's root, as defined by your testing framework. If you |
| 74 | +provide your own working directory via this option, it will change the execution |
| 75 | +directory of your process. |
| 76 | + |
| 77 | +For example: If you are end-to-end testing a file moving script, you don't want |
| 78 | +to have to specify the absolute path every time. In this case, you can specify a |
| 79 | +directory as the render `cwd`. |
| 80 | + |
| 81 | +```javascript |
| 82 | +const containingPath = path.resolve(__dirname, './movables') |
| 83 | +
|
| 84 | +const {getByText} = render('node', ['mover.js'], { |
| 85 | + cwd: containingPath, |
| 86 | +}) |
| 87 | +``` |
| 88 | + |
| 89 | +## `spawnOpts` |
| 90 | + |
| 91 | +Oftentimes, you want to modify the behavior of the spawn environment. This may |
| 92 | +include things like changing the shell that's used to run scripts or more. |
| 93 | + |
| 94 | +This argument allows you to configure the options that are passed to the |
| 95 | +underlying |
| 96 | +[`child_process.spawn` NodeJS API](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options). |
| 97 | + |
| 98 | +```javascript |
| 99 | +const {getByText} = render('script.ps1', { |
| 100 | + spawnOpts: { |
| 101 | + shell: 'powershell.exe', |
| 102 | + }, |
| 103 | +}) |
| 104 | +``` |
| 105 | + |
| 106 | +# `render` Result |
| 107 | + |
| 108 | +The `render` method returns an object that has a few properties: |
| 109 | + |
| 110 | +## `...queries` |
| 111 | + |
| 112 | +The most important feature of render is that the queries from |
| 113 | +[CLI Testing Library](https://github.com/crutchcorn/cli-testing-library) are |
| 114 | +automatically returned with their first argument bound to the testInstance. |
| 115 | + |
| 116 | +See [Queries](./queries.md) to learn more about how to use these queries and the |
| 117 | +philosophy behind them. |
| 118 | + |
| 119 | +### ByText |
| 120 | + |
| 121 | +> getByText, queryByText, findByText |
| 122 | + |
| 123 | +```typescript |
| 124 | +getByText( |
| 125 | + // If you're using `screen`, then skip the container argument: |
| 126 | + instance: TestInstance, |
| 127 | + text: TextMatch, |
| 128 | + options?: { |
| 129 | + exact?: boolean = true, |
| 130 | + trim?: boolean = false, |
| 131 | + stripAnsi?: boolean = false, |
| 132 | + collapseWhitespace?: boolean = false, |
| 133 | + normalizer?: NormalizerFn, |
| 134 | + suggest?: boolean, |
| 135 | + }): TestInstance |
| 136 | +``` |
| 137 | + |
| 138 | +Queries for test instance `stdout` results with the given text (and it also |
| 139 | +accepts a TextMatch). |
| 140 | + |
| 141 | +These options are all standard for text matching. To learn more, see our |
| 142 | +[Queries page](./queries.md). |
| 143 | + |
| 144 | +## `userEvent[eventName]` |
| 145 | + |
| 146 | +```javascript |
| 147 | +userEvent[eventName](...eventProps) |
| 148 | +``` |
| 149 | + |
| 150 | +> While `userEvent` isn't usually returned on `render` in, say, |
| 151 | +> `React Testing Library`, we're able to do so because of our differences in |
| 152 | +> implementation with upstream. See our [Differences](./differences.md) doc for |
| 153 | +> more. |
| 154 | + |
| 155 | +This object is the same as described with |
| 156 | +[`userEvent` documentation](./user-event.md) with the key difference that |
| 157 | +`instance` is not expected to be passed when bound to `render`. |
| 158 | + |
| 159 | +## `debug` |
| 160 | + |
| 161 | +This method is a shortcut for `console.log(prettyCLI(instance)).` |
| 162 | + |
| 163 | +```javascript |
| 164 | +import {render} from 'cli-testing-library' |
| 165 | +
|
| 166 | +const {debug} = render('command') |
| 167 | +debug() |
| 168 | +// Hello, world! How are you? |
| 169 | +// |
| 170 | +// you can also pass an instance: debug(getByText('message')) |
| 171 | +// and you can pass all the same arguments to debug as you can |
| 172 | +// to prettyCLI: |
| 173 | +// const maxLengthToPrint = 10000 |
| 174 | +// debug(getByText('message'), maxLengthToPrint) |
| 175 | +``` |
| 176 | + |
| 177 | +This is a simple wrapper around `prettyCLI` which is also exposed and comes from |
| 178 | +[CLI Testing Library](./debug.md). |
| 179 | + |
| 180 | +## `hasExit` |
| 181 | + |
| 182 | +This method allows you to check if the spawned process has exit, and if so, what |
| 183 | +exit code it closed with. |
| 184 | + |
| 185 | +```javascript |
| 186 | +const instance = render('command') |
| 187 | +
|
| 188 | +await waitFor(() => instance.hasExit()).toMatchObject({exitCode: 1}) |
| 189 | +``` |
| 190 | + |
| 191 | +This method returns `null` if still running, but `{exitCode: number}` if it has |
| 192 | +exit |
| 193 | + |
| 194 | +## `process` |
| 195 | + |
| 196 | +The spawned process created by your rendered `TestInstnace`. It's a |
| 197 | +`child_instance`. This is a |
| 198 | +[regularly spawned process](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), |
| 199 | +so you can call `process.pid` etc. to inspect the process. |
| 200 | + |
| 201 | +## `stdoutArr`/`stderrArr` |
| 202 | + |
| 203 | +Each of these is an array of what's output by their respective `std`\* pipe. |
| 204 | +This is used internally to create the [`debug`methods](./debug.md) and more. |
| 205 | +They're defined as: |
| 206 | + |
| 207 | +```typescript |
| 208 | +interface TestInstance { |
| 209 | + stdoutArr: Array<Buffer | string> |
| 210 | + stderrArr: Array<Buffer | string> |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +## `clear` |
| 215 | + |
| 216 | +This method acts as the terminal `clear` command might on most systems. It |
| 217 | +allows you to clear out the buffers for `stdoutArr` and `stderrArr` - even in |
| 218 | +the middle of processing - in order to do more narrowed queries. |
0 commit comments