From 714382dc4ba3a8a68b719a320fe7eb71812a4066 Mon Sep 17 00:00:00 2001 From: Robert Oskamp Date: Thu, 14 Nov 2024 14:59:28 +0100 Subject: [PATCH] kbn-expect - add .eql support for sets (#200034) ## Summary This PR adds support for asserting test equality with kbn-expect. It also introduces some unit tests to kbn-expect in order to verify the changes are working. --- packages/kbn-expect/expect.js | 9 ++++++ packages/kbn-expect/expect.test.ts | 47 ++++++++++++++++++++++++++++++ packages/kbn-expect/jest.config.js | 14 +++++++++ packages/kbn-expect/tsconfig.json | 8 +++-- 4 files changed, 76 insertions(+), 2 deletions(-) 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..f2538c5a10fb5 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 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; // 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'], +}; 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/**/*",