Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use startVitest instead of execa #5439

Merged
merged 1 commit into from Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions test/changed/tests/related.test.ts
@@ -1,10 +1,14 @@
import { join } from 'node:path'
import { expect, it } from 'vitest'

import { runVitestCli } from '../../test-utils'
import { runVitest } from '../../test-utils'

it('related correctly runs only related tests', async () => {
const { stdout, stderr } = await runVitestCli('related', join(process.cwd(), 'fixtures/related/src/sourceA.ts'), '--root', join(process.cwd(), './fixtures/related'), '--globals', '--no-watch')
const { stdout, stderr } = await runVitest({
related: 'src/sourceA.ts',
root: './fixtures/related',
globals: true,
})

expect(stderr).toBe('')
expect(stdout).toContain('3 passed')
expect(stdout).toContain('related.test.ts')
Expand Down
16 changes: 12 additions & 4 deletions test/changed/tests/workspaceRelated.test.ts
@@ -1,17 +1,25 @@
import { join } from 'node:path'
import { expect, it } from 'vitest'

import { editFile, resolvePath, runVitestCli } from '../../test-utils'
import { editFile, resolvePath, runVitest } from '../../test-utils'

it('doesn\'t run any test in a workspace because there are no changes', async () => {
const { stdout } = await runVitestCli('--root', join(process.cwd(), './fixtures/workspace'), '--no-watch', '--changed')
const { stdout } = await runVitest({
changed: true,
root: './fixtures/workspace',
})

expect(stdout).toContain('No test files found, exiting with code 0')
})

// Fixes #4674
it('related correctly runs only related tests inside a workspace', async () => {
editFile(resolvePath(import.meta.url, '../fixtures/workspace/packages/packageA/index.js'), content => `${content}\n`)
const { stdout, stderr } = await runVitestCli('--root', join(process.cwd(), './fixtures/workspace'), '--no-watch', '--changed')

const { stdout, stderr } = await runVitest({
changed: true,
root: './fixtures/workspace',
})

expect(stderr).toBe('')
expect(stdout).toContain('1 passed')
expect(stdout).toContain('packageA')
Expand Down
13 changes: 5 additions & 8 deletions test/cli/test/exclude.test.ts
@@ -1,15 +1,12 @@
import { expect, test } from 'vitest'

import { runVitestCli } from '../../test-utils'
import { runVitest } from '../../test-utils'

test('should still test math.test.ts', async () => {
const { stderr, stdout } = await runVitestCli(
'run',
'--config',
'fixtures/exclude/vitest.exclude.config.ts',
'--exclude',
'fixtures/exclude/string.test.ts',
)
const { stderr, stdout } = await runVitest({
config: 'fixtures/exclude/vitest.exclude.config.ts',
exclude: ['fixtures/exclude/string.test.ts'],
})

expect(stdout).toContain(`✓ fixtures/exclude/math.test.ts`)
expect(stdout).not.toContain(`string.test.ts`)
Expand Down
14 changes: 6 additions & 8 deletions test/cli/test/project.test.ts
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'
import { runVitest } from '../../test-utils'

test.each([
{ pattern: 'project_1', expected: ['project_1'] },
Expand All @@ -8,13 +8,11 @@ test.each([
{ pattern: 'project*', expected: ['project_1', 'project_2'] },
{ pattern: 'space*', expected: ['space_1'] },
])('should match projects correctly: $pattern', async ({ pattern, expected }) => {
const { stdout, stderr } = await runVitestCli(
'run',
'--root',
'fixtures/project',
'--project',
pattern,
)
const { stdout, stderr } = await runVitest({
root: 'fixtures/project',
reporters: ['basic'],
project: pattern,
})

expect(stderr).toBeFalsy()
expect(stdout).toBeTruthy()
Expand Down
11 changes: 9 additions & 2 deletions test/config/test/retry.test.ts
@@ -1,18 +1,25 @@
import { describe, expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'
import { runVitest } from '../../test-utils'

function run(testNamePattern: string) {
return runVitestCli('run', 'fixtures/retry/retry.test.ts', '-c', 'fixtures/retry/vitest.config.ts', '-t', testNamePattern)
return runVitest({
include: ['fixtures/retry/retry.test.ts'],
config: 'fixtures/retry/vitest.config.ts',
reporters: ['basic'],
testNamePattern,
})
}

describe('retry', () => {
test('should passed', async () => {
const { stdout } = await run('should passed')

expect(stdout).toContain('1 passed')
})

test('retry but still failed', async () => {
const { stdout } = await run('retry but still failed')

expect(stdout).toContain('expected 1 to be 4')
expect(stdout).toContain('expected 2 to be 4')
expect(stdout).toContain('expected 3 to be 4')
Expand Down
7 changes: 5 additions & 2 deletions test/config/test/workspace.test.ts
@@ -1,8 +1,11 @@
import { expect, it } from 'vitest'
import { runVitestCli } from '../../test-utils'
import { runVitest } from '../../test-utils'

it('correctly runs workspace tests when workspace config path is specified', async () => {
const { stderr, stdout } = await runVitestCli('run', '--root', 'fixtures/workspace', '--workspace', './nested/e2e.projects.js')
const { stderr, stdout } = await runVitest({
root: 'fixtures/workspace',
workspace: 'nested/e2e.projects.js',
})
expect(stderr).toBe('')
expect(stdout).toContain('1 + 1 = 2')
expect(stdout).not.toContain('2 + 2 = 4')
Expand Down
2 changes: 1 addition & 1 deletion test/reporters/package.json
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"private": true,
"scripts": {
"test": "NO_COLOR=1 GITHUB_ACTIONS=false vitest run"
"test": "vitest run"
},
"devDependencies": {
"flatted": "^3.2.9",
Expand Down
2 changes: 1 addition & 1 deletion test/reporters/tests/console.test.ts
Expand Up @@ -9,7 +9,7 @@ test('should print logs correctly', async () => {
expect(stdout).toBeTruthy()
expect(stderr).toBeTruthy()

expect(stdout).toContain(
expect(stdout.replace('\n ✓ console.test.ts > suite > snested suite > test', '')).toContain(
`
stdout | console.test.ts > suite > nested suite
beforeAll
Expand Down
12 changes: 4 additions & 8 deletions test/reporters/tests/custom-reporter.spec.ts
Expand Up @@ -8,18 +8,13 @@ const customTsReporterPath = resolve(__dirname, '../src/custom-reporter.ts')
const customJSReporterPath = resolve(__dirname, '../src/custom-reporter.js')
const root = resolve(__dirname, '..')

async function run(...runOptions: string[]): Promise<string> {
const vitest = await runVitestCli({ cwd: root, windowsHide: false }, 'run', ...runOptions)

return vitest.stdout
}

async function runWithRetry(...runOptions: string[]) {
const count = 3

for (let i = count; i >= 0; i--) {
try {
return await run(...runOptions)
const vitest = await runVitestCli({ cwd: root, windowsHide: false }, 'run', ...runOptions)
return vitest.stdout
}
catch (e) {
if (i <= 0)
Expand All @@ -41,7 +36,8 @@ describe('custom reporters', () => {
}, TIMEOUT)

test('load no base on root custom reporter instances defined in configuration works', async () => {
const stdout = await runWithRetry('--config', './reportTest2/custom-reporter-path.vitest.config.ts')
const { stdout, stderr } = await runVitest({ reporters: 'none', config: './reportTest2/custom-reporter-path.vitest.config.ts' })
expect(stderr).toBe('')
expect(stdout).includes('hello from custom reporter')
}, TIMEOUT)

Expand Down
37 changes: 20 additions & 17 deletions test/reporters/tests/default.test.ts
@@ -1,37 +1,40 @@
import path from 'pathe'
import { describe, expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'

const resolve = (id = '') => path.resolve(__dirname, '../fixtures/default', id)
async function run(fileFilter: string[], watch = false, ...args: string[]) {
return runVitestCli(
...fileFilter,
'--root',
resolve(),
watch ? '--watch' : '--run',
...args,
)
}
import { runVitest, runVitestCli } from '../../test-utils'

describe('default reporter', async () => {
test('normal', async () => {
const { stdout } = await run(['b1.test.ts', 'b2.test.ts'])
const { stdout } = await runVitest({
include: ['b1.test.ts', 'b2.test.ts'],
root: 'fixtures/default',
reporters: 'none',
})

expect(stdout).contain('✓ b2 test')
expect(stdout).not.contain('✓ nested b1 test')
expect(stdout).contain('× b failed test')
})

test('show full test suite when only one file', async () => {
const { stdout } = await run(['a.test.ts'])
const { stdout } = await runVitest({
include: ['a.test.ts'],
root: 'fixtures/default',
reporters: 'none',
})

expect(stdout).contain('✓ a1 test')
expect(stdout).contain('✓ nested a3 test')
expect(stdout).contain('× a failed test')
expect(stdout).contain('nested a failed 1 test')
})

test('rerun should undo', async () => {
const vitest = await run([], true, '-t', 'passed')

const vitest = await runVitestCli(
'--root',
'fixtures/default',
'--watch',
'-t',
'passed',
)
vitest.resetOutput()

// one file
Expand Down
4 changes: 2 additions & 2 deletions test/reporters/tests/html.test.ts
Expand Up @@ -14,7 +14,7 @@ describe('html reporter', async () => {
it('resolves to "passing" status for test file "all-passing-or-skipped"', async () => {
const [expected, testFile, basePath] = ['passing', 'all-passing-or-skipped', 'html/all-passing-or-skipped']

await runVitest({ reporters: 'html', outputFile: `${basePath}/index.html`, root }, [testFile])
await runVitest({ reporters: 'html', outputFile: `${basePath}/index.html`, root, env: { NO_COLOR: '1' } }, [testFile])

const metaJsonGzipeed = fs.readFileSync(resolve(root, `${basePath}/html.meta.json.gz`))
const metaJson = zlib.gunzipSync(metaJsonGzipeed).toString('utf-8')
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('html reporter', async () => {
it('resolves to "failing" status for test file "json-fail"', async () => {
const [expected, testFile, basePath] = ['failing', 'json-fail.test', 'html/fail']

await runVitest({ reporters: 'html', outputFile: `${basePath}/index.html`, root }, [testFile])
await runVitest({ reporters: 'html', outputFile: `${basePath}/index.html`, root, env: { NO_COLOR: '1' } }, [testFile])

const metaJsonGzipped = fs.readFileSync(resolve(root, `${basePath}/html.meta.json.gz`))
const metaJson = zlib.gunzipSync(metaJsonGzipped).toString('utf-8')
Expand Down
9 changes: 7 additions & 2 deletions test/reporters/tests/verbose.test.ts
@@ -1,8 +1,13 @@
import { expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'
import { runVitest } from '../../test-utils'

test('duration', async () => {
const result = await runVitestCli({ env: { CI: '1' } }, '--root=fixtures/duration', '--reporter=verbose')
const result = await runVitest({
root: 'fixtures/duration',
reporters: 'verbose',
env: { CI: '1' },
})

const output = result.stdout.replaceAll(/\d+ms/g, '[...]ms')
expect(output).toContain(`
✓ basic.test.ts > fast
Expand Down
1 change: 1 addition & 0 deletions test/reporters/vitest.config.ts
Expand Up @@ -3,6 +3,7 @@ import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
exclude: ['node_modules', 'fixtures', 'dist'],
reporters: ['verbose'],
testTimeout: 100000,
chaiConfig: {
truncateThreshold: 0,
Expand Down
7 changes: 5 additions & 2 deletions test/run/test/custom-pool.test.ts
@@ -1,8 +1,11 @@
import { expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'
import { runVitest } from '../../test-utils'

test('can run custom pools with Vitest', async () => {
const vitest = await runVitestCli('--run', '--root', 'pool-custom-fixtures')
const vitest = await runVitest({
root: 'pool-custom-fixtures',
reporters: ['basic'],
})

expect(vitest.stderr).toMatchInlineSnapshot(`
"[pool] printing: options are respected
Expand Down
7 changes: 5 additions & 2 deletions test/test-utils/index.ts
Expand Up @@ -23,10 +23,13 @@ export async function runVitest(config: UserConfig, cliFilters: string[] = [], m

let vitest: Vitest | undefined
try {
const { reporters, ...rest } = config

vitest = await startVitest(mode, cliFilters, {
watch: false,
reporters: ['verbose'],
...config,
// "none" can be used to disable passing "reporter" option so that default value is used (it's not same as reporters: ["default"])
...(reporters === 'none' ? {} : reporters ? { reporters } : { reporters: ['verbose'] }),
...rest,
}, viteOverrides)
}
catch (e: any) {
Expand Down
7 changes: 3 additions & 4 deletions test/ws-api/tests/server-url.test.ts
@@ -1,18 +1,17 @@
import { join } from 'node:path'
import { expect, it } from 'vitest'

import { runVitestCli } from '../../test-utils'
import { runVitest } from '../../test-utils'

it('api server-url http', async () => {
delete process.env.TEST_HTTPS
const { stdout } = await runVitestCli('run', '--root', join(process.cwd(), './fixtures/server-url'), '--api')
const { stdout } = await runVitest({ root: 'fixtures/server-url', api: true })
expect(stdout).toContain('API started at http://localhost:51204/')
expect(stdout).toContain('Test Files 1 passed')
})

it('api server-url https', async () => {
process.env.TEST_HTTPS = '1'
const { stdout } = await runVitestCli('run', '--root', join(process.cwd(), './fixtures/server-url'), '--api')
const { stdout } = await runVitest({ root: 'fixtures/server-url', api: true })
expect(stdout).toContain('API started at https://localhost:51204/')
expect(stdout).toContain('Test Files 1 passed')
})
Expand Down