|
| 1 | +import type { WorkerGlobalState } from '../../../packages/vitest/src/types/worker' |
| 2 | +import { pathToFileURL } from 'node:url' |
| 3 | +import { dirname, normalize } from 'pathe' |
| 4 | +import { describe, expect, test } from 'vitest' |
| 5 | +import { distDir } from '../../../packages/vitest/src/paths' |
| 6 | +import { getCachedVitestImport } from '../../../packages/vitest/src/runtime/moduleRunner/cachedResolver' |
| 7 | + |
| 8 | +const normalizedDistDir = normalize(distDir) |
| 9 | + |
| 10 | +function stateWithRoot(root: string): () => WorkerGlobalState { |
| 11 | + return () => ({ config: { root } }) as WorkerGlobalState |
| 12 | +} |
| 13 | + |
| 14 | +function unrelatedRootForSuffix(suffix: string, fill: string): string { |
| 15 | + return `/${fill.repeat(normalizedDistDir.length - suffix.length - 1)}` |
| 16 | +} |
| 17 | + |
| 18 | +describe('getCachedVitestImport', () => { |
| 19 | + test('does not resolve a bare builtin relative to an unrelated root', () => { |
| 20 | + const root = unrelatedRootForSuffix('t', 'a') |
| 21 | + expect(normalizedDistDir.slice(root.length)).toBe('t') |
| 22 | + |
| 23 | + expect(getCachedVitestImport('timers', stateWithRoot(root))).toBeNull() |
| 24 | + }) |
| 25 | + |
| 26 | + test('does not resolve a builtin subpath relative to an unrelated root', () => { |
| 27 | + const root = unrelatedRootForSuffix('st', 'b') |
| 28 | + expect(normalizedDistDir.slice(root.length)).toBe('st') |
| 29 | + |
| 30 | + expect(getCachedVitestImport('stream/promises', stateWithRoot(root))).toBeNull() |
| 31 | + }) |
| 32 | + |
| 33 | + test('does not derive a relative path from a sibling root prefix', () => { |
| 34 | + const root = normalizedDistDir.slice(0, -'st/dist'.length) |
| 35 | + expect(normalizedDistDir.slice(root.length)).toBe('st/dist') |
| 36 | + |
| 37 | + expect(getCachedVitestImport('st/dist/index.js', stateWithRoot(root))).toBeNull() |
| 38 | + }) |
| 39 | + |
| 40 | + test('resolves a dist path relative to an ancestor root', () => { |
| 41 | + const root = dirname(normalizedDistDir) |
| 42 | + const id = `${normalizedDistDir.slice(root.length)}/index.js?relative` |
| 43 | + |
| 44 | + expect(getCachedVitestImport(id, stateWithRoot(root))).toEqual({ |
| 45 | + externalize: `${pathToFileURL(`${normalizedDistDir}/index.js`)}?relative`, |
| 46 | + type: 'module', |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + test('still resolves absolute and bare Vitest imports', () => { |
| 51 | + const root = unrelatedRootForSuffix('test', 'c') |
| 52 | + const id = `${normalizedDistDir}/index.js?absolute` |
| 53 | + |
| 54 | + expect(getCachedVitestImport(id, stateWithRoot(root))).toEqual({ |
| 55 | + externalize: `${pathToFileURL(`${normalizedDistDir}/index.js`)}?absolute`, |
| 56 | + type: 'module', |
| 57 | + }) |
| 58 | + expect(getCachedVitestImport('vitest/config', stateWithRoot(root))).toEqual({ |
| 59 | + externalize: 'vitest/config', |
| 60 | + type: 'module', |
| 61 | + }) |
| 62 | + }) |
| 63 | +}) |
0 commit comments