Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add merge utility #37

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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();
});
Loading