-
Notifications
You must be signed in to change notification settings - Fork 84
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
Auto resolve imports to lwc/componentFoo/__mocks__/componentFoo.js #379
Auto resolve imports to lwc/componentFoo/__mocks__/componentFoo.js #379
Conversation
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.
Thanks a lot for the PR!
This looks good to me at a first glance, although there are lint errors in CI.
Sadly it may not be possible to test this given the current test structure in this project.
Question: does this supersede salesforce/lwc-test#260 or are you looking to get both merged?
@nolanlawson There's is one thing before we progress. Currently it will resolve the mock first and then fallback to the implementation. Which is great for all jests generally to avoid tests spanning over components. However, as it is currently when you want to test the actual component. This would break everyone's tests if they upgraded! It doesn't look like we have enough context in the resolve to make a distinction on import from test file vs component file. I'll need to look at the transformer |
As for salesforce/lwc-test#260, yes looking to get both merged |
This isn't even possible for parent-child implicit imports – e.g. Rather than doing this at the transformer level, I wonder if it would be cleaner to expose a config or something that can be set in the test: import { setComponentsToMock } from 'somewhere'
beforeEach(() => {
setComponentsToMock(['c/foo', 'c/bar'])
})
afterEach(() => {
setComponentsToMock([])
}) |
I would expect that any component referenced would use the mock if it is there. Such that tests are shallow and only test the referenced component. So The import It seems clean to me without having to have additional config or per test file setups. If there was a way to follow jests requireActual() then you could override the test to use the true implementation. If you have any ideas for this, happy to implement |
Some more exploring with Jest and LWC. This may be unnecessary. It's convenient to automatically use any manual mocks created, however, with jest you can use
|
Options I see. Manual mocks in
|
Thanks for providing so many details! This is helpful for anyone else trying to solve this problem. 💪 |
Update LWC resolver to attempt to resolve to
**/lwc/componentName/__mocks__/componentName.js
first, then fallback to the actual implementation.This avoids having to set many
moduleNameMapper
items or defining mocks in a separate directory from the components which complicates upkeep.If there was a way to honor
jest.requireActual()
that would be great, but I'm not familiar with how to implement that