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

Fix: Import Babel config from package.json and export in babel.config.js to fix Jest tests in custom plugins that have a package.json #4782

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require("fs");

/**
* @summary Babel 7 doesn't transpile files in sub-directories that have a package.json when Babel is configured
* through package.json or .babelrc. This causes Jest test failures in custom plugins that have a package.json. It
* isn't an issue with non-test files because those are imported through the main Reaction app (see /server/plugins.js
* and client/plugins.js). Babel does transpile these files when it is configured through the new babel.config.js.
* Meteor currently only loads Babel config through .babelrc or package.json. So, in order to support Babel transpiling
* of Jest tests, we load the babel config defined in package.json and export it here.
* See this Github comment: https://github.com/facebook/jest/issues/6053#issuecomment-383632515
*/
module.exports = function (api) {
api.cache(false);

/**
* Meteor only reads the babel config from .babelrc or package.json. So with just babel.config.js,
* the Meteor app fails but the Jest tests pass. With just package.json, the Jest tests in custom plugins
* fail but the app runs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

*/
const file = fs.readFileSync("./package.json");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to add a note here mentioning that this is required because as you mentioned

Meteor only reads the babel config from .babelrc or package.json. So with just babel.config.js, the Meteor app fails but the Jest tests pass. With just package.json, the Jest tests in custom plugins fail but the app runs.

So that we're aware of why we did this and if Meteor changes or our dependency on Meteor changes we can change this in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spencern Comment added

const packageJSON = JSON.parse(file);

return packageJSON.babel;
};