Skip to content

Commit

Permalink
chore: more assertions moved
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed May 9, 2024
1 parent 7339029 commit 318ad6f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
File renamed without changes.
21 changes: 21 additions & 0 deletions packages/expect/src/jest-assertions/toEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineAssertion } from '../utils'
import { iterableEquality, equals as jestEquals } from '../jest-utils'
import { getCustomEqualityTesters } from '../jest-matcher-utils'

export default defineAssertion('toEqual', function (expected: unknown) {
const customTesters = getCustomEqualityTesters()
const actual = this._obj
const equal = jestEquals(
actual,
expected,
[...customTesters, iterableEquality],
)

return this.assert(
equal,
'expected #{this} to deeply equal #{exp}',
'expected #{this} to not deeply equal #{exp}',
expected,
actual,
)
})
28 changes: 28 additions & 0 deletions packages/expect/src/jest-assertions/toStrictEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineAssertion } from '../utils'
import { arrayBufferEquality, iterableEquality, equals as jestEquals, sparseArrayEquality, typeEquality } from '../jest-utils'
import { getCustomEqualityTesters } from '../jest-matcher-utils'

export default defineAssertion('toStrictEqual', function (expected: unknown) {
const customTesters = getCustomEqualityTesters()
const obj = this._obj
const equal = jestEquals(
obj,
expected,
[
...customTesters,
iterableEquality,
typeEquality,
sparseArrayEquality,
arrayBufferEquality,
],
true,
)

return this.assert(
equal,
'expected #{this} to strictly equal #{exp}',
'expected #{this} to not strictly equal #{exp}',
expected,
obj,
)
})
48 changes: 2 additions & 46 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { diff, getCustomEqualityTesters, stringify } from './jest-matcher-utils'
import { JEST_MATCHERS_OBJECT } from './constants'
import { recordAsyncExpect, wrapSoft } from './utils'

import matchers from './assertions/index'
import matchers from './jest-assertions/index'

// polyfill globals because expect can be used in node environment
declare class Node {
Expand All @@ -21,17 +21,13 @@ declare class DOMTokenList {
contains(item: unknown): boolean
}

const defineJestMatchers: ChaiPlugin = (chai, utils) => {
matchers.forEach(define => define(chai, utils))
}

// Jest Expect Compact
export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const { AssertionError } = chai
const c = () => getColors()
const customTesters = getCustomEqualityTesters()

defineJestMatchers(chai, utils)
matchers.forEach(define => define(chai, utils))

function def(name: keyof Assertion | (keyof Assertion)[], fn: ((this: Chai.AssertionStatic & Assertion, ...args: any[]) => any)) {
const addMethod = (n: keyof Assertion) => {
Expand Down Expand Up @@ -84,46 +80,6 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
return this
})

def('toEqual', function (expected) {
const actual = utils.flag(this, 'object')
const equal = jestEquals(
actual,
expected,
[...customTesters, iterableEquality],
)

return this.assert(
equal,
'expected #{this} to deeply equal #{exp}',
'expected #{this} to not deeply equal #{exp}',
expected,
actual,
)
})

def('toStrictEqual', function (expected) {
const obj = utils.flag(this, 'object')
const equal = jestEquals(
obj,
expected,
[
...customTesters,
iterableEquality,
typeEquality,
sparseArrayEquality,
arrayBufferEquality,
],
true,
)

return this.assert(
equal,
'expected #{this} to strictly equal #{exp}',
'expected #{this} to not strictly equal #{exp}',
expected,
obj,
)
})
def('toBe', function (expected) {
const actual = this._obj
const pass = Object.is(actual, expected)
Expand Down

0 comments on commit 318ad6f

Please sign in to comment.