|
| 1 | +import { readdir, readFile, rm, writeFile } from 'node:fs/promises' |
| 2 | +import { join } from 'node:path' |
| 3 | +import { beforeEach, expect, test } from 'vitest' |
| 4 | +import { runVitestCli } from '../../test-utils' |
| 5 | + |
| 6 | +const ARROW_DOWN = '\u001B[B' |
| 7 | +const ENTER = '\n' |
| 8 | + |
| 9 | +const cwd = 'fixtures/browser-init' |
| 10 | + |
| 11 | +beforeEach(async () => { |
| 12 | + await cleanup() |
| 13 | + return cleanup |
| 14 | + |
| 15 | + async function cleanup() { |
| 16 | + for (const file of await getFiles()) { |
| 17 | + if (file !== 'package.json') { |
| 18 | + await rm(`${cwd}/${file}`, { recursive: true }) |
| 19 | + } |
| 20 | + } |
| 21 | + await writeFile(`${cwd}/vitest.config.ts`, '{}', 'utf8') |
| 22 | + } |
| 23 | +}) |
| 24 | + |
| 25 | +test('initializes project', async () => { |
| 26 | + const { vitest } = await runVitestCli({ nodeOptions: { cwd } }, 'init', 'browser') |
| 27 | + |
| 28 | + await vitest.waitForStdout('This utility will help you set up a browser testing environment.') |
| 29 | + await vitest.waitForStdout('? Choose a language for your tests') |
| 30 | + vitest.write(ENTER) |
| 31 | + |
| 32 | + await vitest.waitForStdout('Choose a browser provider') |
| 33 | + vitest.write(`${ARROW_DOWN}${ARROW_DOWN}${ENTER}`) |
| 34 | + |
| 35 | + await vitest.waitForStdout('Choose a browser') |
| 36 | + vitest.write(ENTER) |
| 37 | + |
| 38 | + await vitest.waitForStdout('Choose your framework') |
| 39 | + vitest.write(ENTER) |
| 40 | + |
| 41 | + await vitest.waitForStdout('✔ All packages are already installed.') |
| 42 | + await vitest.waitForStdout('✔ Added "test:browser" script to your package.json.') |
| 43 | + await vitest.waitForStdout(`✔ Created example test file in ${join('vitest-example', 'HelloWorld.test.ts')}`) |
| 44 | + await vitest.waitForStdout('All done! Run your tests with pnpm test:browser') |
| 45 | + |
| 46 | + expect(await getFiles()).toMatchInlineSnapshot(` |
| 47 | + [ |
| 48 | + "package.json", |
| 49 | + "vitest-example", |
| 50 | + "vitest.config.ts", |
| 51 | + "vitest.workspace.ts", |
| 52 | + ] |
| 53 | + `) |
| 54 | + |
| 55 | + expect(await getFileContent('/vitest.workspace.ts')).toMatchInlineSnapshot(` |
| 56 | + "import { defineWorkspace } from 'vitest/config' |
| 57 | +
|
| 58 | + export default defineWorkspace([ |
| 59 | + // If you want to keep running your existing tests in Node.js, uncomment the next line. |
| 60 | + // 'vitest.config.ts', |
| 61 | + { |
| 62 | + extends: 'vitest.config.ts', |
| 63 | + test: { |
| 64 | + browser: { |
| 65 | + enabled: true, |
| 66 | + provider: 'preview', |
| 67 | + instances: [ |
| 68 | + ], |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + ]) |
| 73 | + " |
| 74 | + `) |
| 75 | + |
| 76 | + expect(await getFiles('/vitest-example')).toMatchInlineSnapshot(` |
| 77 | + [ |
| 78 | + "HelloWorld.test.ts", |
| 79 | + "HelloWorld.ts", |
| 80 | + ] |
| 81 | + `) |
| 82 | +}) |
| 83 | + |
| 84 | +async function getFiles(subDir = '') { |
| 85 | + return await readdir(cwd + subDir) |
| 86 | +} |
| 87 | + |
| 88 | +async function getFileContent(subDir = '') { |
| 89 | + return await readFile(cwd + subDir, 'utf8') |
| 90 | +} |
0 commit comments