问题描述
我知道有很多“无法在模块外使用import语句”线程,但是对于我发现的茉莉花却没有。我有一个使用Preact,Parcel和Openlayers的客户端应用程序。我的大部分代码都位于Map.js模块中,我正在尝试设置Jasmine对其中的功能进行一些测试。
我已经安装了茉莉花(后来在茉莉花es6上),babel-cli,babel-present-env,babel-preset-es2015,但是每当我运行jasmin命令时,导入依赖项时测试都会失败。我的spec文件能够导入我的Map.js,但是无法导入Map.js的依赖项,例如 导入... node_modules \ ol \ control \ MousePosition。
我的json.package具有类型:模块。
我的.babelrc文件:
{
"presets": ["@babel/preset-env"]
}
jasmine.json:
"spec_dir": "/spec","spec_files": [
"**/*[sS]pec.js"
],"helpers": [
"../node_modules/@babel/register/lib/node.js"
],"stopSpecOnExpectationFailure": false,"random": true
}
示例规范:
import MapMod from "../src/Map"
describe('Testing map initialization',() => {
beforeEach(() => {
// I've tried require() here but get the same error
});
it('Should not throw errors',function () {
expect(MapMod.initMap).not.toThrow();
});
})
我很困惑。有人可以帮忙吗?
解决方法
"helpers": [
"../node_modules/@babel/register/lib/node.js"
]
config 在更高版本的 @babel/register
(> 7.12.1
) 上似乎对我不起作用。
相反,我创建了一个名为 babel.js
的新助手,其中包含以下内容:
require('@babel/register')
并将其作为帮助器代替 ../node_modules/@babel/register/lib/node.js
:
"helpers": [
"spec/helpers/babel.js"
]