Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions packages/jest-jasmine2/src/__tests__/iterators-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
*/
'use strict';

describe('iterators', () => {

it('works for arrays', () => {
const mixedArray = [1, {}, []];

expect(mixedArray).toEqual(mixedArray);
expect([1, 2, 3]).toEqual([1, 2, 3]);
expect([1]).not.toEqual([2]);
expect([1, 2, 3]).not.toEqual([1, 2]);
expect([1, 2, 3]).not.toEqual([1, 2, 3, 4]);
});

it('works for custom iterables', () => {
const iterable = {
0: 'a',
1: 'b',
2: 'c',
length: 3,
[Symbol.iterator]: Array.prototype[Symbol.iterator],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fancy JavaScript you got there.

};
const expectedIterable = {
0: 'a',
1: 'b',
2: 'c',
length: 3,
[Symbol.iterator]: Array.prototype[Symbol.iterator],
};
expect(iterable).toEqual(expectedIterable);
expect(iterable).not.toEqual(['a', 'b']);
expect(iterable).not.toEqual(['a', 'b', 'c']);
expect(iterable).not.toEqual(['a', 'b', 'c', 'd']);
});

it('works for Sets', () => {
const numbers = [1, 2, 3, 4];
const setOfNumbers = new Set(numbers);
expect(setOfNumbers).not.toEqual(new Set());
expect(setOfNumbers).not.toBe(numbers);
expect(setOfNumbers).not.toEqual([1, 2]);
expect(setOfNumbers).not.toEqual([1, 2, 3]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add a test for it not to equal [1, 2].

expect(setOfNumbers).toEqual(new Set(numbers));

const nestedSets = new Set([new Set([1, 2])]);
expect(nestedSets).not.toEqual(new Set([new Set([1, 4])]));
expect(nestedSets).toEqual(new Set([new Set([1, 2])]));
});

it('works for Maps', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add two tests: one comparing just against key1: 'value1' and one comparing against a Map with three items.

const keyValuePairs = [['key1', 'value1'], ['key2', 'value2']];
const smallerKeyValuePairs = [['key1', 'value1']];
const biggerKeyValuePairs = [
['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3'],
];
const map = new Map(keyValuePairs);
expect(map).not.toEqual(smallerKeyValuePairs);
expect(map).not.toEqual(new Map(smallerKeyValuePairs));
expect(map).not.toEqual(biggerKeyValuePairs);
expect(map).not.toEqual(new Map(biggerKeyValuePairs));
expect(map).not.toEqual(keyValuePairs);
expect(map).not.toBe(keyValuePairs);
expect(map).toEqual(new Map(keyValuePairs));
});

});
39 changes: 39 additions & 0 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,46 @@ function jasmine2(config, environment, moduleLoader, testPath) {
moduleLoader.requireModule(null, config.setupTestFrameworkScriptFile);
}
});

const hasIterator = object => !!(object != null && object[Symbol.iterator]);
const iterableEquality = (a, b) => {
if (
typeof a !== 'object' ||
typeof b !== 'object' ||
Array.isArray(a) ||
Array.isArray(b) ||
!hasIterator(a) ||
!hasIterator(b)
) {
return undefined;
}
if (a.constructor !== b.constructor) {
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming that on www if this returns false it is just going to look at the other equality checkers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check is only called for typeof === 'object', excluding arrays and that have iterators, so it should be fine like this, I'll check after importing and in case I can return undefined here.

}
const bIterator = b[Symbol.iterator]();

for (const aValue of a) {
const nextB = bIterator.next();
if (
nextB.done ||
!jasmine.matchersUtil.equals(
aValue,
nextB.value,
[iterableEquality]
)
) {
return false;
}
}
if (!bIterator.next().done) {
return false;
}
return true;
};

env.beforeEach(() => {
jasmine.addCustomEqualityTester(iterableEquality);

jasmine.addMatchers({
toBeCalled: () => ({
compare: (actual, expected) => {
Expand Down