Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions docs/api/browser/assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1152,10 +1152,9 @@ await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button', {

- `comparatorName: "pixelmatch" = "pixelmatch"`

The name of the algorithm/library used for comparing images.
The algorithm/library used for comparing images.

Currently, [`"pixelmatch"`](https://github.com/mapbox/pixelmatch) is the only
supported comparator.
`"pixelmatch"` is the only built-in comparator, but you can use custom ones by [registering them in the config file](/config/browser/expect#browser-expect-tomatchscreenshot-comparators).

- `comparatorOptions: object`

Expand Down Expand Up @@ -1210,7 +1209,7 @@ await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button', {

#### `"pixelmatch"` comparator options

The following options are available when using the `"pixelmatch"` comparator:
The `"pixelmatch"` comparator uses [`@blazediff/core`](https://blazediff.dev/docs/core) under the hood. The following options are available when using it:

- `allowedMismatchedPixelRatio: number | undefined = undefined`

Expand Down
2 changes: 1 addition & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
"vitest": "workspace:*"
},
"dependencies": {
"@blazediff/core": "1.9.1",
"@vitest/mocker": "workspace:*",
"@vitest/utils": "workspace:*",
"magic-string": "catalog:",
"pixelmatch": "7.1.0",
"pngjs": "^7.0.0",
"sirv": "catalog:",
"tinyrainbow": "catalog:",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ScreenshotComparatorRegistry } from '../../../../../context'
import type { Comparator } from '../types'
import pm from 'pixelmatch'
import { diff } from '@blazediff/core'

const defaultOptions = {
allowedMismatchedPixelRatio: undefined,
Expand Down Expand Up @@ -36,7 +36,7 @@ export const pixelmatch: Comparator<ScreenshotComparatorRegistry['pixelmatch']>
? new Uint8Array(reference.data.length)
: undefined

const mismatchedPixels = pm(
const mismatchedPixels = diff(
reference.data,
actual.data,
diffBuffer,
Expand Down
21 changes: 20 additions & 1 deletion packages/mocker/src/automocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,28 @@ export function mockObject(
continue
}

if (options.type === 'autospy' && type === 'Module') {
// Replace with clean object to recursively autospy exported module objects:
// export * as ns from "./ns"
// or
// import * as ns from "./ns"
// export { ns }
const exports = Object.create(null)
Object.defineProperty(exports, Symbol.toStringTag, {
value: 'Module',
configurable: true,
writable: true,
})
try {
newContainer[property] = exports
}
catch {
continue
}
}
// Sometimes this assignment fails for some unknown reason. If it does,
// just move along.
if (!define(newContainer, property, isFunction || options.type === 'autospy' ? value : {})) {
else if (!define(newContainer, property, isFunction || options.type === 'autospy' ? value : {})) {
continue
}

Expand Down
19 changes: 8 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 83 additions & 1 deletion test/cli/test/stacktraces.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolve } from 'pathe'
import { glob } from 'tinyglobby'
import { describe, expect, it } from 'vitest'
import { rolldownVersion } from 'vitest/node'
import { runInlineTests, runVitest } from '../../test-utils'

// To prevent the warning coming up in snapshots
Expand Down Expand Up @@ -217,7 +218,87 @@ it('resolves/rejects', async () => {
`,
})

expect(stderr).toMatchInlineSnapshot(`
if (rolldownVersion) {
expect(stderr).toMatchInlineSnapshot(`
"
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 4 ⎯⎯⎯⎯⎯⎯⎯

FAIL repro.test.ts > resolves: resolved promise with mismatched value
AssertionError: expected 3 to be 4 // Object.is equality

- Expected
+ Received

- 4
+ 3

❯ repro.test.ts:5:41
3|
4| test('resolves: resolved promise with mismatched value', async (…
5| await expect(Promise.resolve(3)).resolves.toBe(4)
| ^
6| })
7|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/4]⎯

FAIL repro.test.ts > rejects: rejected promise with mismatched value
AssertionError: expected 3 to be 4 // Object.is equality

- Expected
+ Received

- 4
+ 3

❯ repro.test.ts:9:40
7|
8| test('rejects: rejected promise with mismatched value', async ()…
9| await expect(Promise.reject(3)).rejects.toBe(4)
| ^
10| })
11|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/4]⎯

FAIL repro.test.ts > rejects: resolves when rejection expected
AssertionError: promise resolved "3" instead of rejecting

- Expected:
Error {
"message": "rejected promise",
}

+ Received:
3

❯ repro.test.ts:13:41
11|
12| test('rejects: resolves when rejection expected', async () => {
13| await expect(Promise.resolve(3)).rejects.toBe(4)
| ^
14| })
15|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/4]⎯

FAIL repro.test.ts > resolves: rejects when resolve expected
AssertionError: promise rejected "3" instead of resolving
❯ repro.test.ts:17:40
15|
16| test('resolves: rejects when resolve expected', async () => {
17| await expect(Promise.reject(3)).resolves.toBe(4)
| ^
18| })
19|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/4]⎯

"
`)
}
else {
expect(stderr).toMatchInlineSnapshot(`
"
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 4 ⎯⎯⎯⎯⎯⎯⎯

Expand Down Expand Up @@ -294,6 +375,7 @@ it('resolves/rejects', async () => {

"
`)
}
expect(errorTree()).toMatchInlineSnapshot(`
{
"repro.test.ts": {
Expand Down
1 change: 1 addition & 0 deletions test/core/src/mocks/autospying-namespace/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as NamespaceTarget from './namespaceTarget.js'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const computeSquare = (n: number) => n * n
8 changes: 8 additions & 0 deletions test/core/test/mocking/autospying.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import axios from 'axios'
import { expect, test, vi } from 'vitest'
import { getAuthToken } from '../../src/env'
import * as NamespaceModule from '../../src/mocks/autospying-namespace/index.js'

vi.mock(import('../../src/env'), { spy: true })

vi.mock('axios', { spy: true })
vi.mock('../../src/mocks/autospying-namespace/index.js', { spy: true })

test('getAuthToken is spied', async () => {
import.meta.env.AUTH_TOKEN = '123'
Expand All @@ -23,3 +25,9 @@ test('package in __mocks__ has lower priority', async () => {
expect(axios.isAxiosError(new Error('test'))).toBe(false)
expect(axios.isAxiosError).toHaveBeenCalled()
})

test('spies on namespace re-exports', async () => {
expect(vi.isMockFunction(NamespaceModule.NamespaceTarget.computeSquare)).toBe(true)
expect(NamespaceModule.NamespaceTarget.computeSquare(5)).toBe(25)
expect(NamespaceModule.NamespaceTarget.computeSquare).toHaveBeenCalledTimes(1)
})
Loading