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

Improve handling of transpiled packages in unit tests #14432

Merged
merged 1 commit into from
Mar 14, 2019

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Mar 14, 2019

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 because npm 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 and build-module folders from your packages. Run npm run test-unit and make sure that all of them pass.

@gziolo gziolo added [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. [Type] Code Quality Issues or PRs that relate to code quality labels Mar 14, 2019
@gziolo gziolo added this to the 5.3 (Gutenberg) milestone Mar 14, 2019
@gziolo gziolo self-assigned this Mar 14, 2019
@gziolo gziolo requested a review from oandregal March 14, 2019 13:16
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' )
Copy link
Member

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).

Copy link
Member Author

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;
Copy link
Member

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?

Copy link
Member Author

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 🙂

@gziolo gziolo merged commit dc03691 into master Mar 14, 2019
@gziolo gziolo deleted the update/jest-config-babel branch March 14, 2019 16:40
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' )
Copy link
Member

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".

Copy link
Member Author

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.

Copy link
Member Author

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants