From c5bc792a085801b16965f6775d705874ae33ecf6 Mon Sep 17 00:00:00 2001 From: Robert Oskamp Date: Wed, 13 Nov 2024 16:54:12 +0100 Subject: [PATCH 1/3] kbn-expect - add .eql support for sets --- packages/kbn-expect/expect.js | 9 ++++++ packages/kbn-expect/expect.test.ts | 47 ++++++++++++++++++++++++++++++ packages/kbn-expect/jest.config.js | 14 +++++++++ 3 files changed, 70 insertions(+) create mode 100644 packages/kbn-expect/expect.test.ts create mode 100644 packages/kbn-expect/jest.config.js diff --git a/packages/kbn-expect/expect.js b/packages/kbn-expect/expect.js index 1a48d75a4615d..e2ddbd9e3b114 100644 --- a/packages/kbn-expect/expect.js +++ b/packages/kbn-expect/expect.js @@ -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) + } + if (isDOMElement(value)) { return getOuterHTML(value); } @@ -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 + // 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 diff --git a/packages/kbn-expect/expect.test.ts b/packages/kbn-expect/expect.test.ts new file mode 100644 index 0000000000000..b26d0363eb345 --- /dev/null +++ b/packages/kbn-expect/expect.test.ts @@ -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(); +}); diff --git a/packages/kbn-expect/jest.config.js b/packages/kbn-expect/jest.config.js new file mode 100644 index 0000000000000..4e517d865391a --- /dev/null +++ b/packages/kbn-expect/jest.config.js @@ -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: ['/packages/kbn-expect'], +}; From da6e6fb0a7bfa5630036c8c3dad1db0ed7806e4b Mon Sep 17 00:00:00 2001 From: Robert Oskamp Date: Wed, 13 Nov 2024 17:58:10 +0100 Subject: [PATCH 2/3] Fix tsconfig --- packages/kbn-expect/tsconfig.json | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/kbn-expect/tsconfig.json b/packages/kbn-expect/tsconfig.json index 4346803ced341..bf599d834c806 100644 --- a/packages/kbn-expect/tsconfig.json +++ b/packages/kbn-expect/tsconfig.json @@ -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/**/*", From adf3b3978dd92352143a8069b9b721099c402fd0 Mon Sep 17 00:00:00 2001 From: Robert Oskamp Date: Thu, 14 Nov 2024 10:16:45 +0100 Subject: [PATCH 3/3] Fix typo in comment --- packages/kbn-expect/expect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kbn-expect/expect.js b/packages/kbn-expect/expect.js index e2ddbd9e3b114..f2538c5a10fb5 100644 --- a/packages/kbn-expect/expect.js +++ b/packages/kbn-expect/expect.js @@ -935,7 +935,7 @@ 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 + // If both are Sets, they should be treated equal if they have the same // entries, independent of the ordering } else if (actual instanceof Set && expected instanceof Set) { return actual.size === expected.size && actual.difference(expected).size === 0;