|
| 1 | +import type { ProcessArgs, ProcessInterface } from '../../src/utils/entry-point'; |
| 2 | +import { getEntryPointType, parseProcessPaths } from '../../src/utils/entry-point'; |
| 3 | + |
| 4 | +const PROCESS_ARG_TESTS: [ProcessInterface, ProcessArgs][] = [ |
| 5 | + [ |
| 6 | + { cwd: () => '/user/tim/docs', argv: ['/bin/node', 'app.js'], execArgv: ['--import', './something.js'] }, |
| 7 | + { appPath: '/user/tim/docs/app.js', importPaths: ['/user/tim/docs/something.js'], requirePaths: [] }, |
| 8 | + ], |
| 9 | + [ |
| 10 | + { |
| 11 | + cwd: () => '/user/tim/docs', |
| 12 | + argv: ['/bin/node', 'app.js'], |
| 13 | + execArgv: ['--import', './something.js', '--import=./else.js'], |
| 14 | + }, |
| 15 | + { |
| 16 | + appPath: '/user/tim/docs/app.js', |
| 17 | + importPaths: ['/user/tim/docs/something.js', '/user/tim/docs/else.js'], |
| 18 | + requirePaths: [], |
| 19 | + }, |
| 20 | + ], |
| 21 | + [ |
| 22 | + { |
| 23 | + cwd: () => '/user/tim/docs', |
| 24 | + argv: ['/bin/node', 'app.js'], |
| 25 | + execArgv: ['--require', './something.js', '--import=else.js'], |
| 26 | + }, |
| 27 | + { |
| 28 | + appPath: '/user/tim/docs/app.js', |
| 29 | + importPaths: ['/user/tim/docs/else.js'], |
| 30 | + requirePaths: ['/user/tim/docs/something.js'], |
| 31 | + }, |
| 32 | + ], |
| 33 | + [ |
| 34 | + { |
| 35 | + cwd: () => '/user/tim/docs', |
| 36 | + argv: ['/bin/node', 'app.js'], |
| 37 | + execArgv: ['--require=here/something.js'], |
| 38 | + }, |
| 39 | + { |
| 40 | + appPath: '/user/tim/docs/app.js', |
| 41 | + importPaths: [], |
| 42 | + requirePaths: ['/user/tim/docs/here/something.js'], |
| 43 | + }, |
| 44 | + ], |
| 45 | +]; |
| 46 | + |
| 47 | +describe('getEntryPointType', () => { |
| 48 | + it.each(PROCESS_ARG_TESTS)('parseProcessArgs', (input, output) => { |
| 49 | + const result = parseProcessPaths(input); |
| 50 | + expect(result).toStrictEqual(output); |
| 51 | + }); |
| 52 | + |
| 53 | + it('app absolute', () => { |
| 54 | + const ctx = getEntryPointType({ |
| 55 | + cwd: () => __dirname, |
| 56 | + argv: ['/bin/node', __filename], |
| 57 | + execArgv: [], |
| 58 | + }); |
| 59 | + |
| 60 | + expect(ctx).toEqual('app'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('app relative', () => { |
| 64 | + const ctx = getEntryPointType({ |
| 65 | + cwd: () => __dirname, |
| 66 | + argv: ['/bin/node', 'entry-point.test.ts'], |
| 67 | + execArgv: [], |
| 68 | + }); |
| 69 | + |
| 70 | + expect(ctx).toEqual('app'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('import absolute', () => { |
| 74 | + const ctx = getEntryPointType({ |
| 75 | + cwd: () => __dirname, |
| 76 | + argv: ['/bin/node', 'app.ts'], |
| 77 | + execArgv: ['--import', __filename], |
| 78 | + }); |
| 79 | + |
| 80 | + expect(ctx).toEqual('import'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('import relative', () => { |
| 84 | + const ctx = getEntryPointType({ |
| 85 | + cwd: () => __dirname, |
| 86 | + argv: ['/bin/node', 'app.ts'], |
| 87 | + execArgv: ['--import', './entry-point.test.ts'], |
| 88 | + }); |
| 89 | + |
| 90 | + expect(ctx).toEqual('import'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('require relative', () => { |
| 94 | + const ctx = getEntryPointType({ |
| 95 | + cwd: () => __dirname, |
| 96 | + argv: ['/bin/node', 'app.ts'], |
| 97 | + execArgv: ['--require', './entry-point.test.ts'], |
| 98 | + }); |
| 99 | + |
| 100 | + expect(ctx).toEqual('require'); |
| 101 | + }); |
| 102 | +}); |
0 commit comments