Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kbn-expect - add .eql support for sets #200034

Merged
merged 5 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions packages/kbn-expect/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,11 @@ function i (obj, showHidden, depth) {
return stylize('null', 'null');
}

// format sets like arrays
if (value instanceof Set) {
value = Array.from(value)
}
Comment on lines +650 to +653
Copy link
Member Author

Choose a reason for hiding this comment

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

The format doesn't know how to handle sets properly, so it applies the code for generic objects, which prints a failure message like this:

expected {} to sort of equal {}

The quickest way to get a proper output was treating the sets as arrays for formatting purposes, which prints out failure messages like this:

expected [ 'a', 'b', 'c' ] to sort of equal [ 'x', 'y', 'z' ]


if (isDOMElement(value)) {
return getOuterHTML(value);
}
Expand Down Expand Up @@ -930,6 +935,10 @@ expect.eql = function eql(actual, expected) {
// to determine equivalence.
} else if (isRegExp(actual) && isRegExp(expected)) {
return regExpEquiv(actual, expected);
// If both are Sets, they should be treated equal if the have the same
pheyos marked this conversation as resolved.
Show resolved Hide resolved
// entries, independent of the ordering
} else if (actual instanceof Set && expected instanceof Set) {
return actual.size === expected.size && actual.difference(expected).size === 0;
// 7.4. For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
Expand Down
47 changes: 47 additions & 0 deletions packages/kbn-expect/expect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

// importing this package's expect as `kbnExpect` to not conflict with Jest's expect
import kbnExpect from './expect';

it('asserts that sets with same entries but different ordering are equal', async () => {
const setA = new Set(['a', 'b', 'c']);
const setB = new Set(['c', 'b', 'a']);

expect(() => {
kbnExpect(setA).to.eql(setB);
}).not.toThrow();
expect(() => {
kbnExpect(setB).to.eql(setA);
}).not.toThrow();
});

it('asserts that sets with same size but different entries are not equal', async () => {
const setA = new Set(['a', 'b', 'c']);
const setB = new Set(['x', 'y', 'z']);

expect(() => {
kbnExpect(setA).to.eql(setB);
}).toThrow();
expect(() => {
kbnExpect(setB).to.eql(setA);
}).toThrow();
});

it('asserts that sets with different size but overlapping items are not equal', async () => {
const setA = new Set(['a', 'b', 'c']);
const setB = new Set(['a', 'b']);

expect(() => {
kbnExpect(setA).to.eql(setB);
}).toThrow();
expect(() => {
kbnExpect(setB).to.eql(setA);
}).toThrow();
});
14 changes: 14 additions & 0 deletions packages/kbn-expect/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-expect'],
};
8 changes: 6 additions & 2 deletions packages/kbn-expect/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types"
"outDir": "target/types",
"types": [
"jest",
],
},
"include": [
"expect.d.ts"
"expect.d.ts",
"**/*.ts"
],
"exclude": [
"target/**/*",
Expand Down