Javascript Karma、PhantomJS 和 es6 Promises
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29391111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Karma, PhantomJS and es6 Promises
提问by Travis Parks
I am writing a JavaScript library that uses the new es6 promises. I can test the library in Firefox because promises are defined. However, when I try to test my code with Karma and PhantomJS, I get the error Can't find variable: Promise.. I am guessing this is because the PhantomJS browser doesn't support es6 promises yet.
我正在编写一个使用新 es6 承诺的 JavaScript 库。我可以在 Firefox 中测试这个库,因为 promise 已经定义好了。但是,当我尝试使用 Karma 和 PhantomJS 测试我的代码时,出现错误Can't find variable: Promise.。我猜这是因为 PhantomJS 浏览器还不支持 es6 承诺。
How can I configure Karma to bring in the polyfill for promises?
如何配置 Karma 以引入 polyfill 以实现 Promise?
回答by spikeheap
You can pull in the Babel polyfill by simply installing Babel Polyfill:
你可以通过简单地安装Babel Polyfill 来引入 Babel polyfill:
npm install --save-dev babel-polyfill
and then include the polyfill file before your source and test files within the filessection of your karma.conf.js:
然后包括前内源和测试文件的填充工具文件files的部分karma.conf.js:
files: [
'node_modules/babel-polyfill/dist/polyfill.js',
'index.js', //could be /src/**/*.js
'index.spec.js' //could be /test/**/*.spec.js
],
Unless you know that all your target browsers support Promises, you probably want to apply this polyfill to your released build too.
除非你知道你的所有目标浏览器都支持 Promises,否则你可能也想将这个 polyfill 应用到你发布的构建中。
If you're feeling really adventurous you can use Browserify to pull files in to make your testing more modular, and then use Babelify to transpile ES6 to ES5. I've created a sample project with these and a working test involving a Promise (running on PhantomJS2) for reference.
如果你真的喜欢冒险,你可以使用 Browserify 拉取文件,让你的测试更加模块化,然后使用 Babelify 将 ES6 转换为 ES5。我已经用这些创建了一个示例项目和一个涉及 Promise(在 PhantomJS2 上运行)的工作测试以供参考。
回答by gasolin
For Babel 6, we need install babel-polyfillto support promise.
对于 Babel 6,我们需要安装babel-polyfill来支持 promise。
npm install --save-dev babel-polyfill
and add a line in karma.conf.jswithin the filessection
并karma.conf.js在该files部分中添加一行
files: [
'node_modules/babel-polyfill/dist/polyfill.js',
....
]
It's well documented in https://github.com/babel/karma-babel-preprocessor#polyfill
它在 https://github.com/babel/karma-babel-preprocessor#polyfill 中有详细记录
回答by MKant
as correctly pointed out by the author it is not able to recognize es6 promise. In order to load it, es6-promise module can be loaded with the help of webpack.ProvidePlugin and configuring it inside plugins array of webpack.
正如作者正确指出的那样,它无法识别 es6 承诺。为了加载它,es6-promise 模块可以在 webpack.ProvidePlugin 的帮助下加载,并在 webpack 的 plugins 数组中配置它。
plugins: [
new webpack.ProvidePlugin({
'Promise': 'es6-promise'
})
]
This seems to work for me!
这似乎对我有用!
回答by Tony
回答by SET
You can use karma-babel-preprocessorfor files that uses ES6 features. Install it with
您可以将karma-babel-preprocessor用于使用 ES6 功能的文件。安装它
npm install --save-dev karma-babel-preprocessor
npm install --save-dev karma-babel-preprocessor
and then add specify what files should be preprocessed you karma.conf:
然后添加指定应该预处理哪些文件karma.conf:
preprocessors: {
"src/**/*.js": ["babel"],
"test/**/*.js": ["babel"]
},

