Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] } } }),
},
}
},
Expand Down
44 changes: 44 additions & 0 deletions test/affected.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading