From 94935b023b11eefa95eae7263d7955463cf3bfed Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Thu, 27 Aug 2020 10:20:01 +0100 Subject: [PATCH] working flat() polyfill for jest --- packages/builder/package.json | 3 +++ packages/builder/scripts/jestSetup.js | 25 +++++++++++++++++++ .../tests/fetchBindableProperties.spec.js | 6 ----- 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 packages/builder/scripts/jestSetup.js diff --git a/packages/builder/package.json b/packages/builder/package.json index 42d1677ecf..6886388981 100644 --- a/packages/builder/package.json +++ b/packages/builder/package.json @@ -49,6 +49,9 @@ ], "setupFilesAfterEnv": [ "@testing-library/jest-dom/extend-expect" + ], + "setupFiles": [ + "./scripts/jestSetup.js" ] }, "eslintConfig": { diff --git a/packages/builder/scripts/jestSetup.js b/packages/builder/scripts/jestSetup.js new file mode 100644 index 0000000000..52053d4092 --- /dev/null +++ b/packages/builder/scripts/jestSetup.js @@ -0,0 +1,25 @@ +if (!Array.prototype.flat) { + Object.defineProperty(Array.prototype, "flat", { + configurable: true, + value: function flat() { + var depth = isNaN(arguments[0]) ? 1 : Number(arguments[0]) + + return depth + ? Array.prototype.reduce.call( + this, + function(acc, cur) { + if (Array.isArray(cur)) { + acc.push.apply(acc, flat.call(cur, depth - 1)) + } else { + acc.push(cur) + } + + return acc + }, + [] + ) + : Array.prototype.slice.call(this) + }, + writable: true, + }) +} diff --git a/packages/builder/tests/fetchBindableProperties.spec.js b/packages/builder/tests/fetchBindableProperties.spec.js index 59b68a1a6f..052d98d8ea 100644 --- a/packages/builder/tests/fetchBindableProperties.spec.js +++ b/packages/builder/tests/fetchBindableProperties.spec.js @@ -1,10 +1,4 @@ import fetchBindableProperties from "../src/builderStore/fetchBindableProperties" -import { flat } from "lodash" - -// supports running jest on Node 10.x -if (!Array.flat) { - Array.flat = flat -} describe("fetch bindable properties", () => {