(
title: string,
fn: (d: { expect: typeof import('chai').expect }) => void
)
| 342 | } |
| 343 | |
| 344 | export async function itInNodeProcess( |
| 345 | title: string, |
| 346 | fn: (d: { expect: typeof import('chai').expect }) => void |
| 347 | ) { |
| 348 | it(title, async () => { |
| 349 | const script = ` |
| 350 | import { expect } from 'chai'; |
| 351 | const run = ${fn}; |
| 352 | run({ expect }).then( |
| 353 | () => { |
| 354 | process.exitCode = 0; |
| 355 | }, |
| 356 | error => { |
| 357 | console.error(error) |
| 358 | process.exitCode = 1; |
| 359 | } |
| 360 | );\n`; |
| 361 | |
| 362 | const scriptName = `./testing_${title.split(/\s/).join('_')}_script.cts`; |
| 363 | const cwd = path.resolve(__dirname, '..', '..'); |
| 364 | const tsNode = path.resolve(__dirname, '..', '..', 'node_modules', '.bin', 'ts-node'); |
| 365 | try { |
| 366 | await fs.writeFile(scriptName, script, { encoding: 'utf8' }); |
| 367 | const scriptInstance = child_process.fork(scriptName, { |
| 368 | signal: AbortSignal.timeout(50_000), |
| 369 | cwd, |
| 370 | stdio: 'pipe', |
| 371 | execArgv: [tsNode] |
| 372 | }); |
| 373 | |
| 374 | scriptInstance.stdout?.setEncoding('utf8'); |
| 375 | scriptInstance.stderr?.setEncoding('utf8'); |
| 376 | |
| 377 | let stdout = ''; |
| 378 | scriptInstance.stdout?.addListener('data', data => { |
| 379 | stdout += data; |
| 380 | }); |
| 381 | |
| 382 | let stderr = ''; |
| 383 | scriptInstance.stderr?.addListener('data', (data: string) => { |
| 384 | stderr += data; |
| 385 | }); |
| 386 | |
| 387 | // do not fail the test if the debugger is running |
| 388 | stderr = stderr |
| 389 | .split('\n') |
| 390 | .filter(line => !line.startsWith('Debugger') && !line.startsWith('For help')) |
| 391 | .join('\n'); |
| 392 | |
| 393 | const [exitCode] = await once(scriptInstance, 'close'); |
| 394 | |
| 395 | if (stderr.length) console.log(stderr); |
| 396 | expect({ exitCode, stdout, stderr }).to.deep.equal({ exitCode: 0, stdout: '', stderr: '' }); |
| 397 | } finally { |
| 398 | await fs.unlink(scriptName); |
| 399 | } |
| 400 | }); |
| 401 | } |
no test coverage detected