|
1 | 1 | import { expect, test } from 'vitest' |
2 | | -import { runVitest, useFS } from '../../test-utils' |
| 2 | +import { runInlineTests, runVitest, useFS } from '../../test-utils' |
3 | 3 |
|
4 | 4 | test("if file has import.meta.glob, it's not cached", async () => { |
5 | 5 | const { createFile } = useFS( |
@@ -138,6 +138,62 @@ test('if cache key generator is defined, the hash is valid', async () => { |
138 | 138 | `) |
139 | 139 | }) |
140 | 140 |
|
| 141 | +test.each([ |
| 142 | + ['foo', 'bar'], |
| 143 | + ['bar', 'foo'], |
| 144 | +])('cache key generators are scoped to projects (%s, %s)', async (first, second) => { |
| 145 | + const cold = await runInlineTests({ |
| 146 | + 'vitest.config.js': ` |
| 147 | + import { defineConfig } from 'vitest/config' |
| 148 | + export default defineConfig({ |
| 149 | + test: { |
| 150 | + fsModuleCache: true, |
| 151 | + fsModuleCachePath: './node_modules/.vitest-fs-cache', |
| 152 | + projects: ${JSON.stringify([first, second])}.map(name => ({ |
| 153 | + plugins: [{ |
| 154 | + name: 'replacer', |
| 155 | + configureVitest({ defineCacheKeyGenerator }) { |
| 156 | + defineCacheKeyGenerator(() => name) |
| 157 | + }, |
| 158 | + transform(code, id) { |
| 159 | + if (id.endsWith('/common.js')) { |
| 160 | + return code.replace('PLACEHOLDER', name) |
| 161 | + } |
| 162 | + }, |
| 163 | + }], |
| 164 | + test: { name, include: [name + '.test.js'] }, |
| 165 | + })), |
| 166 | + }, |
| 167 | + }) |
| 168 | + `, |
| 169 | + 'common.js': `export const value = 'PLACEHOLDER'`, |
| 170 | + 'foo.test.js': ` |
| 171 | + import { expect, test } from 'vitest' |
| 172 | + import { value } from './common.js' |
| 173 | + test('project value', () => expect(value).toBe('foo')) |
| 174 | + `, |
| 175 | + 'bar.test.js': ` |
| 176 | + import { expect, test } from 'vitest' |
| 177 | + import { value } from './common.js' |
| 178 | + test('project value', () => expect(value).toBe('bar')) |
| 179 | + `, |
| 180 | + }) |
| 181 | + const warm = await runVitest({ root: cold.root }) |
| 182 | + for (const run of [cold, warm]) { |
| 183 | + expect(run.stderr).toBe('') |
| 184 | + expect(run.errorTree()).toMatchInlineSnapshot(` |
| 185 | + { |
| 186 | + "bar.test.js": { |
| 187 | + "project value": "passed", |
| 188 | + }, |
| 189 | + "foo.test.js": { |
| 190 | + "project value": "passed", |
| 191 | + }, |
| 192 | + } |
| 193 | + `) |
| 194 | + } |
| 195 | +}) |
| 196 | + |
141 | 197 | test('if cache key generator bails out, the file is not cached', async () => { |
142 | 198 | process.env.REPLACED = 'value1' |
143 | 199 |
|
|
0 commit comments