Mocha, Babel, and Windows symlinks
See original GitHub issueBug Report
Current Behavior Mocha is not running Babel beforehand when the project is behind a symlink on windows.
I have a small SSD and a larger HDD so to make my life easier I have a hard symlink to D:/projects in C:/Users/MyUser home directory, so can access via ~/projects.
When running mocha --require @babel/register
I’m getting the Unexpected identifier import problem. I even re-installed windows thinking it might be an environment problem before figuring out that this was the problem
Input Code
Create a simple mocha es6 project in realfoldercontainer/myproject, to be run via npm test with mocha -r @babel/register
in package.json
Create a hard symlink with mklink /j linkname realfoldercontainer
Navigate to linkname/myproject
Run npm test
Expected behavior/code
npm test
> mochatest@1.0.0 test D:\projects\mochatest
> mocha -r @babel/register
index test
sayHello function
√ should say Hello guys!
1 passing (44ms)
Actual behavior
npm test
> mochatest@1.0.0 test D:\testlink\mochatest
> mocha -r @babel/register
D:\projects\mochatest\test\index.test.js:3
import sayHello from "../src/index"
^^^^^^^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:749:23)
at Module._compile (D:\projects\mochatest\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:816:10)
at Object.newLoader [as .js] (D:\projects\mochatest\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:672:32)
at tryModuleLoad (internal/modules/cjs/loader.js:612:12)
at Function.Module._load (internal/modules/cjs/loader.js:604:3)
at Module.require (internal/modules/cjs/loader.js:711:19)
at require (internal/modules/cjs/helpers.js:14:16)
at D:\projects\mochatest\node_modules\mocha\lib\mocha.js:327:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (D:\projects\mochatest\node_modules\mocha\lib\mocha.js:324:14)
at Mocha.run (D:\projects\mochatest\node_modules\mocha\lib\mocha.js:801:10)
at Object.exports.singleRun (D:\projects\mochatest\node_modules\mocha\lib\cli\run-helpers.js:207:16)
at exports.runMocha (D:\projects\mochatest\node_modules\mocha\lib\cli\run-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (D:\projects\mochatest\node_modules\mocha\lib\cli\run.js:296:3)
at Object.runCommand (D:\projects\mochatest\node_modules\yargs\lib\command.js:238:44)
at Object.parseArgs [as _parseArgs] (D:\projects\mochatest\node_modules\yargs\yargs.js:1089:24)
at Object.parse (D:\projects\mochatest\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (D:\projects\mochatest\node_modules\mocha\lib\cli\cli.js:62:6)
at Object.<anonymous> (D:\projects\mochatest\node_modules\mocha\bin\_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:805:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:816:10)
at Module.load (internal/modules/cjs/loader.js:672:32)
at tryModuleLoad (internal/modules/cjs/loader.js:612:12)
at Function.Module._load (internal/modules/cjs/loader.js:604:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:868:12)
at internal/main/run_main_module.js:21:11
npm ERR! Test failed. See above for more details.
Babel Configuration (.babelrc, package.json, cli command)
{
"your": { "config": "here" }
}
Environment node v11.13.0
“@babel/cli”: “^7.4.3”, “@babel/core”: “^7.4.3”, “@babel/node”: “^7.2.2”, “@babel/preset-env”: “^7.4.3”, “@babel/register”: “^7.4.0”, “chai”: “^4.2.0”, “jest”: “^24.7.1”, “mocha”: “^6.0.2”
Additional context/Screenshots I can now corretctly run the tests by going directly to the folder, but it took a long time to figure this out, including a system rebuild to try and fix the problem.
I’m unsure as to whether this is a babel or mocha issue, so will be posting in both github issues pages.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
The issue here is that
@babel/register
, by default, only runs Babel on files inside the working directory and not inside anode_modules
folder. If you want a broader scope, you’ll need to configure it. For example you can do--require ./babel-register.js
and make that file:to compile all non-
node_modules
files, even those outside the working directory.Yes exactly.
Yes I realize that I should have mentioned one other thing. .babelrc config files a) only apply to files in child directories b) need to be in a folder Babel expects .babelrc configs to be in.
Like the other case, since Babel sees the file as outside of your project, there is no way for a .babelrc to apply to it. This is why Babel 7 added support for project-wide/file-path-independent configuration that can be loaded from a babel.config.js. you can read more at https://babeljs.io/docs/en/config-files#project-wide-configuration