Improve handling of transpiled packages in unit tests#14432
Conversation
| const glob = require( 'glob' ).sync; | ||
|
|
||
| // Finds all packages which are transpiled with Babel to force Jest to use their source code. | ||
| const transpiledPackageNames = glob( 'packages/*/src/index.js' ) |
There was a problem hiding this comment.
This will also pick docgen (which shouldn't be transpiled but at the moment it is).
There was a problem hiding this comment.
Yes, this is the same story as with transpliling it :) We should address it separately.
|
|
||
| jest.mock( '@wordpress/dom', () => { | ||
| const focus = require.requireActual( '@wordpress/dom' ).focus; | ||
| const focus = require.requireActual( '../../../../dom/src' ).focus; |
There was a problem hiding this comment.
Can we add @worpress/dom back to the jest config?
There was a problem hiding this comment.
require.requireActual is problematic. If you use as @wordpress/dom, it will load it from node_modules and try to use main from packages.json - it skips module name mapper (edited). The mocking logic has its own life 🙂
| const glob = require( 'glob' ).sync; | ||
|
|
||
| // Finds all packages which are transpiled with Babel to force Jest to use their source code. | ||
| const transpiledPackageNames = glob( 'packages/*/src/index.js' ) |
There was a problem hiding this comment.
I'm wondering if we ought to start codifying / documenting where we have these conventions, i.e. "sources should have a src/index.js file".
There was a problem hiding this comment.
I would not only document them but maybe try to go further and centralize the source of those lists. I will open a follow-up issue.
Description
When working on #14168 we encountered some issues with using
@wordpress/browserlist-configwith unit tests. I did some more debugging and I discovered that there are a few places where we use transpiled code rather than original source code. It's important that we use source code becausenpm run test-unitdoesn't trigger packages to be rebuilt every time source code changes. This often led to a frustrating experience when changes weren't picked up by tests. To mitigate that I'm proposing a solution which whitelists all packages that are transpiled with Babel to always use their source code.The proposed solution will also remove another list which had to be maintained manually and led to subtle bugs hard to fix like the one in #14168.
Testing
Remove all
buildandbuild-modulefolders from your packages. Runnpm run test-unitand make sure that all of them pass.