Skip to content

Commit

Permalink
Use is-equal instead of deep-equal for comparisons
Browse files Browse the repository at this point in the history
Fixes #47
Fixes #50
  • Loading branch information
mjackson committed Dec 10, 2015
1 parent 1608bba commit 32e2169
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
10 changes: 5 additions & 5 deletions modules/Expectation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import deepEqual from 'deep-equal'
import isRegExp from 'is-regexp'
import isEqual from 'is-equal'
import isRegExp from 'is-regex'
import assert from './assert'
import { isSpy } from './SpyUtils'
import { functionThrows, arrayContains, stringContains, isArray, isFunction, isA } from './TestUtils'
Expand Down Expand Up @@ -65,7 +65,7 @@ class Expectation {
toEqual(value, message) {
try {
assert(
deepEqual(this.actual, value),
isEqual(this.actual, value),
(message || 'Expected %s to equal %s'),
this.actual,
value
Expand All @@ -83,7 +83,7 @@ class Expectation {

toNotEqual(value, message) {
assert(
!deepEqual(this.actual, value),
!isEqual(this.actual, value),
(message || 'Expected %s to not equal %s'),
this.actual,
value
Expand Down Expand Up @@ -334,7 +334,7 @@ class Expectation {

assert(
spy.calls.some(function (call) {
return deepEqual(call.arguments, expectedArgs)
return isEqual(call.arguments, expectedArgs)
}),
'spy was never called with %s',
expectedArgs
Expand Down
6 changes: 3 additions & 3 deletions modules/TestUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import deepEqual from 'deep-equal'
import isRegExp from 'is-regexp'
import isEqual from 'is-equal'
import isRegExp from 'is-regex'

/**
* Returns true if the given function throws the given value
Expand Down Expand Up @@ -41,7 +41,7 @@ export function functionThrows(fn, context, args, value) {
*/
export function arrayContains(array, value, comparator) {
if (comparator == null)
comparator = deepEqual
comparator = isEqual

return array.some(function (item) {
return comparator(item, value) !== false
Expand Down
27 changes: 27 additions & 0 deletions modules/__tests__/toEqual-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ describe('toEqual', function () {
}).toThrow(/Expected 'actual' to equal 'expected'/)
})

it('works when object has circular reference' , function () {
function circular() {
this.circularRef = this
}

const a = new circular()
const b = new circular()

expect(a).toEqual(b)
})

it('works with Map', function () {
const a = new Map()
a.set('key', 'value')

const b = new Map()
b.set('key', 'value')

expect(a).toEqual(b)
})

it('works with Set', function () {
const a = new Set('a')
const b = new Set('a')
expect(a).toEqual(b)
})

it('shows diff', function () {
try {
expect('actual').toEqual('expected')
Expand Down
20 changes: 20 additions & 0 deletions modules/__tests__/toNotEqual-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*eslint-env mocha */
import expect from '../index'

describe('toNotEqual', function () {
it('works with Map', function () {
const a = new Map()
a.set('key', 'value')

const b = new Map()
b.set('key', 'another value')

expect(a).toNotEqual(b)
})

it('works with Set', function () {
const a = new Set('a')
const b = new Set('b')
expect(a).toNotEqual(b)
})
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Write better assertions",
"main": "lib/index",
"dependencies": {
"deep-equal": "^1.0.1",
"is-regexp": "^1.0.0",
"is-equal": "^1.3.0",
"is-regex": "^1.0.3",
"object-inspect": "^1.0.2"
},
"devDependencies": {
Expand Down

0 comments on commit 32e2169

Please sign in to comment.