Skip to content

Commit

Permalink
chore: improve actual/expected
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 4, 2023
1 parent 542d176 commit 83731ea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,13 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {

if (typeof DOMTokenList !== 'undefined' && actual instanceof DOMTokenList) {
assertTypes(item, 'class name', ['string'])
const isNot = utils.flag(this, 'negate') as boolean
const expectedClassList = isNot ? actual.value.replace(item, '').trim() : `${actual.value} ${item}`
return this.assert(
actual.contains(item),
`expected "${actual.value}" to contain "${item}"`,
`expected "${actual.value}" not to contain "${item}"`,
`${actual.value} ${item}`,
expectedClassList,
actual.value,
)
}
Expand Down
38 changes: 37 additions & 1 deletion test/core/test/environments/jsdom.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// @vitest-environment jsdom

import { expect, test } from 'vitest'
import { createColors, getDefaultColors, setupColors } from '@vitest/utils'
import { processError } from '@vitest/utils/error'
import { afterEach, expect, test } from 'vitest'

afterEach(() => {
setupColors(createColors(true))
})

const nodeMajor = Number(process.version.slice(1).split('.')[0])

Expand Down Expand Up @@ -44,4 +50,34 @@ test('toContain correctly handles DOM nodes', () => {
expect(() => {
expect(wrapper.classList).toContain(2)
}).toThrowErrorMatchingInlineSnapshot(`"class name value must be string, received "number""`)

setupColors(getDefaultColors())

try {
expect(wrapper.classList).toContain('flex-row')
expect.unreachable()
}
catch (err: any) {
expect(processError(err).diff).toMatchInlineSnapshot(`
"- Expected
+ Received
- flex flex-col flex-row
+ flex flex-col"
`)
}

try {
expect(wrapper.classList).not.toContain('flex')
expect.unreachable()
}
catch (err: any) {
expect(processError(err).diff).toMatchInlineSnapshot(`
"- Expected
+ Received
- flex-col
+ flex flex-col"
`)
}
})

0 comments on commit 83731ea

Please sign in to comment.