-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Make it possible to merge transform
option with preset
#5505
Conversation
transform
options and presettransform
option and preset
Can you add a test for that? Merging transforms may cause unexpected issues though, like we had with |
Sure thing, I wanted to have PR to reference in the PR I'm working on. I have to override this option to make it work which is pretty unexpected when using preset. Well, it might be not that confusing if some options wouldn't work this way :) I must admit that I think we can also provide some helper functions to make this easier to maintain: const mergeOptionWithPreset = (options, preset, optionName) => {
if (options[optionName] && preset[optionName]) {
options[optionName] = Object.assign(
{},
preset[optionName],
options[optionName],
);
}
} It can be expressed also in a more functional way if you prefer. |
See #3689 |
So it's only about the order. It makes sense then. I will follow the same pattern. It still makes sense to do some refactoring, should I include it in this PR or open another one? |
This is the same logic, so feel free to refactor it as a part of this PR, it's small enough to fit in. |
@@ -964,7 +995,7 @@ describe('preset without setupFiles', () => { | |||
|
|||
beforeAll(() => { | |||
jest.mock( | |||
'/node_modules/react-native/jest-preset.json', | |||
'/node_modules/react-foo/jest-preset.json', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to be some kind of bug here. When it mocks the same file name as in the other describe
block, it doesn't reload the config but uses what was previously set. I updated this name to ensure that setupFiles
is configured properly, i.e. babel-jest
is enabled which triggers prepend of regenerator-runtime
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could switch to jest.doMock
here to avoid this, since it's already placed before the require call and we don't need to hoist it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me try it. This was my guess that jest.mock
calls get hoisted and this flow doesn't work as expected.
@@ -46,6 +46,21 @@ const PRESET_NAME = 'jest-preset' + JSON_EXTENSION; | |||
const createConfigError = message => | |||
new ValidationError(ERROR, message, DOCUMENTATION_NOTE); | |||
|
|||
const mergeOptionWithPreset = ( | |||
options: InitialOptions, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope it is fine. This is the first time I update code that uses Flow :)
@@ -922,7 +924,10 @@ describe('preset', () => { | |||
expect(options.setupFiles.sort()).toEqual([ | |||
'/node_modules/a', | |||
'/node_modules/b', | |||
'/node_modules/regenerator-runtime/runtime', | |||
]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
babel-jest
isn't explicitly set, so it doesn't append regenerator-runtime
anymore.
@thymikee - I refactored code, added a new test and updated existing tests to better reflect the current behavior. There are some issues with cache on CircleCi, but I don't have permissions to reset them. Let me know if there is anything else to fix or add. |
// Object initializer not used for properties as a workaround for | ||
// sort-keys eslint rule while specifying properties in | ||
// non-alphabetical order for a better test | ||
const transform = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just disable the rule for this object / line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shamelessly copied from the other test, but it makes sense what you said. I will update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good already 👍I wasn't able to rerun all the jobs without cache, just a couple (not sure why)
I tried with 757bc3b |
|
@gziolo mind updating the changelog? |
Yes, I wasn’t aware of that. I will do it by Monday 👍 |
transform
option and presettransform
option with preset
@SimenB done ✅ |
I can confirm the full test suite passes on my machine (a mac), so no issues from the skipped windows tests either |
{}, | ||
options[optionName], | ||
preset[optionName], | ||
options[optionName], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we have options[optionName]
twice? I realise it was the same before this patch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To preserve the order of keys in the config that is overriding preset. I had the same question initially 😃 Maybe an inline comment with explanation would help?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think so.
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
I discovered that
transform
option set in Jest preset is completely overridden when the same option is provided in the Jest config that extends such preset. I run into this issue when working on the preset for WordPress core: WordPress/gutenberg#4705. It turned out that behavior differs from another option that usesobject
as data representations:moduleNameMapper
. This PR tries to introduce consistency in the code and allow to merge values fromtransform
option set in both config and preset.Example
@wordpress/jest-preset-default
:jest.config.js
:Expected output:
Test plan
This change is limited to `jest-config. This is how it was tested:
yarn test packages/jest-config