-
-
Notifications
You must be signed in to change notification settings - Fork 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
Example import syntax in readme doesn't work for ES6 module users #1293
Comments
|
hmm.. reading issues like this: microsoft/TypeScript#2719 I find babel quotes like this:
I think perhaps when babel said "encourage", they must have meant "confuse" if I'm reading this right, I'm getting the sense that the seems like either babel's interop, or typescript generally, has to be on the wrong side of the ES6 module fence here.. and it's not looking like typescript I don't think.. enzyme may be better off using that "commonStrict" module formatter situation that babel mentions? |
I'm afraid you're not reading it right; there are no standards about interop, and there's no "wrong side of the module fence" here - there's just "what everyone in JS has been doing for years", and this new weird thing TypeScript is imposing on its users. |
@ljharb — this classic blog post is obviously talking about the ratified standard for es6 modules, the details of which are undoubtedly in the full ecma-262 6th edition language specification 2015:
I can see it in the enzyme module source code that there is no "export with the special name default":
so enzyme is not providing a standard ES6 default export there, right? interestingly, some of the enzyme source files use es6 default exports, such as render.js:
now that right there, is a proper es6 default export — which can be imported with the proper syntax in typescript, which strives at great length to align with the evolving ecmascript standards why isn't the enzyme module similar to the render module in that way? |
this article did a great job clearing confusion for me: http://www.thedreaming.org/2017/04/28/es6-imports-babel/
please update the readme usage example with something like the following:
let me know if you'd rather me post a PR for it |
I noticed something, in the source file enzyme/mount.js:
we can see that enzyme is actually taking the internal ES6 modules, and exposing them as Common JS modules instead this is very interesting it's almost like internally, enzyme is using the newer ES6 modules — however when the functionality is presented for the consumer to use, it is transformed into the older Common JS format |
@ChaseMoskal yes, this is correct for all npm packages - node doesn't support ES Modules natively yet, so everything is transpiled to commonJS. However, babel's interop means that when you use "Enzyme is provided in Common JS format, so developers using es6 modules will need to use the import * as syntax instead:" is only true for TypeScript. Developers using ES6 Modules with every other tool don't need to do that extra step, because every other tool has chosen an interop method that avoids inflicting burden on users. |
I found a relevant typescript compiler option:
I suspect this may allow the readme usage example to work for TypeScript users, I think this is something like the equivalent of Babel's interop I think it would be useful to add a quip in the readme's usage example:
|
That one sentence seems much more palatable to me (with the order of the "either"s reversed). Want to make a PR? |
Hi. I just ran into this issue and ended up solving it using the The real compatible mode would be to simply not use a default export. This way it would work cleanly out of the box for everyone (and only users on very old-school codebases that can use neither ES6 modules nor object destructuring would have a slightly clumsier import). import { Adapter } from 'enzyme-adapter-react-16'; // babel/TS
const { Adapter } = require('enzyme-adapter-react-16'); // Node 6+ no transpilation
var Adapter = require('enzyme-adapter-react-16').Adapter; // Node 5 or older no transpilation
// A bit clumsy but not broken like the TS "fix" If I may say so, I think in 2018 supporting Typescript ought to be higher priority than offering a slightly lighter syntax to pre-2016 versions of Node.js. Maybe starting with the next version of React. |
I believe TypeScript has fixed their module issues in v2.8; Regardless, “not JavaScript” will never be a higher priority than “JavaScript” :-) |
Hello, just now I started migrating from enzyme 2 to 3, and ran into a pickle here: I found that the typescript import syntax differs from the official enzyme docs, so I'm posting my troubles and solutions to help others in the same situation.
FAIL
FAIL
SUCCEED
side question
Do I really have to do this
configure
call in every single*.test.ts
file? Jest doesn't seem to give me an entrypoint, so I guess so...The text was updated successfully, but these errors were encountered: