-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will also pick docgen
(which shouldn't be transpiled but at the moment it is).
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.
Yes, this is the same story as with transpliling it :) We should address it separately.
@@ -14,7 +14,7 @@ import { Component } from '@wordpress/element'; | |||
import Disabled from '../'; | |||
|
|||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add @worpress/dom
back to the jest config?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
#14508 is up for grabs :)
Description
When working on #14168 we encountered some issues with using
@wordpress/browserlist-config
with 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-unit
doesn'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
build
andbuild-module
folders from your packages. Runnpm run test-unit
and make sure that all of them pass.