Skip to content

Commit

Permalink
feat: Export snapshot state to expect.extend. (#1378)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed May 29, 2022
1 parent 1793937 commit 4ec0a76
Show file tree
Hide file tree
Showing 9 changed files with 1,185 additions and 4 deletions.
5 changes: 5 additions & 0 deletions examples/image-snapshot/README.md
@@ -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
21 changes: 21 additions & 0 deletions examples/image-snapshot/package.json
@@ -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"
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions examples/image-snapshot/test/basic.test.ts
@@ -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()
})
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions examples/image-snapshot/vite.config.ts
@@ -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,
},
})
2 changes: 2 additions & 0 deletions packages/vitest/src/integrations/chai/jest-extend.ts
Expand Up @@ -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'

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/types/chai.ts
Expand Up @@ -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>
Expand All @@ -33,6 +34,7 @@ export interface MatcherState {
isExpectingAssertionsError?: Error | null
isNot: boolean
promise: string
snapshotState: SnapshotState
suppressedErrors: Array<Error>
testPath?: string
utils: typeof jestMatcherUtils & {
Expand Down

0 comments on commit 4ec0a76

Please sign in to comment.