Skip to content

Commit

Permalink
chore(test/browser): extract runVitest helper
Browse files Browse the repository at this point in the history
This is in preparation for adding a new test file which will run the
tests with config.base set to confirm the fix for vitest-dev#4686.
  • Loading branch information
mbland committed Dec 7, 2023
1 parent 7006bb3 commit cca4b87
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
26 changes: 26 additions & 0 deletions test/browser/specs/run-vitest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { readFile } from 'node:fs/promises'
import { execa } from 'execa'

const browser = process.env.BROWSER || (process.env.PROVIDER === 'playwright' ? 'chromium' : 'chrome')

export default async function runVitest(moreArgs = []) {
const argv = ['vitest', '--run', `--browser.name=${browser}`, '--browser.headless']
const { stderr, stdout } = await execa('npx', argv.concat(moreArgs), {
env: {
...process.env,
CI: 'true',
NO_COLOR: 'true',
},
reject: false,
})
const browserResult = await readFile('./browser.json', 'utf-8')
const browserResultJson = JSON.parse(browserResult)

const getPassed = results => results.filter(result => result.status === 'passed')
const getFailed = results => results.filter(result => result.status === 'failed')

const passedTests = getPassed(browserResultJson.testResults)
const failedTests = getFailed(browserResultJson.testResults)

return { stderr, stdout, browserResultJson, passedTests, failedTests }
}
29 changes: 8 additions & 21 deletions test/browser/specs/runner.test.mjs
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
import assert from 'node:assert'
import { readFile } from 'node:fs/promises'
import test from 'node:test'
import { execa } from 'execa'
import runVitest from './run-vitest.mjs'

const browser = process.env.BROWSER || (process.env.PROVIDER === 'playwright' ? 'chromium' : 'chrome')

const { stderr, stdout } = await execa('npx', ['vitest', '--run', `--browser.name=${browser}`, '--browser.headless'], {
env: {
...process.env,
CI: 'true',
NO_COLOR: 'true',
},
reject: false,
})

const browserResult = await readFile('./browser.json', 'utf-8')
const browserResultJson = JSON.parse(browserResult)

const getPassed = results => results.filter(result => result.status === 'passed')
const getFailed = results => results.filter(result => result.status === 'failed')

const passedTests = getPassed(browserResultJson.testResults)
const failedTests = getFailed(browserResultJson.testResults)
const {
stderr,
stdout,
browserResultJson,
passedTests,
failedTests,
} = await runVitest()

await test('tests are actually running', async () => {
assert.ok(browserResultJson.testResults.length === 8, 'Not all the tests have been run')
Expand Down

0 comments on commit cca4b87

Please sign in to comment.