|
| 1 | +import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { findRootDirAndLockFiles, warnDuplicatedLockFiles } from './find-root' |
| 5 | + |
| 6 | +describe('findRootDirAndLockFiles()', () => { |
| 7 | + it('prefers the nearest lockfile over stray parent lockfiles', async () => { |
| 8 | + const rootDir = await mkdtemp(join(tmpdir(), 'nextjs-find-root-')) |
| 9 | + |
| 10 | + try { |
| 11 | + const appDir = join(rootDir, 'app') |
| 12 | + const parentLockfile = join(rootDir, 'package-lock.json') |
| 13 | + const appLockfile = join(appDir, 'package-lock.json') |
| 14 | + |
| 15 | + await mkdir(appDir) |
| 16 | + await writeFile(parentLockfile, '{}') |
| 17 | + await writeFile(appLockfile, '{}') |
| 18 | + |
| 19 | + const result = findRootDirAndLockFiles(appDir) |
| 20 | + |
| 21 | + expect(result.rootDir).toBe(appDir) |
| 22 | + expect(result.lockFiles).toEqual([appLockfile, parentLockfile]) |
| 23 | + } finally { |
| 24 | + await rm(rootDir, { recursive: true, force: true }) |
| 25 | + } |
| 26 | + }) |
| 27 | +}) |
| 28 | + |
| 29 | +describe('warnDuplicatedLockFiles()', () => { |
| 30 | + it('reports the nearest lockfile as the selected root marker', () => { |
| 31 | + const appLockfile = '/repo/app/package-lock.json' |
| 32 | + const parentLockfile = '/repo/package-lock.json' |
| 33 | + const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}) |
| 34 | + |
| 35 | + try { |
| 36 | + warnDuplicatedLockFiles([appLockfile, parentLockfile]) |
| 37 | + |
| 38 | + const message = String(warn.mock.calls[0][0]) |
| 39 | + |
| 40 | + expect(message).toContain( |
| 41 | + `selected the directory of ${appLockfile} as the root directory` |
| 42 | + ) |
| 43 | + expect(message).toContain(`* ${parentLockfile}`) |
| 44 | + expect(message).not.toContain( |
| 45 | + `selected the directory of ${parentLockfile} as the root directory` |
| 46 | + ) |
| 47 | + } finally { |
| 48 | + warn.mockRestore() |
| 49 | + } |
| 50 | + }) |
| 51 | +}) |
0 commit comments