feat: Export snapshot state to expect.extend. (#1378)

This commit is contained in:
Christoph Nakazawa 2022-05-29 14:04:48 +09:00 committed by GitHub
parent 17939379ed
commit 4ec0a76f6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1181 additions and 0 deletions

View File

@ -0,0 +1,5 @@
# Vitest Demo
Run `npm test` and change a test or source code to see HMR in action!
Learn more at https://vitest.dev

View File

@ -0,0 +1,21 @@
{
"name": "@vitest/example-image-snapshot",
"type": "module",
"private": true,
"license": "MIT",
"main": "index.js",
"scripts": {
"test": "vitest",
"test:ui": "vitest --ui",
"test:run": "vitest run"
},
"devDependencies": {
"@vitest/ui": "latest",
"jest-image-snapshot": "^4.5.1",
"vite": "^2.9.9",
"vitest": "latest"
},
"stackblitz": {
"startCommand": "npm run test:ui"
}
}

View File

@ -0,0 +1,17 @@
import { readFileSync } from 'fs'
import { expect, test } from 'vitest'
import { toMatchImageSnapshot } from 'jest-image-snapshot'
declare global {
namespace jest {
interface Matchers<R> {
toMatchImageSnapshot(): R
}
}
}
expect.extend({ toMatchImageSnapshot })
test('image snapshot', () => {
expect(readFileSync('./test/stubs/input-image.png')).toMatchImageSnapshot()
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

@ -0,0 +1,12 @@
/// <reference types="vitest" />
// Configure Vitest (https://vitest.dev/config/)
import { defineConfig } from 'vite'
export default defineConfig({
test: {
/* for example, use global to avoid globals imports (describe, test, expect): */
// globals: true,
},
})

View File

@ -5,6 +5,7 @@ import type {
MatchersObject,
SyncExpectationResult,
} from '../../types/chai'
import { getSnapshotClient } from '../snapshot/chai'
import { AsymmetricMatcher } from './jest-asymmetric-matchers'
import { getState } from './jest-expect'
@ -37,6 +38,7 @@ const getMatcherState = (assertion: Chai.AssertionStatic & Chai.Assertion) => {
equals,
// needed for built-in jest-snapshots, but we don't use it
suppressedErrors: [],
snapshotState: getSnapshotClient().snapshotState!,
}
return {

View File

@ -9,6 +9,7 @@ import type { use as chaiUse } from 'chai'
*/
import type * as jestMatcherUtils from '../integrations/chai/jest-matcher-utils'
import type SnapshotState from '../integrations/snapshot/port/state'
export type FirstFunctionArgument<T> = T extends (arg: infer A) => unknown ? A : never
export type ChaiPlugin = FirstFunctionArgument<typeof chaiUse>
@ -33,6 +34,7 @@ export interface MatcherState {
isExpectingAssertionsError?: Error | null
isNot: boolean
promise: string
snapshotState: SnapshotState
suppressedErrors: Array<Error>
testPath?: string
utils: typeof jestMatcherUtils & {

1122
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff