Skip to content

Instantly share code, notes, and snippets.

@Kikketer
Created February 19, 2016 17:44
Show Gist options
  • Save Kikketer/1646eccdaff76944b358 to your computer and use it in GitHub Desktop.
Save Kikketer/1646eccdaff76944b358 to your computer and use it in GitHub Desktop.
Babel + Angular 1 + Promises not resolving
// Reference: http://karma-runner.github.io/0.12/config/configuration-file.html
module.exports = function karmaConfig (config) {
config.set({
frameworks : [
// Reference: https://github.com/karma-runner/karma-jasmine
// Set framework to jasmine
'jasmine'
],
reporters : [
// Reference: https://github.com/mlex/karma-spec-reporter
// Set reporter to print detailed results to console
'progress',
// Reference: https://github.com/karma-runner/karma-coverage
// Output code coverage files
'coverage'
],
files : [
// Add the polyfill manually (THIS MAY BE THE ISSUE? Couldn't get Promise to work any other way)
'node_modules/babel-polyfill/dist/polyfill.min.js',
// Grab all files in the app folder that contain .spec.
'src/tests.webpack.js'
],
preprocessors : {
// Reference: http://webpack.github.io/docs/testing.html
// Reference: https://github.com/webpack/karma-webpack
// Convert files with webpack and load sourcemaps
'src/tests.webpack.js' : ['webpack', 'babel']
},
browsers : [
// Run tests using PhantomJS2
'PhantomJS'
],
singleRun : true,
// Configure code coverage reporter
coverageReporter : {
dir : 'coverage/',
reporters : [
{type : 'text-summary'},
{type : 'html'}
]
},
webpack : require('./webpack.unit-test.config'),
babelPreprocessor: {
options: {
presets: ['es2015'],
sourceMap: 'inline'
}
},
// Hide webpack build information from output
webpackMiddleware : {
noInfo : true
},
plugins: [
require('karma-webpack'),
require('karma-jasmine'),
require('karma-phantomjs-launcher'),
require('karma-coverage'),
require('karma-spec-reporter'),
require('karma-babel-preprocessor')
]
});
};
// Lots of setup up here
// Removed for brevity
it('should get an individual project', function () {
let response;
// mockProject is just a simple JSON object
httpBackend.expectGET('projects/' + mockProject.id).respond(mockProject);
// Just a test to see if promise doesn't throw an error as a variable
new Promise((resolve, reject) => {
resolve();
})
.then(() => {
// This is never fired
console.log('resolved test');
});
// Now actually call it
projectsService.getProject(mockProject.id)
.then((project) => {
// This is NEVER fired
console.log('promise resolved!');
response = project;
});
// Now we have to flush that http request
httpBackend.flush();
// Run the expect... this is always failing since response is empty
expect(response).toEqual(mockProject);
});
// I've included this file just to show nothing fancy happening here.
// This is the main file that the karma.conf is bringing in (Besides the polyfill)
import 'angular';
import 'angular-mocks/angular-mocks';
// I tried to:
// import 'babel-polyfill'
// but I got a global._babelPolyfill error (window doesn't exist?)
var testsContext = require.context(".", true, /.spec$/);
console.log(testsContext);
testsContext.keys().forEach(testsContext);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment