Skip to content

Commit bccd855

Browse files
authored
fix(laravel): make mock work (#421)
1 parent 1a8ee07 commit bccd855

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

tests/laravel.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
11
import { runInRepo } from '../utils.ts'
22
import type { RunOptions } from '../types.d.ts'
3+
import path from 'node:path'
4+
import fs from 'node:fs'
35

46
export async function test(options: RunOptions) {
57
//see https://github.com/laravel/vite-plugin/blob/73466441b0c9eb0c1a5ce0a0e937bd83eaef4b70/.github/workflows/tests.yml#L10
68
process.env.LARAVEL_BYPASS_ENV_CHECK = '1'
79
await runInRepo({
810
...options,
911
repo: 'laravel/vite-plugin',
10-
branch: '1.x',
12+
branch: '2.x',
1113
build: 'build',
14+
async beforeTest() {
15+
// Add `vitest.config.ts` to exclude Vite from inlined by Vitest.
16+
// Otherwise the mock here doesn't work.
17+
// https://github.com/laravel/vite-plugin/blob/3f7bf9eddc69580796c26890c99065d7259c785e/tests/index.test.ts#L7-L22
18+
const dir = path.resolve(options.workspace, 'vite-plugin')
19+
const vitestConfigFile = path.join(dir, 'vitest.config.ts')
20+
fs.writeFileSync(
21+
vitestConfigFile,
22+
getVitestConfig(options.vitePath),
23+
'utf-8',
24+
)
25+
},
1226
test: 'test',
27+
agent: 'npm',
1328
})
1429
}
30+
31+
const getVitestConfig = (viteRepoPath: string) => /* ts */ `
32+
import { defineConfig } from "vitest/config";
33+
34+
export default defineConfig({
35+
test: {
36+
deps: {
37+
moduleDirectories: [
38+
${JSON.stringify(path.resolve(viteRepoPath, 'packages'))},
39+
],
40+
},
41+
},
42+
});
43+
`

utils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ export async function runInRepo(options: RunOptions & RepoOptions) {
352352
...localOverrides,
353353
}
354354
}
355-
await applyPackageOverrides(dir, pkg, overrides)
355+
await applyPackageOverrides(agent, dir, pkg, overrides)
356356
await beforeBuildCommand?.(pkg.scripts)
357357
await buildCommand?.(pkg.scripts)
358358
if (test) {
@@ -525,6 +525,7 @@ async function overridePackageManagerVersion(
525525
}
526526

527527
export async function applyPackageOverrides(
528+
agent: (typeof AGENTS)[number],
528529
dir: string,
529530
pkg: any,
530531
overrides: Overrides = {},
@@ -540,10 +541,6 @@ export async function applyPackageOverrides(
540541
)
541542
await $`git clean -fdxq` // remove current install
542543

543-
const agent = await detect({ cwd: dir, autoInstall: false })
544-
if (!agent) {
545-
throw new Error(`failed to detect packageManager in ${dir}`)
546-
}
547544
// Remove version from agent string:
548545
// yarn@berry => yarn
549546
// pnpm@6, pnpm@7 => pnpm

0 commit comments

Comments
 (0)