standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 146 forks source link

adding -r esm to Mocha isn't working #788

Closed jclark-dot-org closed 5 years ago

jclark-dot-org commented 5 years ago

(Brand new to ES6, and no wizard with node, so apologies in advance if I'm being dense).

node 12.0 "esm": "^3.2.22" "chai": "^4.2.0", "mocha": "^6.1.4"

I've got a brand new node project; I've added mocha and chai, created an index.js and a test/test.js with the beginnings of my new module. My tests passed. Then I decided to try to use ES6 syntax, so I changed the top of my test.js from this:

let expect = require('chai').expect;
let ApexLogStream = require('../index.js');

to this:

import { expect } from 'chai'
import { ApexLogStream } from '../index.js'

Which gives an error, as node considers ES6 experimental:

> $ npm test                                    

> apex-log-stream@1.0.0 test /Users/jclark/dev/apex-log-stream
> mocha                             

/Users/jclark/dev/apex-log-stream/test/test.js:1                            
import { expect } from 'chai';                    
       ^                                                                                                                                                                                                                                                                                                                                                                                              
SyntaxError: Unexpected token {                  
    at Module._compile (internal/modules/cjs/loader.js:703:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)                                                                                                                       
    at Module.load (internal/modules/cjs/loader.js:628:32)
<snip>

So I google, and then I npm install --save esm, and I update my test command in package.json:

"test": "mocha -r esm"

and try again. Still failing, very similar error, but with a new stack trace:

> $ npm test

> apex-log-stream@1.0.0 test /Users/jclark/dev/apex-log-stream
> mocha -r esm

/Users/jclark/dev/apex-log-stream/test/test.js:1
import {expect} from 'chai';
       ^

SyntaxError: Unexpected token {
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
npm ERR! Test failed.  See above for more details.

I'm not sure how to make the import any simpler, and I've seen other examples of folks using esm with chai. What am I missing?

jdalton commented 5 years ago

Hi @jclark-dot-org!

Could you create a small repro repo that demonstrates the issue. It'll help me better diagnose the issue.

jclark-dot-org commented 5 years ago

Thanks @jdalton. https://github.com/jclark-dot-org/esm-bug is a very simple repro.

jdalton commented 5 years ago

Thanks @jclark-dot-org!

I created a PR against your simplified repro repo to fix your usage : )

jclark-dot-org commented 5 years ago

Oops, that's embarrassing. Scrolling back through my buffer, I see that I should have caught that error when testing the repro.

However, those mistakes don't exist in my original project. So I accepted your pull request, made a minor adjustment to index.js to export the old way (for parity with the original project), and retested. It failed, but for a whole different reason (and I think a valid one - It wants my module to use ES6 constructor syntax). That's great... but it's not the error I'm seeing on my original project.

After doing some more comparing, I've found the difference. I committed the change and the repro repo fails again with the same error as my original bug report. I added "type": "module" to my package.json, which I did in the original project based on https://nodejs.org/api/esm.html#esm_code_package_json_code_code_type_code_field. I guess I was conflating esm with node 12's native ES6 support (which also requires command line args). Removing "type": "module" from my original project fixes the issue there as well, so I'll just avoid it for now and use esm.

Thanks for all the help!

jdalton commented 5 years ago

Related to #784.