|
1 | | -import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 1 | +import { spawnSync } from 'node:child_process'; |
| 2 | +import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
2 | 3 | import { tmpdir } from 'node:os'; |
3 | 4 | import { join } from 'node:path'; |
4 | 5 |
|
5 | 6 | import * as semver from 'semver'; |
6 | 7 | import { describe, expect, test } from 'vitest'; |
| 8 | +import * as yaml from 'yaml'; |
7 | 9 |
|
| 10 | +import { VITEST_VERSION } from '../../../cli/src/utils/constants.ts'; |
8 | 11 | import { |
9 | | - alignVendoredVitestDependencies, |
10 | 12 | mergePnpmWorkspaces, |
11 | 13 | syncCargoOxcVersions, |
12 | 14 | syncViteDevtoolsDependencies, |
13 | 15 | } from '../sync-remote-deps.ts'; |
| 16 | +import { alignVendoredVitestDependencies } from '../vendored-vitest.mjs'; |
14 | 17 |
|
15 | 18 | describe('vendored Vitest v5 bridge', () => { |
| 19 | + test('can be imported from stdin without running the bootstrap', () => { |
| 20 | + const root = mkdtempSync(join(tmpdir(), 'vp-vendored-vitest-import-')); |
| 21 | + try { |
| 22 | + const url = new URL('../vendored-vitest.mjs', import.meta.url).href; |
| 23 | + const result = spawnSync(process.execPath, ['--input-type=module', '-'], { |
| 24 | + cwd: root, |
| 25 | + encoding: 'utf8', |
| 26 | + input: `import ${JSON.stringify(url)};`, |
| 27 | + }); |
| 28 | + expect(result.stderr).toBe(''); |
| 29 | + expect(result.status).toBe(0); |
| 30 | + } finally { |
| 31 | + rmSync(root, { recursive: true, force: true }); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + test('keeps the bootstrap runtime pin in sync with the root catalog', () => { |
| 36 | + const workspace = yaml.parse( |
| 37 | + readFileSync(new URL('../../../../pnpm-workspace.yaml', import.meta.url), 'utf8'), |
| 38 | + ); |
| 39 | + expect(workspace.catalog.vitest).toBe(VITEST_VERSION); |
| 40 | + }); |
| 41 | + |
| 42 | + test.each(['4.1.10', '^5.0.0', '5.1.0-beta.1'])( |
| 43 | + 'rejects an unsupported bootstrap runtime pin %s before changing manifests', |
| 44 | + (version) => { |
| 45 | + const root = mkdtempSync(join(tmpdir(), 'vp-vendored-vitest-bootstrap-')); |
| 46 | + try { |
| 47 | + const constantsDir = join(root, 'packages/cli/src/utils'); |
| 48 | + mkdirSync(constantsDir, { recursive: true }); |
| 49 | + writeFileSync( |
| 50 | + join(constantsDir, 'constants.ts'), |
| 51 | + `export const VITEST_VERSION = '${version}';\n`, |
| 52 | + ); |
| 53 | + const script = join(root, 'vendored-vitest.mjs'); |
| 54 | + copyFileSync(new URL('../vendored-vitest.mjs', import.meta.url), script); |
| 55 | + const result = spawnSync(process.execPath, [script], { cwd: root, encoding: 'utf8' }); |
| 56 | + expect(result.status).not.toBe(0); |
| 57 | + expect(result.stderr).toContain('VITEST_VERSION must declare an exact stable v5 version'); |
| 58 | + } finally { |
| 59 | + rmSync(root, { recursive: true, force: true }); |
| 60 | + } |
| 61 | + }, |
| 62 | + ); |
| 63 | + |
| 64 | + test.each(['5.0.0', '5.1.2'])( |
| 65 | + 'runs before dependency installation with the selected %s runtime pin', |
| 66 | + (version) => { |
| 67 | + const root = mkdtempSync(join(tmpdir(), 'vp-vendored-vitest-bootstrap-')); |
| 68 | + try { |
| 69 | + const constantsDir = join(root, 'packages/cli/src/utils'); |
| 70 | + mkdirSync(constantsDir, { recursive: true }); |
| 71 | + writeFileSync( |
| 72 | + join(constantsDir, 'constants.ts'), |
| 73 | + `export const VITEST_VERSION = '${version}';\n`, |
| 74 | + ); |
| 75 | + // Copy the entry point outside the repo so it cannot resolve node_modules. |
| 76 | + const script = join(root, 'vendored-vitest.mjs'); |
| 77 | + copyFileSync(new URL('../vendored-vitest.mjs', import.meta.url), script); |
| 78 | + for (const vendor of ['vite', 'rolldown']) { |
| 79 | + mkdirSync(join(root, vendor, 'packages', vendor), { recursive: true }); |
| 80 | + writeFileSync( |
| 81 | + join(root, vendor, 'packages', vendor, 'package.json'), |
| 82 | + JSON.stringify({ |
| 83 | + dependencies: { '@vitest/utils': '4.1.10' }, |
| 84 | + devDependencies: { vitest: 'catalog:' }, |
| 85 | + optionalDependencies: { '@vitest/spy': '^4.1.10' }, |
| 86 | + }), |
| 87 | + ); |
| 88 | + } |
| 89 | + const run = () => spawnSync(process.execPath, [script], { cwd: root, encoding: 'utf8' }); |
| 90 | + const first = run(); |
| 91 | + expect(first.stderr).toBe(''); |
| 92 | + expect(first.status).toBe(0); |
| 93 | + const manifests = ['vite', 'rolldown'].map((vendor) => |
| 94 | + join(root, vendor, 'packages', vendor, 'package.json'), |
| 95 | + ); |
| 96 | + const sources = manifests.map((file) => readFileSync(file, 'utf8')); |
| 97 | + for (const source of sources) { |
| 98 | + expect(JSON.parse(source)).toEqual({ |
| 99 | + dependencies: { '@vitest/utils': version }, |
| 100 | + devDependencies: { vitest: 'catalog:' }, |
| 101 | + optionalDependencies: { '@vitest/spy': version }, |
| 102 | + }); |
| 103 | + } |
| 104 | + expect(run().status).toBe(0); |
| 105 | + expect(manifests.map((file) => readFileSync(file, 'utf8'))).toEqual(sources); |
| 106 | + } finally { |
| 107 | + rmSync(root, { recursive: true, force: true }); |
| 108 | + } |
| 109 | + }, |
| 110 | + ); |
| 111 | + |
16 | 112 | test('resolves the reviewed Vitest major catalog conflict', () => { |
17 | 113 | const merged = mergePnpmWorkspaces( |
18 | 114 | { catalog: { vitest: '5.0.0' } }, |
|
0 commit comments