Skip to content

Commit

Permalink
feat: Add merge utility
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Mar 25, 2024
1 parent 17cbb36 commit 4e56826
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
```
Expand Down
13 changes: 13 additions & 0 deletions merge.js
Original file line number Diff line number Diff line change
@@ -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);
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
79 changes: 79 additions & 0 deletions test/merge.test.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 4e56826

Please sign in to comment.