|
#!/usr/bin/env node |
|
|
|
const { spawn } = require('child_process'); |
|
|
|
/** |
|
* This script acts as a wrapper for the Jest CLI. |
|
* Its primary purpose is to map the deprecated `--testPathPattern` argument, |
|
* often used by older tools or IDEs, to the modern `--testPathPatterns` argument. |
|
* |
|
* It passes all other arguments and environment variables through to Jest untouched. |
|
*/ |
|
|
|
// 1. Get all command-line arguments passed to this script, excluding 'node' and the script path. |
|
const originalArgs = process.argv.slice(2); |
|
|
|
// 2. Map the arguments, replacing the deprecated pattern with the new one. |
|
// This handles both `--testPathPattern <value>` and `--testPathPattern=<value>`. |
|
const mappedArgs = originalArgs.map((arg) => { |
|
if (arg.startsWith('--testPathPattern=')) { |
|
return arg.replace('--testPathPattern=', '--testPathPatterns='); |
|
} |
|
if (arg === '--testPathPattern') { |
|
return '--testPathPatterns'; |
|
} |
|
return arg; |
|
}); |
|
|
|
try { |
|
// 3. Execute the actual 'jest' command with the modified arguments. |
|
// 'spawn' is used to create a new process. |
|
const jestProcess = spawn('node_modules/jest/bin/jest.js', mappedArgs, { |
|
// The 'env' is inherited from the current process by default, but being |
|
// explicit ensures all environment variables are passed through. |
|
env: process.env, |
|
|
|
// This ensures that the output (stdout, stderr) and input (stdin) of the |
|
// Jest process are connected to the terminal, making it fully interactive. |
|
// This is crucial for colors, progress bars, and watch mode. |
|
stdio: 'inherit', |
|
}); |
|
|
|
// 4. Listen for the Jest process to exit. When it does, exit this |
|
// wrapper script with the same exit code. This is vital for CI/CD pipelines. |
|
jestProcess.on('close', (code) => { |
|
process.exit(code); |
|
}); |
|
|
|
// 5. Handle errors that might occur when trying to spawn the process, |
|
// such as if the 'jest' command is not found in the system's PATH. |
|
jestProcess.on('error', (err) => { |
|
console.error('Failed to start the Jest process:', err); |
|
process.exit(1); |
|
}); |
|
} catch (error) { |
|
console.error('An error occurred within the Jest wrapper script:', error); |
|
process.exit(1); |
|
} |