|
| 1 | +import type { Plugin } from 'vite' |
| 2 | +import { mkdtempSync, rmSync } from 'node:fs' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { playwright } from '@vitest/browser-playwright' |
| 5 | +import { join } from 'pathe' |
| 6 | +import { expect, onTestFinished, test } from 'vitest' |
| 7 | +import { createVitest } from 'vitest/node' |
| 8 | +import { createFile, useTmpFS } from '../../test-utils' |
| 9 | +import { instances, provider, runInlineBrowserTests } from './utils' |
| 10 | + |
| 11 | +const CACHED = 'private,max-age=31536000' |
| 12 | + |
| 13 | +function recordResponses(matches: (pathname: string) => boolean = (path) => path === '/shared.ts') { |
| 14 | + const responses: { status: number; cacheControl?: string }[] = [] |
| 15 | + const plugin: Plugin = { |
| 16 | + name: 'record-responses', |
| 17 | + configureServer(server) { |
| 18 | + server.middlewares.use((req, res, next) => { |
| 19 | + res.on('finish', () => { |
| 20 | + if (req.url && matches(new URL(req.url, 'http://localhost').pathname)) { |
| 21 | + responses.push({ |
| 22 | + status: res.statusCode, |
| 23 | + cacheControl: res.getHeader('cache-control') as string | undefined, |
| 24 | + }) |
| 25 | + } |
| 26 | + }) |
| 27 | + next() |
| 28 | + }) |
| 29 | + }, |
| 30 | + } |
| 31 | + return { plugin, responses } |
| 32 | +} |
| 33 | + |
| 34 | +const sharedModuleTests = { |
| 35 | + 'shared.ts': `export const value = 'shared'`, |
| 36 | + 'first.test.ts': ` |
| 37 | + import { expect, test } from 'vitest' |
| 38 | + import { value } from './shared' |
| 39 | + test('imports the shared module', () => { |
| 40 | + expect(value).toBe('shared') |
| 41 | + }) |
| 42 | + `, |
| 43 | + 'second.test.ts': ` |
| 44 | + import { expect, test } from 'vitest' |
| 45 | + import { value } from './shared' |
| 46 | + test('imports the shared module', () => { |
| 47 | + expect(value).toBe('shared') |
| 48 | + }) |
| 49 | + `, |
| 50 | +} |
| 51 | + |
| 52 | +test.runIf(provider.name === 'playwright')( |
| 53 | + 'tester iframes reuse cached modules during the run', |
| 54 | + async () => { |
| 55 | + const { plugin, responses } = recordResponses() |
| 56 | + const { stderr } = await runInlineBrowserTests(sharedModuleTests, { |
| 57 | + fileParallelism: false, |
| 58 | + browser: { instances: [instances[0]] }, |
| 59 | + $viteConfig: { plugins: [plugin] }, |
| 60 | + }) |
| 61 | + |
| 62 | + expect(stderr).toReportNoErrors() |
| 63 | + // the second test file takes the module from the cache |
| 64 | + expect(responses).toEqual([{ status: 200, cacheControl: CACHED }]) |
| 65 | + }, |
| 66 | +) |
| 67 | + |
| 68 | +test.runIf(provider.name === 'playwright')( |
| 69 | + 'modules are revalidated when mocks are served by request interception', |
| 70 | + async () => { |
| 71 | + const { plugin, responses } = recordResponses() |
| 72 | + const { stderr } = await runInlineBrowserTests(sharedModuleTests, { |
| 73 | + fileParallelism: false, |
| 74 | + browser: { instances: [instances[0]], importMapMocks: false }, |
| 75 | + $viteConfig: { plugins: [plugin] }, |
| 76 | + }) |
| 77 | + |
| 78 | + expect(stderr).toReportNoErrors() |
| 79 | + // the second test file revalidates the module |
| 80 | + expect(responses).toEqual([{ status: 200, cacheControl: 'no-cache' }, { status: 304 }]) |
| 81 | + }, |
| 82 | +) |
| 83 | + |
| 84 | +test.runIf(provider.name === 'playwright')( |
| 85 | + 'a persistent context of any instance keeps modules revalidated', |
| 86 | + async () => { |
| 87 | + const userDataDir = mkdtempSync(join(tmpdir(), 'vitest-run-module-cache-')) |
| 88 | + onTestFinished(() => rmSync(userDataDir, { recursive: true, force: true })) |
| 89 | + const { plugin, responses } = recordResponses() |
| 90 | + const { stderr } = await runInlineBrowserTests(sharedModuleTests, { |
| 91 | + fileParallelism: false, |
| 92 | + browser: { |
| 93 | + instances: [ |
| 94 | + { ...instances[0], name: 'a', provider: playwright() }, |
| 95 | + // its cache would keep the modules for the next `vitest run` |
| 96 | + { ...instances[0], name: 'b', provider: playwright({ persistentContext: userDataDir }) }, |
| 97 | + ], |
| 98 | + }, |
| 99 | + $viteConfig: { plugins: [plugin] }, |
| 100 | + }) |
| 101 | + |
| 102 | + expect(stderr).toReportNoErrors() |
| 103 | + expect(responses.length).toBeGreaterThan(0) |
| 104 | + expect(responses.filter((response) => response.cacheControl === CACHED)).toEqual([]) |
| 105 | + }, |
| 106 | +) |
| 107 | + |
| 108 | +test.runIf(provider.name === 'playwright')( |
| 109 | + 'the shims of factory mocks are revalidated', |
| 110 | + async () => { |
| 111 | + const { plugin, responses } = recordResponses((path) => |
| 112 | + path.includes('/@id/__x00__vitest-mock:'), |
| 113 | + ) |
| 114 | + const testFile = (name: string) => ` |
| 115 | + import { expect, test, vi } from 'vitest' |
| 116 | + import { value } from './dep' |
| 117 | + vi.mock('./dep', () => ({ value: '${name}' })) |
| 118 | + test('imports the mock', () => { |
| 119 | + expect(value).toBe('${name}') |
| 120 | + }) |
| 121 | + ` |
| 122 | + const { stderr } = await runInlineBrowserTests( |
| 123 | + { |
| 124 | + 'dep.ts': `export const value = 'dep'`, |
| 125 | + 'first.test.ts': testFile('first'), |
| 126 | + 'second.test.ts': testFile('second'), |
| 127 | + }, |
| 128 | + { |
| 129 | + fileParallelism: false, |
| 130 | + browser: { instances: [instances[0]] }, |
| 131 | + $viteConfig: { plugins: [plugin] }, |
| 132 | + }, |
| 133 | + ) |
| 134 | + |
| 135 | + expect(stderr).toReportNoErrors() |
| 136 | + // the shim takes its exports from the factory of the current test file |
| 137 | + expect(responses.length).toBeGreaterThan(0) |
| 138 | + expect(responses.filter((response) => response.cacheControl === CACHED)).toEqual([]) |
| 139 | + }, |
| 140 | +) |
| 141 | + |
| 142 | +test.runIf(provider.name === 'playwright')( |
| 143 | + 'an invalidated module is read again on the next run', |
| 144 | + async () => { |
| 145 | + const { root } = useTmpFS({ |
| 146 | + 'value.ts': `export const value = 'before'`, |
| 147 | + 'basic.test.ts': ` |
| 148 | + import { test, vi } from 'vitest' |
| 149 | + import { value } from './value' |
| 150 | + test('logs the value', async () => { |
| 151 | + const actual = await vi.importActual('./value') |
| 152 | + console.log(\`value: \${value}, actual: \${actual.value}\`) |
| 153 | + }) |
| 154 | + `, |
| 155 | + }) |
| 156 | + const values: string[] = [] |
| 157 | + const vitest = await createVitest('test', { |
| 158 | + root, |
| 159 | + watch: false, |
| 160 | + reporters: [ |
| 161 | + { |
| 162 | + onUserConsoleLog(log) { |
| 163 | + values.push(log.content.trim()) |
| 164 | + }, |
| 165 | + }, |
| 166 | + ], |
| 167 | + browser: { enabled: true, headless: true, provider, instances: [instances[0]] }, |
| 168 | + }) |
| 169 | + onTestFinished(() => vitest.close()) |
| 170 | + |
| 171 | + await vitest.start() |
| 172 | + createFile(join(root, 'value.ts'), `export const value = 'after'`) |
| 173 | + vitest.invalidateFile(join(root, 'value.ts')) |
| 174 | + await vitest.rerunFiles() |
| 175 | + |
| 176 | + // the browser context of the first run has the old module in its cache |
| 177 | + expect(values).toEqual(['value: before, actual: before', 'value: after, actual: after']) |
| 178 | + }, |
| 179 | +) |
0 commit comments