Mocha: Can not transpile Coffescript when running mocha command

Created on 6 Sep 2018  路  9Comments  路  Source: mochajs/mocha

Prerequisites

  • [x] Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • [x] Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • [x] 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • [x] Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.

Description

I try to run Mocha with a CoffeeScript compiler and I get Unexpected token import.
Below is my command which I'm trying to run:
node_modules/mocha/bin/mocha --require coffeescript/register app/server/lib/server_tests/*.test.coffee --reporter mochawesome

Here is the error stack trace
````
import should from 'should';
^^^^^^

SyntaxError: Unexpected token import
at new Script (vm.js:51:7)
at createScript (vm.js:136:10)
at Object.runInThisContext (vm.js:197:10)
at Module._compile (module.js:613:28)
at Object.loadFile (/Users/timofte/Documents/work/projects/sonar/node_modules/coffeescript/lib/coffeescript/register.js:18:19)
at Module.load (/Users/timofte/Documents/work/projects/sonar/node_modules/coffeescript/lib/coffeescript/register.js:53:36)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)
at Module.require (module.js:593:17)
at require (internal/module.js:11:18)
at /Users/timofte/Documents/work/projects/sonar/node_modules/mocha/lib/mocha.js:250:27
at Array.forEach (:null:null)
at Mocha.loadFiles (/Users/timofte/Documents/work/projects/sonar/node_modules/mocha/lib/mocha.js:247:14)
at Mocha.run (/Users/timofte/Documents/work/projects/sonar/node_modules/mocha/lib/mocha.js:576:10)
at Object. (/Users/timofte/Documents/work/projects/sonar/node_modules/mocha/bin/_mocha:637:18)
at Module._compile (module.js:649:30)
at Object.Module._extensions..js (module.js:660:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)
at Function.Module.runMain (module.js:690:10)
at startup (bootstrap_node.js:194:16)
at bootstrap_node.js:666:3

``` It works if I change that line toshould = require 'should'`

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 5.2.0
  • The output of node --version:v9.8.0
  • The version and architecture of your operating system: MacOS 10.13
  • Your shell (bash, zsh, PowerShell, cmd, etc.): Terminal
  • Your browser and version (if running browser tests): Google Chrome Version 68.0.3440.106
  • Any other third party Mocha related modules (with versions): mochawesome - v3.0.3
  • The code transpiler being used: coffeescript - v2.3.1
needs-feedback question stale

Most helpful comment

Hi, I don't have any updates on this yet.

It could be just a CoffeeScript issue. I'll have to investigate more when I got some time.

Thanks for your help. If I'll find a solution I'll post it here, maybe it will help others.

All 9 comments

About halfway down the stacktrace ("mocha.js:250"), Mocha attempts to load your "require'd" coffeescript registration - it blows up during that processing here.

My current CoffeeScript experience is with v1.12.7, where my code uses the style documented here (i.e., like your "It works if I" code). Think your import should from 'should'; is invalid CoffeeScript which is why it implodes.

CoffeeScript 2 supports import. I use that code in other places in my meteor app and it works. If I run these tests from meteor with mocha it works just fine.

My problem came when I've tried to use mochawesome as a reporter and I have to run mocha command for that.

So it does, I now see.

Does it work with the "spec" reporter?

Mocha works in Meteor environment, but only with the terminal reporter, with this command: "TEST_WATCH=1 meteor test --port 3100 --driver-package meteortesting:mocha",

The web one is an empty page as follows:

screen shot 2018-09-06 at 16 35 52

That's why I'm tying to add a custom reporter, running mocha command as described in the issue.

Assuming what you're calling the terminal reporter is the same as "spec" reporter. What happens when you use the "html" reporter then?

If I run node_modules/mocha/bin/mocha --require coffeescript/register app/server/lib/server_tests/*.test.coffee --reporter html, I get the same error.

I meteor I'm not able to pass any mocha arguments to my meteor test command.

I'm not sure if this helps.

Thanks for your time and patience.

Well, at least that eliminates "mochawesome" reporter from the issue.

And you could specify arguments in "mocha.opts", if we knew what.

Could be bootstrapping issue (code that CS would use to make use of import not loaded at point where initial compilation attempted). Maybe try --require something else prior to CoffeeScript registration.

How about:

$ node_modules/mocha/bin/mocha --require should --require coffeescript/register app/server/lib/server_tests/*.test.coffee --reporter html

@elisei-timofte Do you have any updates regarding this?

I do not think this is a mocha issue, rather than how you are configuring coffee script to run through mocha.

Hi, I don't have any updates on this yet.

It could be just a CoffeeScript issue. I'll have to investigate more when I got some time.

Thanks for your help. If I'll find a solution I'll post it here, maybe it will help others.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Swivelgames picture Swivelgames  路  3Comments

luoxi001713 picture luoxi001713  路  3Comments

EdvinOlofsson picture EdvinOlofsson  路  3Comments

eschwartz picture eschwartz  路  3Comments

delta62 picture delta62  路  3Comments