在Jasmine中运行javascript es6代码 [英] Run javascript es6 code in Jasmine

查看:202
本文介绍了在Jasmine中运行javascript es6代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在angularjs应用程序中浏览JavaScript es6代码,并使用grunt babel将es6编译为es5.

I am exploring JavaScript es6 code in angularjs app and used grunt babel to compile the es6 to es5.

我的单元测试(茉莉花)未使用phantomjs与es6代码一起运行.

My unit test (jasmine) doesn't run with es6 code using phantomjs.

进行测试的最佳方法是什么? jasmine是否可以使用任何插件来运行es6代码?

Whats best way to run test? Is there any plugin to use for jasmine to run es6 code?

推荐答案

您可以将Jasmine配置为使用Babel作为帮助程序,并即时转换代码.

You can configure Jasmine to use Babel as a helper and transform your code on the fly.

安装babel-register模块:

npm install --save-dev babel-register

并将其注册为茉莉花助手

And register it as a Jasmine helper

在您的spec/support/jasmine.json文件中进行以下更改:

In your spec/support/jasmine.json file make the following changes:

{
  "helpers": [
    "../node_modules/babel-register/lib/node.js"
  ]
}

有关更多信息,请参见Github上的 piecioshka/test-jasmine-babel 存储库.

For more information see the piecioshka/test-jasmine-babel repository on Github.

Babel 6.x出厂时未启用任何转换.您需要明确告诉它要运行哪些转换.您已经在使用Babel,因此应该安装这些模块.如果没有,您可以使用npm安装ES2015 Preset:

Babel 6.x does not ship with any transformations enabled by default. You need to explicitly tell it what transformations to run. You are already using Babel so those modules should be installed. If not, you can install the ES2015 Preset using npm:

npm install babel-preset-es2015 --save-dev

假设您已经安装了Babel和ES2015 Preset,要启用它,必须在.babelrc文件中对其进行定义,如下所示:

Assuming you have installed Babel and the ES2015 Preset, in order to enable it you have to define it in your .babelrc file, like this:

{
  "presets": ["es2015"]
}

这篇关于在Jasmine中运行javascript es6代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆