Skip to content

Commit

Permalink
chore: more usecases for .resolves.toThrow
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed May 13, 2022
1 parent 1edb697 commit 05eedef
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
38 changes: 33 additions & 5 deletions packages/vitest/src/integrations/chai/jest-expect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import c from 'picocolors'
import { AssertionError } from 'chai'
import type { EnhancedSpy } from '../spy'
import { isMockFunction } from '../spy'
import { addSerializer } from '../snapshot/port/plugins'
Expand Down Expand Up @@ -56,15 +57,26 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
return function (this: Chai.Assertion & Chai.AssertionStatic, ...args: any[]) {
const promise = utils.flag(this, 'promise')
const object = utils.flag(this, 'object')
const isNot = utils.flag(this, 'negate') as boolean
if (promise === 'rejects') {
utils.flag(this, 'object', () => {
throw object
})
}
else if (promise === 'resolves') {
utils.flag(this, 'object', () => {
return object
})
// if it got here, it's already resolved
// unless it tries to resolve to a function that should throw
// called as '.resolves[.not].toThrow()`
else if (promise === 'resolves' && typeof object !== 'function') {
if (!isNot) {
const message = utils.flag(this, 'message') || 'expected promise to throw an error, but it didn\'t'
const error = {
showDiff: false,
}
throw new AssertionError(message, error, utils.flag(this, 'ssfi'))
}
else {
return
}
}
_super.apply(this, args)
}
Expand Down Expand Up @@ -432,11 +444,27 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {

const obj = this._obj
const promise = utils.flag(this, 'promise')
const isNot = utils.flag(this, 'negate') as boolean
let thrown: any = null

if (promise) {
if (promise === 'rejects') {
thrown = obj
}
// if it got here, it's already resolved
// unless it tries to resolve to a function that should throw
// called as .resolves.toThrow(Error)
else if (promise === 'resolves' && typeof obj !== 'function') {
if (!isNot) {
const message = utils.flag(this, 'message') || 'expected promise to throw an error, but it didn\'t'
const error = {
showDiff: false,
}
throw new AssertionError(message, error, utils.flag(this, 'ssfi'))
}
else {
return
}
}
else {
try {
obj()
Expand Down
30 changes: 30 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,33 @@ describe('async expect', () => {
await expect((async () => 'true')()).resolves.toBe('true')
await expect((async () => 'true')()).resolves.not.toBe('true22')
await expect((async () => 'true')()).resolves.not.toThrow()
await expect((async () => new Error('msg'))()).resolves.not.toThrow() // calls chai assertion
await expect((async () => new Error('msg'))()).resolves.not.toThrow(Error) // calls our assertion
await expect((async () => () => {
throw new Error('msg')
})()).resolves.toThrow()
await expect((async () => () => {
return new Error('msg')
})()).resolves.not.toThrow()
await expect((async () => () => {
return new Error('msg')
})()).resolves.not.toThrow(Error)
})

it('resolves trows chai', async () => {
const assertion = async () => {
await expect((async () => new Error('msg'))()).resolves.toThrow()
}

await expect(assertion).rejects.toThrowError('expected promise to throw an error, but it didn\'t')
})

it('resolves trows jest', async () => {
const assertion = async () => {
await expect((async () => new Error('msg'))()).resolves.toThrow(Error)
}

await expect(assertion).rejects.toThrowError('expected promise to throw an error, but it didn\'t')
})

it('throws an error on .resolves when the argument is not a promise', () => {
Expand All @@ -455,6 +482,9 @@ describe('async expect', () => {
await expect((async () => {
throw new Error('err')
})()).resolves.toBe('true')
})

it.fails('failed to throw', async () => {
await expect((async () => {
throw new Error('err')
})()).resolves.not.toThrow()
Expand Down

0 comments on commit 05eedef

Please sign in to comment.