From 4e56826b1a4cfef7abcde99d19b766f80d9dee66 Mon Sep 17 00:00:00 2001 From: Boris Serdiuk Date: Mon, 25 Mar 2024 18:18:39 +0100 Subject: [PATCH] feat: Add merge utility --- README.md | 6 ++-- merge.js | 13 ++++++++ package-lock.json | 4 +-- package.json | 3 +- test/merge.test.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 merge.js create mode 100644 test/merge.test.js diff --git a/README.md b/README.md index ed5696f..fa641d6 100644 --- a/README.md +++ b/README.md @@ -40,14 +40,14 @@ In your `jest.config.json`: ### Advanced usage -If you are already have another preset in your configuration, you can [merge them together](https://stackoverflow.com/questions/51002460/is-it-possible-to-use-jest-with-multiple-presets-at-the-same-time): +If you are already have another preset in your configuration, you can use `@cloudscape-design/jest-preset/merge` helper to merge them together: ```js -const merge = require('merge'); +const mergePresets = require('@cloudscape-design/jest-preset/merge'); const tsPreset = require('ts-jest/jest-preset'); const cloudscapePreset = require('@cloudscape-design/jest-preset'); -module.exports = merge.recursive(tsPreset, cloudscapePreset, { +module.exports = mergePresets(tsPreset, cloudscapePreset, { // ... your config }); ``` diff --git a/merge.js b/merge.js new file mode 100644 index 0000000..ab61035 --- /dev/null +++ b/merge.js @@ -0,0 +1,13 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +const { mergeWith } = require('lodash'); + +function mergeArrays(dest, src) { + if (Array.isArray(dest)) { + return dest.concat(src); + } +} + +module.exports = function mergePresets(...presets) { + return mergeWith({}, ...presets, mergeArrays); +}; diff --git a/package-lock.json b/package-lock.json index e44dc0a..6c63db0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,8 @@ "name": "@cloudscape-design/jest-preset", "version": "2.0.0", "dependencies": { - "@babel/plugin-transform-modules-commonjs": "^7.9.0" + "@babel/plugin-transform-modules-commonjs": "^7.9.0", + "lodash": "^4.17.21" }, "devDependencies": { "eslint": "^8.56.0", @@ -6165,7 +6166,6 @@ }, "node_modules/lodash": { "version": "4.17.21", - "dev": true, "license": "MIT" }, "node_modules/lodash.merge": { diff --git a/package.json b/package.json index bcc746c..7a83771 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "jest": ">=24" }, "dependencies": { - "@babel/plugin-transform-modules-commonjs": "^7.9.0" + "@babel/plugin-transform-modules-commonjs": "^7.9.0", + "lodash": "^4.17.21" }, "devDependencies": { "eslint": "^8.56.0", diff --git a/test/merge.test.js b/test/merge.test.js new file mode 100644 index 0000000..3bb0830 --- /dev/null +++ b/test/merge.test.js @@ -0,0 +1,79 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +const tap = require('tap'); +const { strict: assert } = require('assert'); +const merge = require('../merge'); + +tap.test('merging simple configurations', t => { + assert.deepEqual( + merge( + { + transformIgnorePatterns: ['project-ignore'], + transform: { + 'project-js': 'project-babel-jest', + }, + }, + { + transformIgnorePatterns: ['our-ignore'], + transform: { + 'cloudscape-js': 'our-babel-jest', + }, + }, + ), + { + transform: { + 'project-js': 'project-babel-jest', + 'cloudscape-js': 'our-babel-jest', + }, + transformIgnorePatterns: ['project-ignore', 'our-ignore'], + }, + ); + t.end(); +}); + +tap.test('merging multiple configurations', t => { + assert.deepEqual( + merge( + { + transform: { 'project-js': 'project-babel-jest' }, + }, + { + transform: { 'project-ts': 'ts-transformer' }, + }, + + { + transform: { 'cloudscape-js': 'our-babel-jest' }, + }, + ), + { + transform: { + 'project-js': 'project-babel-jest', + 'project-ts': 'ts-transformer', + 'cloudscape-js': 'our-babel-jest', + }, + }, + ); + t.end(); +}); + +tap.test('does not mutate objects', t => { + const src = { + transform: { 'project-js': 'project-babel-jest' }, + }; + const dest = { + transform: { 'cloudscape-js': 'our-babel-jest' }, + }; + assert.deepEqual(merge(src, dest), { + transform: { + 'project-js': 'project-babel-jest', + 'cloudscape-js': 'our-babel-jest', + }, + }); + assert.deepEqual(src, { + transform: { 'project-js': 'project-babel-jest' }, + }); + assert.deepEqual(dest, { + transform: { 'cloudscape-js': 'our-babel-jest' }, + }); + t.end(); +});