diff --git a/src/index.ts b/src/index.ts index e746cfd..1e0bc8f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -69,6 +69,16 @@ function supportsChanged(vitest: Vitest): boolean { ) } +/** + * Vitest externalizes modules under node_modules, so the installed setup file + * would import its own `vitest`: resolved from the plugin's directory, which a + * package manager may point at another copy than the one running the tests + * (pnpm, for two workspace packages whose peer sets differ). Its `afterAll` + * would then register with that copy. Inlined, the plugin's modules get the + * running vitest. + */ +const PACKAGE = '@useblacksmith/vitest' + export default function blacksmith(options: BlacksmithOptions = {}): Plugin { const provider = new TreeSnapshotProvider() let changedApplied = false @@ -83,12 +93,17 @@ export default function blacksmith(options: BlacksmithOptions = {}): Plugin { (userConfig as { test?: { sequence?: { sequencer?: unknown } } }).test ?.sequence?.sequencer, ) + // Merging a list into `inline: true` would yield `[true, ...]`. + const inlineAll = + (userConfig as { test?: { server?: { deps?: { inline?: unknown } } } }) + .test?.server?.deps?.inline === true return { test: { experimental: options.shadow ? { recordDependencies: true } : { recordDependencies: true, vcsProvider: provider }, ...(theirs ? {} : { sequence: { sequencer: HashShardSequencer } }), + ...(inlineAll ? {} : { server: { deps: { inline: [PACKAGE] } } }), }, } }, diff --git a/test/affected.test.ts b/test/affected.test.ts index 2764230..b179dde 100644 --- a/test/affected.test.ts +++ b/test/affected.test.ts @@ -766,6 +766,50 @@ describe('worker threads', () => { }) }) +describe('installed under node_modules', () => { + let repo: Repo + beforeAll(() => { + repo = new Repo('installed') + // The built package, with a vitest of its own beside it: what pnpm + // gives a workspace package whose peer set differs from the plugin's. + const installed = path.join( + repo.cwd, + 'node_modules', + '@useblacksmith', + 'vitest', + ) + cpSync(path.join(packageRoot, 'dist'), path.join(installed, 'dist'), { + recursive: true, + }) + cpSync( + path.join(packageRoot, 'package.json'), + path.join(installed, 'package.json'), + ) + cpSync( + path.join(packageRoot, 'node_modules', 'vitest'), + path.join(installed, 'node_modules', 'vitest'), + { recursive: true, dereference: true }, + ) + const config = path.join(repo.cwd, 'vitest.config.ts') + writeFileSync( + config, + readFileSync(config, 'utf8').replace( + "'../../src/index.ts'", + "'@useblacksmith/vitest'", + ), + ) + }) + + test('the setup file registers with the vitest running the tests and still records reads', async () => { + const first = await repo.run() + expect(first.exitCode, first.output).toBe(0) + expect(first.output).not.toContain('failed to find the current suite') + expect(first.ran).toEqual([...TESTS].sort()) + repo.write('src/fixtures/five/data.json', '{ "value": "five", "n": 2 }\n') + expect((await repo.run('--changed')).ran).toEqual(['five']) + }) +}) + describe('git-ignored inputs', () => { test('a change to a generated file the test read reruns it', async () => { const repo = new Repo('ignored')