Skip to content

Commit

Permalink
Merge branch 'main' into docs-timers-toFake
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 26, 2024
2 parents e22e4b0 + fee7d8b commit 037a3a2
Show file tree
Hide file tree
Showing 41 changed files with 405 additions and 82 deletions.
14 changes: 7 additions & 7 deletions docs/api/index.md
Expand Up @@ -940,7 +940,7 @@ These hooks will throw an error if they are called outside of the test body.

This hook is always called after the test has finished running. It is called after `afterEach` hooks since they can influence the test result. It receives a `TaskResult` object with the current test result.

```ts
```ts {1,5}
import { onTestFinished, test } from 'vitest'

test('performs a query', () => {
Expand All @@ -953,12 +953,12 @@ test('performs a query', () => {
::: warning
If you are running tests concurrently, you should always use `onTestFinished` hook from the test context since Vitest doesn't track concurrent tests in global hooks:

```ts
```ts {3,5}
import { test } from 'vitest'

test.concurrent('performs a query', (t) => {
test.concurrent('performs a query', ({ onTestFinished }) => {
const db = connectDb()
t.onTestFinished(() => db.close())
onTestFinished(() => db.close())
db.query('SELECT * FROM users')
})
```
Expand Down Expand Up @@ -993,7 +993,7 @@ test('performs an organization query', async () => {

This hook is called only after the test has failed. It is called after `afterEach` hooks since they can influence the test result. It receives a `TaskResult` object with the current test result. This hook is useful for debugging.

```ts
```ts {1,5-7}
import { onTestFailed, test } from 'vitest'

test('performs a query', () => {
Expand All @@ -1008,10 +1008,10 @@ test('performs a query', () => {
::: warning
If you are running tests concurrently, you should always use `onTestFailed` hook from the test context since Vitest doesn't track concurrent tests in global hooks:

```ts
```ts {3,5-7}
import { test } from 'vitest'

test.concurrent('performs a query', (t) => {
test.concurrent('performs a query', ({ onTestFailed }) => {
const db = connectDb()
onTestFailed((result) => {
console.log(result.errors)
Expand Down
19 changes: 5 additions & 14 deletions docs/config/index.md
Expand Up @@ -1736,18 +1736,10 @@ Test above this limit will be queued to run when available slot appears.

### cache<NonProjectOption />

- **Type**: `false | { dir? }`
- **Type**: `false`
- **CLI**: `--no-cache`, `--cache=false`

Options to configure Vitest cache policy. At the moment Vitest stores cache for test results to run the longer and failed tests first.

#### cache.dir

- **Type**: `string`
- **Default**: `node_modules/.vitest`
- **CLI**: `--cache.dir=./cache`

Path to cache directory.
Use this option if you want to disable the cache feature. At the moment Vitest stores cache for test results to run the longer and failed tests first.

### sequence

Expand Down Expand Up @@ -1964,7 +1956,7 @@ Retry the test specific number of times if it fails.

### onConsoleLog<NonProjectOption />

- **Type**: `(log: string, type: 'stdout' | 'stderr') => false | void`
- **Type**: `(log: string, type: 'stdout' | 'stderr') => boolean | void`

Custom handler for `console.log` in tests. If you return `false`, Vitest will not print the log to the console.

Expand All @@ -1975,9 +1967,8 @@ import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
onConsoleLog(log: string, type: 'stdout' | 'stderr'): false | void {
if (log === 'message from third party library' && type === 'stdout')
return false
onConsoleLog(log: string, type: 'stdout' | 'stderr'): boolean | void {
return !(log === 'message from third party library' && type === 'stdout')
},
},
})
Expand Down
5 changes: 3 additions & 2 deletions examples/playwright/test/basic.test.ts
Expand Up @@ -20,9 +20,10 @@ describe.runIf(process.platform !== 'win32')('basic', async () => {
})

afterAll(async () => {
await browser.close()
// hook timed out and we already have another error
await browser?.close()
await new Promise<void>((resolve, reject) => {
server.httpServer.close(error => error ? reject(error) : resolve())
server?.httpServer.close(error => error ? reject(error) : resolve())
})
})

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "@vitest/monorepo",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"private": true,
"packageManager": "pnpm@8.10.3",
"description": "Next generation testing framework powered by Vite",
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/package.json
@@ -1,7 +1,7 @@
{
"name": "@vitest/browser",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "Browser running for Vitest",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/client/runner.ts
Expand Up @@ -127,7 +127,7 @@ async function updateFilesLocations(files: File[]) {
if (task.location) {
const { line, column } = originalPositionFor(traceMap, task.location)
if (line != null && column != null)
task.location = { line, column: column + 1 }
task.location = { line, column: task.each ? column : column + 1 }
}
if ('tasks' in task)
task.tasks.forEach(updateLocation)
Expand Down
2 changes: 1 addition & 1 deletion packages/coverage-istanbul/package.json
@@ -1,7 +1,7 @@
{
"name": "@vitest/coverage-istanbul",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "Istanbul coverage provider for Vitest",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/coverage-v8/package.json
@@ -1,7 +1,7 @@
{
"name": "@vitest/coverage-v8",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "V8 coverage provider for Vitest",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/package.json
@@ -1,7 +1,7 @@
{
"name": "@vitest/expect",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "Jest's expect matchers as a Chai plugin",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/package.json
@@ -1,7 +1,7 @@
{
"name": "@vitest/runner",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "Vitest test runner",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
Expand Down
16 changes: 12 additions & 4 deletions packages/runner/src/suite.ts
Expand Up @@ -153,7 +153,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
Error.stackTraceLimit = 10
const error = new Error('stacktrace').stack!
Error.stackTraceLimit = limit
const stack = findStackTrace(error)
const stack = findStackTrace(error, task.each ?? false)
if (stack)
task.location = stack
}
Expand Down Expand Up @@ -226,7 +226,9 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
if (stack) {
suite.location = {
line: stack.line,
column: stack.column,
// because source map is boundary based, this line leads to ")" in test.each()[(]),
// but it should be the next opening bracket - here we assume it's on the same line
column: each ? stack.column + 1 : stack.column,
}
}
}
Expand Down Expand Up @@ -430,7 +432,7 @@ function formatTemplateString(cases: any[], args: any[]): any[] {
return res
}

function findStackTrace(error: string) {
function findStackTrace(error: string, each: boolean) {
// first line is the error message
// and the first 3 stacks are always from the collector
const lines = error.split('\n').slice(4)
Expand All @@ -439,7 +441,13 @@ function findStackTrace(error: string) {
if (stack && stack.file === getTestFilepath()) {
return {
line: stack.line,
column: stack.column,
/**
* test.each([1, 2])('name')
* ^ leads here, but should
* ^ lead here
* in source maps it's the same boundary, so it just points to the start of it
*/
column: each ? stack.column + 1 : stack.column,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/snapshot/package.json
@@ -1,7 +1,7 @@
{
"name": "@vitest/snapshot",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "Vitest snapshot manager",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
Expand Down
2 changes: 1 addition & 1 deletion packages/spy/package.json
@@ -1,7 +1,7 @@
{
"name": "@vitest/spy",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "Lightweight Jest compatible spy implementation",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
@@ -1,7 +1,7 @@
{
"name": "@vitest/ui",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "UI for Vitest",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
@@ -1,7 +1,7 @@
{
"name": "@vitest/utils",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "Shared Vitest utility functions",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-node/package.json
@@ -1,7 +1,7 @@
{
"name": "vite-node",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "Vite as Node.js runtime",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/package.json
@@ -1,7 +1,7 @@
{
"name": "vitest",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "Next generation testing framework powered by Vite",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
Expand Down
10 changes: 5 additions & 5 deletions packages/vitest/src/api/setup.ts
Expand Up @@ -200,7 +200,7 @@ export class WebSocketReporter implements Reporter {
if (this.clients.size === 0)
return
this.clients.forEach((client) => {
client.onCollected?.(files).catch(noop)
client.onCollected?.(files)?.catch?.(noop)
})
}

Expand All @@ -222,25 +222,25 @@ export class WebSocketReporter implements Reporter {
})

this.clients.forEach((client) => {
client.onTaskUpdate?.(packs).catch(noop)
client.onTaskUpdate?.(packs)?.catch?.(noop)
})
}

onFinished(files?: File[], errors?: unknown[]) {
this.clients.forEach((client) => {
client.onFinished?.(files, errors).catch(noop)
client.onFinished?.(files, errors)?.catch?.(noop)
})
}

onFinishedReportCoverage() {
this.clients.forEach((client) => {
client.onFinishedReportCoverage?.().catch(noop)
client.onFinishedReportCoverage?.()?.catch?.(noop)
})
}

onUserConsoleLog(log: UserConsoleLog) {
this.clients.forEach((client) => {
client.onUserConsoleLog?.(log).catch(noop)
client.onUserConsoleLog?.(log)?.catch?.(noop)
})
}
}
4 changes: 2 additions & 2 deletions packages/vitest/src/node/cache/index.ts
Expand Up @@ -21,8 +21,8 @@ export class VitestCache {
return this.stats.getStats(key)
}

static resolveCacheDir(root: string, dir: string | undefined, projectName: string | undefined) {
const baseDir = slash(dir || 'node_modules/.vitest')
static resolveCacheDir(root: string, dir?: string, projectName?: string) {
const baseDir = slash(dir || 'node_modules/.vite/vitest')
return projectName
? resolve(root, baseDir, crypto.createHash('md5').update(projectName, 'utf-8').digest('hex'))
: resolve(root, baseDir)
Expand Down
9 changes: 4 additions & 5 deletions packages/vitest/src/node/cli/cli-config.ts
Expand Up @@ -549,14 +549,13 @@ export const cliOptionsConfig: VitestCLIOptions = {
description: 'Enable cache',
argument: '', // allow only boolean
subcommands: {
dir: {
description: 'Path to the cache directory',
argument: '<path>',
normalize: true,
},
dir: null,
},
default: true,
// cache can only be "false" or an object
transform(cache) {
if (typeof cache !== 'boolean' && cache)
throw new Error('--cache.dir is deprecated')
if (cache)
return {}
return cache
Expand Down
18 changes: 15 additions & 3 deletions packages/vitest/src/node/config.ts
Expand Up @@ -446,9 +446,21 @@ export function resolveConfig(
resolved.css.modules.classNameStrategy ??= 'stable'
}

resolved.cache ??= { dir: '' }
if (resolved.cache)
resolved.cache.dir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir, resolved.name)
if (resolved.cache !== false) {
let cacheDir = VitestCache.resolveCacheDir('', resolve(viteConfig.cacheDir, 'vitest'), resolved.name)

if (resolved.cache && resolved.cache.dir) {
console.warn(
c.yellow(
`${c.inverse(c.yellow(' Vitest '))} "cache.dir" is deprecated, use Vite's "cacheDir" instead if you want to change the cache director. Note caches will be written to "cacheDir\/vitest"`,
),
)

cacheDir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir, resolved.name)
}

resolved.cache = { dir: cacheDir }
}

resolved.sequence ??= {} as any
if (resolved.sequence.shuffle && typeof resolved.sequence.shuffle === 'object') {
Expand Down

0 comments on commit 037a3a2

Please sign in to comment.