|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Lightweight CLI wrapper that runs Jest with the packaged E2E configuration. |
5 | | - * Consumers can still override CLI flags; the bundled config is injected unless |
6 | | - * `--config` is already provided. |
| 4 | + * CLI for @lit-protocol/e2e. |
| 5 | + * |
| 6 | + * - `lit-e2e` runs the bundled suite with zero local config. |
| 7 | + * - `lit-e2e --patterns <glob,glob>` adds extra specs on the fly. |
| 8 | + * - `lit-e2e init` scaffolds `jest.e2e.local.cjs` and `babel.config.cjs` |
| 9 | + * so teams can hand-roll their own Jest entry-points if they prefer. |
7 | 10 | */ |
| 11 | + |
| 12 | +const fs = require('fs'); |
| 13 | +const os = require('os'); |
8 | 14 | const path = require('path'); |
| 15 | +const { run } = require('jest'); |
| 16 | + |
| 17 | +const PACKAGE_ROOT = path.resolve(__dirname, '..'); |
| 18 | +const WRAPPER_PATH = path.join(__dirname, 'jest.export-wrapper.cjs'); |
| 19 | +const BABEL_CONFIG_SRC = path.join(PACKAGE_ROOT, 'babel.config.cjs'); |
| 20 | + |
| 21 | +const HELP_TEXT = ` |
| 22 | +Usage: lit-e2e [options] [-- <jest-args>] |
| 23 | +
|
| 24 | +Options: |
| 25 | + --patterns, -p <glob[,glob]> Run additional specs (globs resolved from cwd) |
| 26 | + init Scaffold jest.e2e.local.cjs & babel.config.cjs |
| 27 | + --help, -h Show this help message |
| 28 | +
|
| 29 | +Examples: |
| 30 | + lit-e2e --runInBand |
| 31 | + lit-e2e --patterns qa-epoch.spec.ts -- --runInBand |
| 32 | + lit-e2e init |
| 33 | +`.trim(); |
| 34 | + |
| 35 | +function printHelp() { |
| 36 | + console.log(HELP_TEXT); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Parse CLI arguments. |
| 41 | + */ |
| 42 | +function parseArgs(argv) { |
| 43 | + if (argv.includes('init')) { |
| 44 | + return { mode: 'init' }; |
| 45 | + } |
| 46 | + if (argv.includes('--help') || argv.includes('-h')) { |
| 47 | + return { mode: 'help' }; |
| 48 | + } |
| 49 | + |
| 50 | + const patterns = []; |
| 51 | + const passThrough = []; |
| 52 | + |
| 53 | + for (let i = 0; i < argv.length; i += 1) { |
| 54 | + const arg = argv[i]; |
| 55 | + if (arg === '--patterns' || arg === '-p') { |
| 56 | + const value = argv[i + 1]; |
| 57 | + if (!value) { |
| 58 | + throw new Error('Missing value for --patterns'); |
| 59 | + } |
| 60 | + value |
| 61 | + .split(',') |
| 62 | + .map((item) => item.trim()) |
| 63 | + .filter(Boolean) |
| 64 | + .forEach((item) => patterns.push(item)); |
| 65 | + i += 1; |
| 66 | + continue; |
| 67 | + } |
| 68 | + passThrough.push(arg); |
| 69 | + } |
9 | 70 |
|
10 | | -let runJest; |
11 | | -try { |
12 | | - runJest = require('jest').run; |
13 | | -} catch (error) { |
14 | | - console.error('❌ Unable to locate Jest. Please install it in your project (e.g. `pnpm add -D jest`).'); |
15 | | - process.exit(1); |
| 71 | + return { mode: 'run', patterns, passThrough }; |
16 | 72 | } |
17 | 73 |
|
18 | | -const defaultConfig = path.join(__dirname, '..', 'jest.e2e.package.config.cjs'); |
| 74 | +/** |
| 75 | + * Normalise extra test patterns. |
| 76 | + */ |
| 77 | +function normalisePatterns(patterns) { |
| 78 | + return patterns.map((pattern) => { |
| 79 | + const absolute = path.isAbsolute(pattern) |
| 80 | + ? pattern |
| 81 | + : path.resolve(process.cwd(), pattern); |
| 82 | + return absolute.replace(/\\/g, '/'); |
| 83 | + }); |
| 84 | +} |
19 | 85 |
|
20 | | -const argv = process.argv.slice(2); |
21 | | -const hasConfigFlag = argv.some((arg) => arg === '--config' || arg.startsWith('--config=')); |
| 86 | +/** |
| 87 | + * Create a temporary Jest config that extends the bundled wrapper. |
| 88 | + */ |
| 89 | +function writeTemporaryConfig(extraPatterns) { |
| 90 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'lit-e2e-')); |
| 91 | + const configPath = path.join(tmpDir, 'jest.config.cjs'); |
| 92 | + const source = ` |
| 93 | +const path = require('path'); |
| 94 | +const baseConfig = require(${JSON.stringify(WRAPPER_PATH)}); |
| 95 | +const extra = ${JSON.stringify(extraPatterns)}; |
22 | 96 |
|
23 | | -if (!hasConfigFlag) { |
24 | | - argv.unshift('--config', defaultConfig); |
| 97 | +module.exports = { |
| 98 | + ...baseConfig, |
| 99 | + testMatch: baseConfig.testMatch.concat(extra), |
| 100 | +}; |
| 101 | +`; |
| 102 | + fs.writeFileSync(configPath, source); |
| 103 | + return { tmpDir, configPath }; |
25 | 104 | } |
26 | 105 |
|
27 | | -runJest(argv); |
| 106 | +function cleanupTemporaryDir(tmpDir) { |
| 107 | + try { |
| 108 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 109 | + } catch (error) { |
| 110 | + // Non fatal |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Scaffold local wrapper/babel config. |
| 116 | + */ |
| 117 | +function createLocalWrapper() { |
| 118 | + const cwd = process.cwd(); |
| 119 | + const jestDest = path.join(cwd, 'jest.e2e.local.cjs'); |
| 120 | + const babelDest = path.join(cwd, 'babel.config.cjs'); |
| 121 | + |
| 122 | + if (!fs.existsSync(jestDest)) { |
| 123 | + fs.copyFileSync(WRAPPER_PATH, jestDest); |
| 124 | + console.log(`✅ Created ${path.relative(cwd, jestDest)}`); |
| 125 | + } else { |
| 126 | + console.warn(`⚠️ ${path.relative(cwd, jestDest)} already exists, skipping`); |
| 127 | + } |
| 128 | + |
| 129 | + if (!fs.existsSync(babelDest)) { |
| 130 | + fs.copyFileSync(BABEL_CONFIG_SRC, babelDest); |
| 131 | + console.log(`✅ Created ${path.relative(cwd, babelDest)}`); |
| 132 | + } else { |
| 133 | + console.warn(`⚠️ ${path.relative(cwd, babelDest)} already exists, skipping`); |
| 134 | + } |
| 135 | + |
| 136 | + console.log('✨ Local scaffolding complete. Point Jest at jest.e2e.local.cjs to run from this project.'); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Run Jest using the temporary config. |
| 141 | + */ |
| 142 | +async function runSuite(patterns, passThrough) { |
| 143 | + const normalised = normalisePatterns(patterns); |
| 144 | + const configHandle = writeTemporaryConfig(normalised); |
| 145 | + |
| 146 | + try { |
| 147 | + const jestArgs = ['--config', configHandle.configPath, ...passThrough]; |
| 148 | + await run(jestArgs); |
| 149 | + } finally { |
| 150 | + cleanupTemporaryDir(configHandle.tmpDir); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +(async () => { |
| 155 | + let parsed; |
| 156 | + try { |
| 157 | + parsed = parseArgs(process.argv.slice(2)); |
| 158 | + } catch (error) { |
| 159 | + console.error(`❌ ${error.message || error}`); |
| 160 | + printHelp(); |
| 161 | + process.exit(1); |
| 162 | + } |
| 163 | + |
| 164 | + if (parsed.mode === 'help') { |
| 165 | + printHelp(); |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + if (parsed.mode === 'init') { |
| 170 | + createLocalWrapper(); |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + try { |
| 175 | + await runSuite(parsed.patterns, parsed.passThrough); |
| 176 | + } catch (error) { |
| 177 | + console.error(error && error.stack ? error.stack : error); |
| 178 | + process.exit(1); |
| 179 | + } |
| 180 | +})(); |
0 commit comments