Skip to content

Commit

Permalink
Fix .resolves and .rejects expectations
Browse files Browse the repository at this point in the history
The promise helpers, .resolves and .rejects, were throwing errors when the returned object could not be serialized. Specifically, this would happen when using dynamic imports (e.g. `expect(() => import('./some_module')).resolves`). Additionally, the .resolves helper did not support a function argument even though a function was expected.
  • Loading branch information
davidmyersdev committed May 12, 2022
1 parent 574d072 commit 984be7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/vitest/src/integrations/chai/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import c from 'picocolors'
import type { EnhancedSpy } from '../spy'
import { isMockFunction } from '../spy'
import { addSerializer } from '../snapshot/port/plugins'
import { toString } from '../utils'
import type { Constructable, Test } from '../../types'
import { assertTypes } from '../../utils'
import { unifiedDiff } from '../../node/diff'
Expand Down Expand Up @@ -550,6 +551,10 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
utils.flag(this, 'promise', 'resolves')
utils.flag(this, 'error', new Error('resolves'))
const obj = utils.flag(this, 'object')

if (typeof obj?.then !== 'function')
throw new TypeError(`You must provide a Promise to expect() when using .resolves, not '${typeof obj}'.`)

const proxy: any = new Proxy(this, {
get: (target, key, receiver) => {
const result = Reflect.get(target, key, receiver)
Expand All @@ -564,7 +569,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
return result.call(this, ...args)
},
(err: any) => {
throw new Error(`promise rejected "${err}" instead of resolving`)
throw new Error(`promise rejected "${toString(err)}" instead of resolving`)
},
)
}
Expand All @@ -579,6 +584,10 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
utils.flag(this, 'error', new Error('rejects'))
const obj = utils.flag(this, 'object')
const wrapper = typeof obj === 'function' ? obj() : obj // for jest compat

if (typeof wrapper?.then !== 'function')
throw new TypeError(`You must provide a Promise to expect() when using .rejects, not '${typeof wrapper}'.`)

const proxy: any = new Proxy(this, {
get: (target, key, receiver) => {
const result = Reflect.get(target, key, receiver)
Expand All @@ -589,7 +598,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
return async (...args: any[]) => {
return wrapper.then(
(value: any) => {
throw new Error(`promise resolved "${value}" instead of rejecting`)
throw new Error(`promise resolved "${toString(value)}" instead of rejecting`)
},
(err: any) => {
utils.flag(this, 'object', err)
Expand Down
9 changes: 9 additions & 0 deletions packages/vitest/src/integrations/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ export function getRunningMode() {
export function isWatchMode() {
return getRunningMode() === 'watch'
}

export function toString(object: any) {
try {
return `${object}`
}
catch (_error) {
return 'unknown'
}
}

0 comments on commit 984be7f

Please sign in to comment.