* @param {string} dir * @param {string[]} [args] * @param {CliTestOptions} [options]
(dir, args = [], options = {})
| 151 | * @param {CliTestOptions} [options] |
| 152 | */ |
| 153 | function runCli(dir, args = [], options = {}) { |
| 154 | const promise = runPrettierCli(dir, args, options); |
| 155 | const getters = { |
| 156 | get status() { |
| 157 | return promise.then(({ status }) => status); |
| 158 | }, |
| 159 | get stdout() { |
| 160 | return promise.then(({ stdout }) => stdout); |
| 161 | }, |
| 162 | get stderr() { |
| 163 | return promise.then(({ stderr }) => stderr); |
| 164 | }, |
| 165 | get write() { |
| 166 | return promise.then(({ write }) => write); |
| 167 | }, |
| 168 | test: testResult, |
| 169 | then(onFulfilled, onRejected) { |
| 170 | return promise.then(onFulfilled, onRejected); |
| 171 | }, |
| 172 | }; |
| 173 | |
| 174 | return getters; |
| 175 | |
| 176 | function testResult(testOptions) { |
| 177 | test(options.title || "", async () => { |
| 178 | const result = await promise; |
| 179 | |
| 180 | let snapshot; |
| 181 | for (const name of ["status", "stdout", "stderr", "write"]) { |
| 182 | let value = result[name]; |
| 183 | // \r is trimmed from jest snapshots by default; |
| 184 | // manually replacing this character with /*CR*/ to test its true presence |
| 185 | // If ignoreLineEndings is specified, \r is simply deleted instead |
| 186 | if (name === "stdout" || name === "stderr") { |
| 187 | value = result[name].replace( |
| 188 | /\r/g, |
| 189 | options.ignoreLineEndings ? "" : "/*CR*/", |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | if (name in testOptions) { |
| 194 | if (name === "status" && testOptions[name] === "non-zero") { |
| 195 | expect(value).not.toBe(0); |
| 196 | } else if (typeof testOptions[name] === "function") { |
| 197 | testOptions[name](value); |
| 198 | } else { |
| 199 | expect(value).toStrictEqual(testOptions[name]); |
| 200 | } |
| 201 | } else { |
| 202 | snapshot = snapshot ?? {}; |
| 203 | snapshot[name] = value; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if (snapshot) { |
| 208 | expect(snapshot).toMatchSnapshot(); |
| 209 | } |
| 210 | }); |
searching dependent graphs…