|
1 | 1 | // __tests__/helpers/loadCli.js |
2 | | -import { createRequire } from 'node:module'; |
| 2 | +import { dirname } from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | +import { execSync } from 'node:child_process'; |
3 | 5 | import path from 'node:path'; |
4 | 6 |
|
5 | 7 | export async function loadCliFromFixture(fixtureDir) { |
6 | | - const r = createRequire(path.join(fixtureDir, 'package.json')); |
7 | | - const modulePath = r.resolve('../../../dist/cli/init.js'); // get the path |
8 | | - const mod = r(modulePath); // actually require the module |
9 | | - console.log('[Global Setup] Loaded CLI from fixture:', modulePath); |
10 | | - // Return a wrapper function that changes directory before running CLI |
11 | | - const wrappedCli = async (args = []) => { |
| 8 | + // Get the directory of this helper file |
| 9 | + const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 10 | + console.log('__dirname: ', __dirname); |
| 11 | + |
| 12 | + // Build the path to the CLI module - from helpers/ go up to core/ then to dist/ |
| 13 | + const modulePath = path.resolve(__dirname, '../../dist/cli/init.js'); |
| 14 | + console.log('[Global Setup] CLI path:', modulePath); |
| 15 | + |
| 16 | + // Return a wrapper function that runs CLI as separate process to avoid module loading issues |
| 17 | + const wrappedCli = async () => { |
12 | 18 | const originalCwd = process.cwd(); |
13 | 19 | try { |
14 | 20 | process.chdir(fixtureDir); // Change to fixture directory |
| 21 | + console.log('[Global Setup] Changed to fixture directory:', fixtureDir); |
15 | 22 |
|
16 | | - // The init.js exports a cli function, so call it |
| 23 | + // Run the CLI as a separate Node.js process to avoid module loading conflicts |
| 24 | + const result = execSync(`node "${modulePath}"`, { stdio: 'pipe', encoding: 'utf-8', timeout: 30000 }); |
17 | 25 |
|
18 | | - if (typeof mod === 'function') { |
19 | | - return await Promise.resolve(mod(args)); // run the CLI |
20 | | - } else if (typeof mod.default === 'function') { |
21 | | - return await Promise.resolve(mod.default(args)); // run the CLI (ESM default export) |
22 | | - } else { |
23 | | - throw new Error('Could not find CLI function in init.js'); |
24 | | - } |
| 26 | + console.log('[Global Setup] CLI executed successfully'); |
| 27 | + return result; |
| 28 | + } catch (error) { |
| 29 | + console.error('[Global Setup] CLI execution failed:', error.message); |
| 30 | + if (error.stdout) console.log('STDOUT:', error.stdout); |
| 31 | + if (error.stderr) console.log('STDERR:', error.stderr); |
| 32 | + throw error; |
25 | 33 | } finally { |
26 | 34 | process.chdir(originalCwd); // Always restore original directory |
27 | 35 | console.log('[Global Setup] Restored original directory:', originalCwd); |
|
0 commit comments