简体   繁体   中英

failing to import with jasmine and babel

I am trying to install jasmine to my project, but I keep getting the error

> jasmine

/home/munhunger/develop/dnd/dmScreen/src/lib/quadTree.spec.js:1
import quadTree from './quadTree';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Loader.requireShim [as require_] (/home/munhunger/develop/dnd/dmScreen/node_modules/jasmine/lib/loader.js:35:3)
    at /home/munhunger/develop/dnd/dmScreen/node_modules/jasmine/lib/loader.js:28:12
    at new Promise (<anonymous>)

This seems to be a fairly common issue, and I've seen a few questions about it here on SO. For example here Run javascript es6 code in Jasmine

It seems straightforwards and I've done as described there (to the best of my knowledge). But I still get the same error.

So I have babel installed, with this config

//.babelrc
{
    "presets": ["@babel/preset-env"]
}

and I have configured a helper in my jasmine conf

//spec/support/jasmine.json
{
    "spec_dir": "src",
    "spec_files": ["**/*.[sS]pec.js"],
    "helpers": ["helpers/**/*.js", "../node_modules/@babel/register/lib/node.js"],
    "stopSpecOnExpectationFailure": false,
    "random": false
}

I am running it without any special config

"scripts": {
    "test": "jasmine"
}

But I still can't run my tests. So am I missing something obvious, or why isn't this working?

Here is the git repo(and commit) where it is failing https://github.com/munHunger/dnd/tree/e2ab4c7d3cd78b01449e41679bdf6ad363ce711c/dmScreen

I had a similar issue when attempting to set up jasmine with typescript.

I eventually traced it to the "spec_dir" config entry causing the issue!

If you remove that line and add the value as a prefix to your "spec_files" entries, it may work. That did the trick for me.

// jasmine.json
{
  "spec_files": ["src/**/*.[sP]pec.js"],
...
}

Feels like a bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM