-
Notifications
You must be signed in to change notification settings - Fork 209
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
Can't use v8 in Jest unit tests #246
Comments
That's unfortunate. Would you mind sharing your babel config (.babelrc or similar)? There might be a tweak you can make to the babel config in order to work around this issue. Given the amount of churn I've seen with providing both a commonjs and an es module, I wonder if the multi-module approach is leading to more trouble than it's worth? Does @lencioni or @jamesplease have any input here around how important the es module is? I'm considering a revert back to commonjs until tooling support is improved (jest, webpack, etc). But I'd like to hear from you first. 🙏 |
As I said I'm using TypeScript. But I could share my Multi-Module packaging should not be a problem as of today. For example you can look at other libraries like redux ore something else how they do accomplish that 😉 |
Go ahead 👍 It looks like |
Default exports are doomed 😉 In our project default exports are forbidden because of different reasons 😊 Would it be a good idea to change your exports to named exports and get rid of default exports completely? |
@trotzig I think it’s fine to drop the ES module, just because there’s currently no value in providing it. It is really only useful for tree shaking, but there is only one export here, so there is nothing to shake. In regards to named vs default exports, the only interop issue between these should be when you use them both at the same time. We are only using default exports here, so any issues should be with an individual developer’s setup.
It sounds to me like this issue stems from however your project is set up. I use Jest with named and default exports without an issue. tl;dr I don’t think we should remove the ES module build because of this particular issue (I’d probably just close this out until there’s more evidence that this is typical of TypeScript projects), but I’d be alright removing it for other reasons. |
Not really. We don't have any technical issues. We just dropped support for default exports because they can be difficult to maintain, refactor and their semantic could change without knowing it. Furthermore you don't know what you really import. For example But this has nothing to do with this issue. When I import import Waypoint from 'react-waypoint';
console.log(Waypoint); It gives me the component when I bundle everything with webpack but After a little bit debugging I believe I found out what's going on: in your package.json you have "main": "cjs/index.js",
"module": "es/index.js", defined. That's cool. Webpack respects that and uses TypeScript import Waypoint from 'react-waypoint';
console.log(Waypoint); gets transpiled to "use strict";
exports.__esModule = true;
var react_waypoint_1 = require("react-waypoint");
console.log(react_waypoint_1["default"]); But there is no import * as Waypoint from 'react-waypoint';
console.log(Waypoint); gets transpiled to "use strict";
exports.__esModule = true;
var Waypoint = require("react-waypoint");
console.log(Waypoint); and then it should work. I also found some issues / PR's in the Jest repository like this jestjs/jest#4842 or that jestjs/jest#5485 So I found the problem but don't have a solution 😁 Except "change your default export to a named export" or "only use CommonJS for your package". |
Thanks for the explanation. I got around this by creating a jest mock of waypoint that exports default. So in
Bit dirty, but does the job for now |
@trotzig would there be any harm in providing both the default export and a named export? |
It doesn't feel like the right solution to the problem. The harm with two exports is that people get confused about what the difference is between the two. That being said, I don't have a good solution to the original problem other than ditching the es module completely and just using a commonjs module. |
I fixed it by myself with introducing two compiler flags to my project: esModuleInterop and allowSyntheticDefaultImports. I change every import from |
I am using TypeScript, Jest and Webpack in my project. I upgraded to v8 and now I can't use
react-waypoint
in my unit tests anymore.With v7 I imported this library with
Now with v8 I have to import it like
With that default import it runs with TypeScript and Webpack as expected but not in my unit tests that run with Jest. If I change the import to the v7
import * as Waypoint
syntax TypeScript and Webpack fails, but Jest runs fine.I believe it has something to do how this library is exported in the package because in Jest I transpile everything to CommonJS (because Node only understands CommonJS) but then there is a
.default
import which doesn't exist. In Webpack I transpile everything to ESM import syntax and TypeScript take the default export and Webpack can handle that.My Workaround at the moment is to use v7 instead of v8 because there everything works out of the box.
Maybe this is related to #238? 🤔
The text was updated successfully, but these errors were encountered: