Karma, PhantomJS and es6 Promises
Solution 1
You can pull in the Babel polyfill by simply installing Babel Polyfill:
npm install --save-dev babel-polyfill
and then include the polyfill file before your source and test files within the files
section of your 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.
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.
Solution 2
For Babel 6, we need install babel-polyfill
to support promise.
npm install --save-dev babel-polyfill
and add a line in karma.conf.js
within the files
section
files: [
'node_modules/babel-polyfill/dist/polyfill.js',
....
]
It's well documented in https://github.com/babel/karma-babel-preprocessor#polyfill
Solution 3
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.
plugins: [
new webpack.ProvidePlugin({
'Promise': 'es6-promise'
})
]
This seems to work for me!
Travis Parks
I'm a developer who spends most of his time writing business applications in .NET. I especially enjoy computing theory, application design and good programming practices. I try to keep my skills up-to-date and keep my knowledge broad. I love creating open source projects; check me out on GitHub. I consider myself an expert at .NET, software design, automation and large-scale distributed enterprise architectures.
Updated on July 09, 2022Comments
-
Travis Parks almost 2 years
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.How can I configure Karma to bring in the polyfill for promises?
-
Artjom B. about 9 yearsQuick test shows that PhantomJS 2.0.1-dev doesn't support Promises.
-
Chris over 8 yearsI'm having a similar issue but after including the polyfill, the Promise never seems to resolve, here's a gist: gist.github.com/Kikketer/1646eccdaff76944b358 Anyone have a clue why the Promise would never run it's '.then' ?
-
spikeheap over 8 years@Chris your problem doesn't look at all related. I couldn't run your gist, but it looks like you're problem is probably related to Angular. Don't use the Promise polyfill with Angular – you need to use the built-in $q implementation (otherwise it won't work with the digest cycle). If you want to build a resolved promise use
$q.when(some_object)
. You really need to open a new question on SO, but post the link here and I'll take a look. -
Chris over 8 years@spikeheap yea I switched to $q throughout the app and it worked. Thanks for looking at it.
-
Aditya Sinha about 8 yearsShouldn't using github.com/webpack/karma-webpack with babel and having its configuration section in karma.conf.js as well as the
proprocessor: { './tests/**/*.js', : [ 'webpack' ] }
be enough? Why would I also have to add the polyfill manually, when webpack adds it in normal circumstances? -
spikeheap about 8 yearsHi @Henrik, it sounds like you've answered your own question ;). This question/answer is useful for the huge number of people not using Webpack, but you're right: if you're using a system which is already polyfilling for you, don't manually add a polyfill.
-
Axel Meier almost 8 yearsI solved the same issue with using promise-polyfill: npmjs.com/package/promise-polyfill
-
Jelle den Burger over 7 yearsUnfortunately this doesn't work. I was already using the
babel-preprocessor
and Karma couldn't find thePromise.resolve()
function. Thebabel-polyfill
fixed the issue for me.