Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vitest-dev/vitest
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.21.0
Choose a base ref
...
head repository: vitest-dev/vitest
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.21.1
Choose a head ref
  • 8 commits
  • 23 files changed
  • 7 contributors

Commits on Aug 6, 2022

  1. feat(types): better local test context support (#1805)

    * feat(types): better local test context support
    
    * feat(types): fix beforeEach and afterEach
    
    * test: add unit test for local context
    Tanimodori authored Aug 6, 2022
    Copy the full SHA
    426503c View commit details
  2. Copy the full SHA
    95c1d34 View commit details
  3. Copy the full SHA
    33b5f83 View commit details

Commits on Aug 7, 2022

  1. Copy the full SHA
    0ae8e19 View commit details

Commits on Aug 8, 2022

  1. chore(deps): update all non-major dependencies (#1820)

    Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
    renovate[bot] authored Aug 8, 2022
    Copy the full SHA
    9e3cac4 View commit details
  2. Copy the full SHA
    7574759 View commit details
  3. docs: fix tiny typo on landing page (#1815)

    * Fix typo on landing page
    
    * Update docs/index.md
    
    Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
    
    Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
    marcomuser and dammy001 authored Aug 8, 2022
    Copy the full SHA
    ccbf0ad View commit details

Commits on Aug 9, 2022

  1. chore: release v0.21.1

    sheremet-va committed Aug 9, 2022
    Copy the full SHA
    6aee8cd View commit details
2 changes: 1 addition & 1 deletion docs/guide/cli.md
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ Alias to `vitest watch`.

### `vitest related`

Run only tests that cover a list of source files. Works with static lazy imports, but not the dynamic ones. All files should be relative to root folder.
Run only tests that cover a list of source files. Works with static imports (e.g., `import('./index.ts')` or `import index from './index.ts`), but not the dynamic ones (e.g., `import(filepath)`). All files should be relative to root folder.

Useful to run with [`lint-staged`](https://github.com/okonet/lint-staged) or with your CI setup.

2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ features:
- title: Vite Powered
details: Reuse Vite's config, transformers, resolvers, and plugins - consistant across your app and tests.
- title: Jest Compatible
details: Expect, snapshot, coverage, and more - migrate from Jest is straightforward.
details: Expect, snapshot, coverage, and more - migrating from Jest is straightforward.
- title: Smart & instant watch mode
details: Only rerun the related changes, just like HMR for tests!
- title: ESM, TypeScript, JSX
10 changes: 5 additions & 5 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -11,21 +11,21 @@
"prefetch": "esno .vitepress/scripts/fetch-avatars.ts"
},
"dependencies": {
"@vueuse/core": "^9.0.2",
"@vueuse/core": "^9.1.0",
"vue": "^3.2.37"
},
"devDependencies": {
"@iconify-json/carbon": "^1.1.7",
"@types/node": "^17.0.35",
"@unocss/reset": "^0.45.1",
"@unocss/reset": "^0.45.5",
"@vitejs/plugin-vue": "^3.0.1",
"esno": "^0.14.1",
"fast-glob": "^3.2.11",
"https-localhost": "^4.7.1",
"unocss": "^0.45.1",
"unplugin-vue-components": "^0.21.2",
"unocss": "^0.45.5",
"unplugin-vue-components": "^0.22.0",
"vite": "2.9.14",
"vite-plugin-pwa": "0.12.0",
"vite-plugin-pwa": "0.12.3",
"vitepress": "^1.0.0-alpha.4",
"workbox-window": "^6.5.4"
}
1 change: 1 addition & 0 deletions examples/mocks/src/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'a default'
1 change: 1 addition & 0 deletions examples/mocks/src/example.ts
Original file line number Diff line number Diff line change
@@ -25,3 +25,4 @@ export const number = 123
export const string = 'baz'
export const boolean = true
export const symbol = Symbol.for('a.b.c')
export default 'a default'
24 changes: 22 additions & 2 deletions examples/mocks/test/factory.test.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,9 @@ import logger from '../src/log'
vi
.mock('../src/example', () => ({
mocked: true,
then: 'a then export',
square: (a: any, b: any) => a + b,
asyncSquare: async (a: any, b: any) => Promise.resolve(a + b),
}))

// doesn't think comments are mocks
@@ -40,10 +43,27 @@ vi.mock('../src/log.ts', async () => {
}
})

vi.mock('../src/default.ts', () => null)

describe('mocking with factory', () => {
test('successfuly mocked', () => {
test('missing exports on mock', () => {
expect(() => example.default).toThrowError('[vitest] No "default" export is defined on the "mock:/src/example.ts"')
expect(() => example.boolean).toThrowError('[vitest] No "boolean" export is defined on the "mock:/src/example.ts"')
expect(() => example.object).toThrowError('[vitest] No "object" export is defined on the "mock:/src/example.ts"')
expect(() => example.array).toThrowError('[vitest] No "array" export is defined on the "mock:/src/example.ts"')
expect(() => example.someClasses).toThrowError('[vitest] No "someClasses" export is defined on the "mock:/src/example.ts"')
})

it('non-object return on factory gives error', async () => {
await expect(() => import('../src/default').then(m => m.default)).rejects
.toThrowError('[vitest] vi.mock(path: string, factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?')
})

test('defined exports on mock', async () => {
expect((example as any).then).toBe('a then export')
expect((example as any).mocked).toBe(true)
expect(example.boolean).toBeUndefined()
expect(example.square(2, 3)).toBe(5)
expect(example.asyncSquare(2, 3)).resolves.toBe(5)
})

test('successfuly with actual', () => {
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@vitest/monorepo",
"type": "module",
"version": "0.21.0",
"version": "0.21.1",
"private": true,
"packageManager": "pnpm@7.8.0",
"packageManager": "pnpm@7.9.0",
"description": "A blazing fast unit test framework powered by Vite",
"scripts": {
"ci": "ni && nr typecheck && nr lint && nr build && nr test:all",
@@ -34,7 +34,7 @@
"@antfu/eslint-config": "^0.25.2",
"@antfu/ni": "^0.17.2",
"@rollup/plugin-alias": "^3.1.9",
"@rollup/plugin-commonjs": "^22.0.1",
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@types/fs-extra": "^9.0.13",
@@ -46,8 +46,8 @@
"bumpp": "^8.2.1",
"c8": "^7.12.0",
"cross-env": "^7.0.3",
"esbuild": "~0.14.51",
"eslint": "^8.20.0",
"esbuild": "~0.14.53",
"eslint": "^8.21.0",
"esno": "^0.14.1",
"fast-glob": "^3.2.11",
"if-node-version": "^1.1.1",
@@ -57,7 +57,7 @@
"npm-run-all": "^4.1.5",
"ohmyfetch": "^0.4.18",
"pathe": "^0.2.0",
"pnpm": "7.8.0",
"pnpm": "7.9.0",
"rimraf": "^3.0.2",
"rollup": "^2.77.2",
"rollup-plugin-dts": "^4.2.2",
6 changes: 3 additions & 3 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vitest/browser",
"type": "module",
"version": "0.21.0",
"version": "0.21.1",
"description": "Browser running for Vitest",
"repository": {
"type": "git",
@@ -34,8 +34,8 @@
},
"dependencies": {
"local-pkg": "^0.4.2",
"mlly": "^0.5.5",
"modern-node-polyfills": "^0.0.7",
"mlly": "^0.5.7",
"modern-node-polyfills": "^0.0.8",
"rollup-plugin-node-polyfills": "^0.2.1",
"sirv": "^2.0.2"
},
16 changes: 8 additions & 8 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vitest/ui",
"type": "module",
"version": "0.21.0",
"version": "0.21.1",
"description": "UI for Vitest",
"license": "MIT",
"repository": {
@@ -44,25 +44,25 @@
"@types/d3-force": "^3.0.3",
"@types/d3-selection": "^3.0.3",
"@types/ws": "^8.5.3",
"@unocss/reset": "^0.45.1",
"@unocss/reset": "^0.45.5",
"@vitejs/plugin-vue": "^3.0.1",
"@vitejs/plugin-vue-jsx": "^2.0.0",
"@vitest/ws-client": "workspace:*",
"@vueuse/core": "^9.0.2",
"@vueuse/core": "^9.1.0",
"ansi-to-html": "^0.7.2",
"birpc": "^0.2.3",
"codemirror": "^5.65.7",
"codemirror-theme-vars": "^0.1.1",
"cypress": "^10.3.1",
"d3-graph-controller": "^2.2.51",
"cypress": "^10.4.0",
"d3-graph-controller": "^2.2.53",
"flatted": "^3.2.6",
"floating-vue": "^2.0.0-y.0",
"picocolors": "^1.0.0",
"rollup": "^2.77.2",
"splitpanes": "^3.1.1",
"unocss": "^0.45.1",
"unplugin-auto-import": "^0.10.3",
"unplugin-vue-components": "^0.21.2",
"unocss": "^0.45.5",
"unplugin-auto-import": "^0.11.1",
"unplugin-vue-components": "^0.22.0",
"vite": "^3.0.4",
"vite-plugin-pages": "^0.25.0",
"vue": "^3.2.37",
4 changes: 2 additions & 2 deletions packages/vite-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-node",
"version": "0.21.0",
"version": "0.21.1",
"description": "Vite as Node.js runtime",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
@@ -71,7 +71,7 @@
},
"dependencies": {
"debug": "^4.3.4",
"mlly": "^0.5.5",
"mlly": "^0.5.7",
"pathe": "^0.2.0",
"vite": "^2.9.12 || ^3.0.0-0"
},
6 changes: 5 additions & 1 deletion packages/vite-node/src/client.ts
Original file line number Diff line number Diff line change
@@ -182,7 +182,11 @@ export class ViteNodeRunner {
const url = pathToFileURL(fsPath).href
const meta = { url }
const exports: any = Object.create(null)
exports[Symbol.toStringTag] = 'Module'
Object.defineProperty(exports, Symbol.toStringTag, {
value: 'Module',
enumerable: false,
configurable: false,
})

this.moduleCache.set(fsPath, { code: transformed, exports })

8 changes: 4 additions & 4 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vitest",
"version": "0.21.0",
"version": "0.21.1",
"description": "A blazing fast unit test framework powered by Vite",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
@@ -95,7 +95,7 @@
}
},
"dependencies": {
"@types/chai": "^4.3.1",
"@types/chai": "^4.3.3",
"@types/chai-subset": "^1.3.3",
"@types/node": "*",
"chai": "^4.3.6",
@@ -107,7 +107,7 @@
},
"devDependencies": {
"@antfu/install-pkg": "^0.1.0",
"@edge-runtime/vm": "1.1.0-beta.23",
"@edge-runtime/vm": "1.1.0-beta.26",
"@sinonjs/fake-timers": "^9.1.2",
"@types/diff": "^5.0.2",
"@types/jsdom": "^20.0.0",
@@ -131,7 +131,7 @@
"log-update": "^5.0.1",
"magic-string": "^0.26.2",
"micromatch": "^4.0.5",
"mlly": "^0.5.5",
"mlly": "^0.5.7",
"natural-compare": "^1.4.0",
"p-limit": "^4.0.0",
"pathe": "^0.2.0",
2 changes: 1 addition & 1 deletion packages/vitest/src/node/logger.ts
Original file line number Diff line number Diff line change
@@ -108,7 +108,7 @@ export class Logger {
async printUnhandledErrors(errors: unknown[]) {
const errorMessage = c.red(c.bold(
`\nVitest caught ${errors.length} unhandled error${errors.length > 1 ? 's' : ''} during the test run. This might cause false positive tests.`
+ '\nPlease, resolve all the errors to make sure your tests are not affected.',
+ '\nPlease, resolve all errors to make sure your tests are not affected.',
))
this.log(c.red(divider(c.bold(c.inverse(' Unhandled Errors ')))))
this.log(errorMessage)
2 changes: 1 addition & 1 deletion packages/vitest/src/node/reporters/tap.ts
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ export class TapReporter implements Reporter {

onInit(ctx: Vitest): void {
this.ctx = ctx
this.logger = new IndentedLogger(this.ctx.logger.log.bind(this.ctx))
this.logger = new IndentedLogger(ctx.logger.log.bind(ctx.logger))
}

static getComment(task: Task): string {
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/hooks.ts
Original file line number Diff line number Diff line change
@@ -5,5 +5,5 @@ import { getCurrentSuite } from './suite'
// suite hooks
export const beforeAll = (fn: SuiteHooks['beforeAll'][0], timeout?: number) => getCurrentSuite().on('beforeAll', withTimeout(fn, timeout ?? getDefaultHookTimeout(), true))
export const afterAll = (fn: SuiteHooks['afterAll'][0], timeout?: number) => getCurrentSuite().on('afterAll', withTimeout(fn, timeout ?? getDefaultHookTimeout(), true))
export const beforeEach = (fn: SuiteHooks['beforeEach'][0], timeout?: number) => getCurrentSuite().on('beforeEach', withTimeout(fn, timeout ?? getDefaultHookTimeout(), true))
export const afterEach = (fn: SuiteHooks['afterEach'][0], timeout?: number) => getCurrentSuite().on('afterEach', withTimeout(fn, timeout ?? getDefaultHookTimeout(), true))
export const beforeEach = <ExtraContext = {}>(fn: SuiteHooks<ExtraContext>['beforeEach'][0], timeout?: number) => getCurrentSuite<ExtraContext>().on('beforeEach', withTimeout(fn, timeout ?? getDefaultHookTimeout(), true))
export const afterEach = <ExtraContext = {}>(fn: SuiteHooks<ExtraContext>['afterEach'][0], timeout?: number) => getCurrentSuite<ExtraContext>().on('afterEach', withTimeout(fn, timeout ?? getDefaultHookTimeout(), true))
24 changes: 23 additions & 1 deletion packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
@@ -107,8 +107,30 @@ export class VitestMocker {
if (cached)
return cached
const exports = await mock()

if (exports === null || typeof exports !== 'object')
throw new Error('[vitest] vi.mock(path: string, factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?')

this.moduleCache.set(dep, { exports })
return exports

const exportHandler = {
get(target: Record<string, any>, prop: any) {
const val = target[prop]

// 'then' can exist on non-Promise objects, need nested instanceof check for logic to work
if (prop === 'then') {
if (target instanceof Promise)
return target.then.bind(target)
}
else if (val === undefined) {
throw new Error(`[vitest] No "${prop}" export is defined on the "${dep}"`)
}

return val
},
}

return new Proxy(exports, exportHandler)
}

private getMockPath(dep: string) {
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/suite.ts
Original file line number Diff line number Diff line change
@@ -50,8 +50,8 @@ export function clearCollectorContext() {
collectorContext.currentSuite = defaultSuite
}

export function getCurrentSuite() {
return collectorContext.currentSuite || defaultSuite
export function getCurrentSuite<ExtraContext = {}>() {
return (collectorContext.currentSuite || defaultSuite) as SuiteCollector<ExtraContext>
}

export function createSuiteHooks() {
24 changes: 15 additions & 9 deletions packages/vitest/src/types/tasks.ts
Original file line number Diff line number Diff line change
@@ -126,7 +126,10 @@ type ChainableTestAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'fails',
[name: string, fn?: TestFunction<ExtraContext>, timeout?: number],
void,
{ each: TestEachFunction }
{
each: TestEachFunction
<T extends ExtraContext>(name: string, fn?: TestFunction<T>, timeout?: number): void
}
>

export type TestAPI<ExtraContext = {}> = ChainableTestAPI<ExtraContext> & {
@@ -137,12 +140,15 @@ export type TestAPI<ExtraContext = {}> = ChainableTestAPI<ExtraContext> & {

type ChainableSuiteAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'shuffle',
[name: string, factory?: SuiteFactory],
[name: string, factory?: SuiteFactory<ExtraContext>],
SuiteCollector<ExtraContext>,
{ each: TestEachFunction }
{
each: TestEachFunction
<T extends ExtraContext>(name: string, factory?: SuiteFactory<T>): SuiteCollector<T>
}
>

export type SuiteAPI<ExtraContext = {}> = ChainableSuiteAPI & {
export type SuiteAPI<ExtraContext = {}> = ChainableSuiteAPI<ExtraContext> & {
each: SuiteEachFunction
skipIf(condition: any): ChainableSuiteAPI<ExtraContext>
runIf(condition: any): ChainableSuiteAPI<ExtraContext>
@@ -152,11 +158,11 @@ export type HookListener<T extends any[], Return = void> = (...args: T) => Await

export type HookCleanupCallback = (() => Awaitable<unknown>) | void

export interface SuiteHooks {
export interface SuiteHooks<ExtraContext = {}> {
beforeAll: HookListener<[Suite | File], HookCleanupCallback>[]
afterAll: HookListener<[Suite | File]>[]
beforeEach: HookListener<[TestContext, Suite], HookCleanupCallback>[]
afterEach: HookListener<[TestContext, Suite]>[]
beforeEach: HookListener<[TestContext & ExtraContext, Suite], HookCleanupCallback>[]
afterEach: HookListener<[TestContext & ExtraContext, Suite]>[]
}

export interface SuiteCollector<ExtraContext = {}> {
@@ -167,10 +173,10 @@ export interface SuiteCollector<ExtraContext = {}> {
tasks: (Suite | Test | SuiteCollector<ExtraContext>)[]
collect: (file?: File) => Promise<Suite>
clear: () => void
on: <T extends keyof SuiteHooks>(name: T, ...fn: SuiteHooks[T]) => void
on: <T extends keyof SuiteHooks<ExtraContext>>(name: T, ...fn: SuiteHooks<ExtraContext>[T]) => void
}

export type SuiteFactory = (test: (name: string, fn: TestFunction) => void) => Awaitable<void>
export type SuiteFactory<ExtraContext = {}> = (test: (name: string, fn: TestFunction<ExtraContext>) => void) => Awaitable<void>

export interface RuntimeContext {
tasks: (SuiteCollector | Test)[]
2 changes: 1 addition & 1 deletion packages/web-worker/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vitest/web-worker",
"type": "module",
"version": "0.21.0",
"version": "0.21.1",
"description": "Web Worker support for testing in Vitest",
"exports": {
".": {
Loading