微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jasmine测试依赖项:无法在模块外部使用import语句

如何解决Jasmine测试依赖项:无法在模块外部使用import语句

我知道有很多“无法在模块外使用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"
  ]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。