|
| 1 | +import type { RunVitestConfig } from '../../test-utils' |
| 2 | +import { setDefaultResultOrder } from 'node:dns' |
1 | 3 | import path from 'node:path' |
2 | | -import { expect, test } from 'vitest' |
| 4 | +import { playwright } from '@vitest/browser-playwright' |
| 5 | +import { webdriverio } from '@vitest/browser-webdriverio' |
| 6 | +import { afterAll, expect, test } from 'vitest' |
3 | 7 | import { rolldownVersion } from 'vitest/node' |
4 | 8 | import { runInlineTests, runVitest } from '../../test-utils' |
5 | 9 |
|
| 10 | +// webdriver@9 sets dns.setDefaultResultOrder("ipv4first") on import, |
| 11 | +// which makes Vite resolve localhost to 127.0.0.1 and breaks other tests asserting "localhost" |
| 12 | +afterAll(() => setDefaultResultOrder('verbatim')) |
| 13 | + |
6 | 14 | test('setting resetMocks works if restoreMocks is also set', async () => { |
7 | 15 | const { stderr, testTree } = await runInlineTests({ |
8 | 16 | 'vitest.config.js': { |
@@ -133,3 +141,139 @@ test('can mock invalid module', () => { |
133 | 141 | `) |
134 | 142 | } |
135 | 143 | }) |
| 144 | + |
| 145 | +function modeToConfig(mode: string): RunVitestConfig { |
| 146 | + if (mode === 'playwright') { |
| 147 | + return { |
| 148 | + browser: { |
| 149 | + enabled: true, |
| 150 | + provider: playwright(), |
| 151 | + instances: [{ browser: 'chromium' }], |
| 152 | + headless: true, |
| 153 | + }, |
| 154 | + } |
| 155 | + } |
| 156 | + if (mode === 'webdriverio') { |
| 157 | + return { |
| 158 | + browser: { |
| 159 | + enabled: true, |
| 160 | + provider: webdriverio(), |
| 161 | + instances: [{ browser: 'chrome' }], |
| 162 | + headless: true, |
| 163 | + }, |
| 164 | + } |
| 165 | + } |
| 166 | + return {} |
| 167 | +} |
| 168 | + |
| 169 | +test.for(['node', 'playwright', 'webdriverio'])('importOriginal for virtual modules (%s)', async (mode) => { |
| 170 | + const { stderr, errorTree, root } = await runInlineTests({ |
| 171 | + 'vitest.config.js': ` |
| 172 | +import { defineConfig } from 'vitest/config' |
| 173 | +export default defineConfig({ |
| 174 | + plugins: [{ |
| 175 | + name: 'virtual-test', |
| 176 | + resolveId(source) { |
| 177 | + if (source === 'virtual:my-module') { |
| 178 | + return "\\0" + source |
| 179 | + } |
| 180 | + }, |
| 181 | + load(id) { |
| 182 | + if (id === '\\0virtual:my-module') { |
| 183 | + return 'export const value = "original"' |
| 184 | + } |
| 185 | + }, |
| 186 | + }], |
| 187 | +}) |
| 188 | + `, |
| 189 | + './basic.test.js': ` |
| 190 | +import { test, expect, vi } from 'vitest' |
| 191 | +import { value } from 'virtual:my-module' |
| 192 | +
|
| 193 | +vi.mock('virtual:my-module', async (importOriginal) => { |
| 194 | + const original = await importOriginal() |
| 195 | + return { value: original.value + '-modified' } |
| 196 | +}) |
| 197 | +
|
| 198 | +test('importOriginal returns original virtual module exports', () => { |
| 199 | + expect(value).toBe('original-modified') |
| 200 | +}) |
| 201 | + `, |
| 202 | + }, modeToConfig(mode)) |
| 203 | + |
| 204 | + // webdriverio uses a server-side interceptor plugin whose load hook |
| 205 | + // intercepts the clean id, so importActual returns the mock instead |
| 206 | + // of the original module. This is a known limitation. |
| 207 | + if (mode === 'webdriverio') { |
| 208 | + const tree = errorTree() |
| 209 | + tree['basic.test.js'].__module_errors__ = tree['basic.test.js'].__module_errors__.map( |
| 210 | + (e: string) => e.replace(root, '<root>'), |
| 211 | + ) |
| 212 | + expect(tree).toMatchInlineSnapshot(` |
| 213 | + { |
| 214 | + "__unhandled_errors__": [ |
| 215 | + "[vitest] There was an error when mocking a module. If you are using "vi.mock" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. Read more: https://vitest.dev/api/vi.html#vi-mock", |
| 216 | + ], |
| 217 | + "basic.test.js": { |
| 218 | + "__module_errors__": [ |
| 219 | + "Failed to import test file <root>/basic.test.js", |
| 220 | + ], |
| 221 | + }, |
| 222 | + } |
| 223 | + `) |
| 224 | + } |
| 225 | + else { |
| 226 | + expect(stderr).toBe('') |
| 227 | + expect(errorTree()).toMatchInlineSnapshot(` |
| 228 | + { |
| 229 | + "basic.test.js": { |
| 230 | + "importOriginal returns original virtual module exports": "passed", |
| 231 | + }, |
| 232 | + } |
| 233 | + `) |
| 234 | + } |
| 235 | +}) |
| 236 | + |
| 237 | +test.for(['node', 'playwright', 'webdriverio'])('mocking virtual module without importOriginal skips loading original (%s)', async (mode) => { |
| 238 | + const { stderr, testTree } = await runInlineTests({ |
| 239 | + 'vitest.config.js': ` |
| 240 | +import { defineConfig } from 'vitest/config' |
| 241 | +export default defineConfig({ |
| 242 | + plugins: [{ |
| 243 | + name: 'virtual-test', |
| 244 | + resolveId(source) { |
| 245 | + if (source === 'virtual:my-module') { |
| 246 | + return "\\0" + source |
| 247 | + } |
| 248 | + }, |
| 249 | + load(id) { |
| 250 | + if (id === '\\0virtual:my-module') { |
| 251 | + throw new Error('virtual module load should not be called') |
| 252 | + } |
| 253 | + }, |
| 254 | + }], |
| 255 | +}) |
| 256 | + `, |
| 257 | + './basic.test.js': ` |
| 258 | +import { test, expect, vi } from 'vitest' |
| 259 | +import { value } from 'virtual:my-module' |
| 260 | +
|
| 261 | +vi.mock('virtual:my-module', () => { |
| 262 | + return { value: 'mocked' } |
| 263 | +}) |
| 264 | +
|
| 265 | +test('mock works without loading original', () => { |
| 266 | + expect(value).toBe('mocked') |
| 267 | +}) |
| 268 | + `, |
| 269 | + }, modeToConfig(mode)) |
| 270 | + |
| 271 | + expect(stderr).toBe('') |
| 272 | + expect(testTree()).toMatchInlineSnapshot(` |
| 273 | + { |
| 274 | + "basic.test.js": { |
| 275 | + "mock works without loading original": "passed", |
| 276 | + }, |
| 277 | + } |
| 278 | + `) |
| 279 | +}) |
0 commit comments