|
| 1 | +import { afterEach, expect, test, vi } from 'vitest' |
| 2 | +import { getModuleGraph } from '../../../packages/vitest/src/utils/graph.js' |
| 3 | +import { runInlineTests } from '../../test-utils' |
| 4 | + |
| 5 | +afterEach(() => { |
| 6 | + vi.unstubAllEnvs() |
| 7 | +}) |
| 8 | + |
| 9 | +test.for(['production', 'test'])( |
| 10 | + 'node builtins are importable in the client environment when NODE_ENV=%s', |
| 11 | + async (nodeEnv) => { |
| 12 | + vi.stubEnv('NODE_ENV', nodeEnv) |
| 13 | + |
| 14 | + const { stderr, testTree, ctx, root } = await runInlineTests( |
| 15 | + { |
| 16 | + 'client-env.js': ` |
| 17 | +export default { |
| 18 | + name: 'client-env', |
| 19 | + viteEnvironment: 'client', |
| 20 | + setup() { |
| 21 | + return { teardown() {} } |
| 22 | + }, |
| 23 | +} |
| 24 | +`, |
| 25 | + 'basic.test.js': ` |
| 26 | +import { timingSafeEqual } from 'node:crypto' |
| 27 | +import * as nodePath from 'node:path' |
| 28 | +import fs from 'fs' |
| 29 | +import { expect, test } from 'vitest' |
| 30 | +
|
| 31 | +test('static imports', () => { |
| 32 | + expect(timingSafeEqual(Buffer.from('aa'), Buffer.from('aa'))).toBe(true) |
| 33 | + expect(nodePath.posix.join('a', 'b')).toBe('a/b') |
| 34 | + expect(fs.existsSync(import.meta.filename)).toBe(true) |
| 35 | +}) |
| 36 | +
|
| 37 | +test('dynamic imports', async () => { |
| 38 | + const os = await import('node:os') |
| 39 | + const { basename } = await import('path') |
| 40 | + expect(os.platform()).toBe(process.platform) |
| 41 | + expect(basename('/a/b.js')).toBe('b.js') |
| 42 | +}) |
| 43 | +`, |
| 44 | + 'mock.test.js': ` |
| 45 | +import os from 'os' |
| 46 | +import { platform } from 'node:os' |
| 47 | +import { readFileSync } from 'node:fs' |
| 48 | +import { expect, test, vi } from 'vitest' |
| 49 | +
|
| 50 | +vi.mock('os') |
| 51 | +vi.mock('node:fs', () => ({ readFileSync: () => 'mocked' })) |
| 52 | +
|
| 53 | +test('automock', () => { |
| 54 | + expect(vi.isMockFunction(os.platform)).toBe(true) |
| 55 | + expect(vi.isMockFunction(platform)).toBe(true) |
| 56 | +}) |
| 57 | +
|
| 58 | +test('factory', () => { |
| 59 | + expect(readFileSync('file')).toBe('mocked') |
| 60 | +}) |
| 61 | +
|
| 62 | +test('importActual', async () => { |
| 63 | + const actual = await vi.importActual('node:os') |
| 64 | + expect(actual.platform()).toBe(process.platform) |
| 65 | +}) |
| 66 | +`, |
| 67 | + }, |
| 68 | + { environment: './client-env.js' }, |
| 69 | + ) |
| 70 | + |
| 71 | + expect(stderr).toBe('') |
| 72 | + expect(testTree()).toMatchInlineSnapshot(` |
| 73 | + { |
| 74 | + "basic.test.js": { |
| 75 | + "dynamic imports": "passed", |
| 76 | + "static imports": "passed", |
| 77 | + }, |
| 78 | + "mock.test.js": { |
| 79 | + "automock": "passed", |
| 80 | + "factory": "passed", |
| 81 | + "importActual": "passed", |
| 82 | + }, |
| 83 | + } |
| 84 | + `) |
| 85 | + |
| 86 | + const graph = await getModuleGraph(ctx!, '', `${root}/basic.test.js`, 'client') |
| 87 | + expect(graph.externalized.filter((id) => !id.includes('/'))).toMatchInlineSnapshot(` |
| 88 | + [ |
| 89 | + "node:crypto", |
| 90 | + "node:path", |
| 91 | + "fs", |
| 92 | + "node:os", |
| 93 | + "path", |
| 94 | + ] |
| 95 | + `) |
| 96 | + expect(graph.inlined.map((id) => id.replace(root, '<root>'))).toMatchInlineSnapshot(` |
| 97 | + [ |
| 98 | + "<root>/basic.test.js", |
| 99 | + ] |
| 100 | + `) |
| 101 | + }, |
| 102 | +) |
0 commit comments