import colors from 'colors' import { execSync, spawn } from 'node:child_process' import { clearLastLogLineIfEmpty, execConfig } from '../helpers' import { IndexState } from '../types' export const TestPath = async (state: IndexState) => { let testPath = '/API/*' const functionName = state.args[1] execSync('npx prisma generate', execConfig) if (functionName) { const o = functionName.split('_') testPath = `/API/${o[0]}/${o[1]}/${o[2]}.${o[3]}.${o[4]}/index.spec.ts` console.info(colors.yellow(`Testing\n ${colors.green(functionName)} \n@\n ${colors.green(testPath)}\n`)) } else { console.info(colors.green('\n Testing All Paths\n\n')) } const test = spawn( 'jest', [ testPath, state.ciPipeline ? '' : '--watch', '--verbose', '--coverage', '--detectOpenHandles', '--bail', '--forceExit', '--setupFiles', 'dotenv/config', '--color=always', ], { shell: true, }, ) test.stdout.on('data', (data) => { if (!state.ciPipeline) clearLastLogLineIfEmpty() console.info(`${`${data}`.replace(/(\n|\r)/gm, '\n')}`) }) test.stderr.on('data', (data) => { console.error(`${data}`) }) test.on('close', (code) => { console.info(`child process exited with code ${code}`) }) } export default TestPath