Help with karma/mocha and ngMocks?
Hello,
I'd love some help setting up karma / mocha / expect with an ES6 webpack typescript angular1.x and angular-mocks.
For whatever reason the testing suite can find the module correctly, but not the controller. Note: the actual bundle works brilliantly in the browser, just for whatever reason Karma says 'nope'.
Here's the error I'm getting:
Error: [ng:areq] Argument 'AppController' is not a function, got undefined
http://errors.angularjs.org/1.5.0/ng/areq?p0=AppController&p1=not%20a%20function%2C%20got%20undefined
I can see AppController just fine in the test bundle in chrome, so I'm not sure what's up.
Here's my spec:
/// <reference path="../typings/tsd.d.ts" />
const expect = chai.expect;
import AppModule from './app.ts';
describe('App', function() {
let makeController : Function;
let makeService : Function;
let scope : any;
beforeEach(angular.mock.module(AppModule));
it('App Module should be properly configured with UI router', function() {
let module = AppModule
expect(module.requires[0]).to.equal('ui.router');
});
describe('Controller', () => {
beforeEach(angular.mock.inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
makeController = function() {
return $controller('AppController',{
'$scope': scope
});
};
}));
it('AppController should have social media accounts', function() {
let controller = makeController();
console.log(controller);
expect(controller).to.have.property('socialMediaAccounts');
});
})
});
which is being loaded by a spec-loader:
import 'angular';
import 'angular-mocks';
// list of all specs
import "./src/app-spec.ts";
Here's my module:
import 'angular'
import 'angular-ui-router'
import AppConfig from './app-config.ts'
import AppController from './app-controller.ts'
import AppService from './app-service.ts'
const AppModule = angular.module('paynoattnApp', ['ui.router'])
.config(AppConfig)
.controller('AppController', AppController)
.service('AppService', AppService);
export default AppModule;
And my karma.conf.js:
module.exports = function (config) {
config.set({
// base path used to resolve all patterns
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'chai'],
// list of files/patterns to load in the browser
files: [
'spec-helper.ts',
'typings/tsd.d.ts'
],
// files to exclude
exclude: [],
plugins: [
require("karma-chai"),
require("karma-chrome-launcher"),
require("karma-mocha"),
require("karma-mocha-reporter"),
require("karma-sourcemap-loader"),
require("karma-webpack")
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: { 'spec-helper.ts': ['webpack', 'sourcemap'] },
webpack: {
devtool: 'inline-source-map',
module: {
loaders: [
{
test: /\.ts$/,
loader: 'babel-loader?presets[]=es2015!ts-loader',
exclude: /node_modules/
},
{
test: /\.scss$/,
loader: 'style-loader!css-loader!autoprefixer-loader?{browsers:["last 5 version", "ie >= 11"]}!sass'
}
]
}
},
webpackServer: {
noInfo: true // prevent console spamming when running in Karma!
},
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['mocha'],
// web server port
port: 9876,
// enable colors in the output
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,
// toggle whether to watch files and rerun tests upon incurring changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// if true, Karma runs tests once and exits
singleRun: false
});
};
and my tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "es6"
},
"files": [
"./src/app.ts",
"./typings/tsd.d.ts",
"./src/app-spec.ts"
]
}
Any help would be appreciated, I've been struggling with this for 3 days now. :(
Tried other solutions from other TypeScript projects and nothing seems to work.
And here's a link to the repo: https://github.com/paynoattn/angular-paynoattn
I haven't looked at this in any detail, but have you seen:
https://github.com/Microsoft/TypeScriptSamples/tree/master/es6-babel-react-flux-karma and http://templecoding.com/blog/2016/02/02/how-to-setup-testing-using-typescript-mocha-chai-sinon-karma-and-webpack/
I've looked at those yes. I've gotten everything working except the typescript/es6 and ng-mocks aspect, so this is more for needing help with that. You can see the documentation for angular-mocks here: https://docs.angularjs.org/guide/unit-testing.
I based this project off https://github.com/AngularClass/NG6-starter mostly. For their controller testing they're simply creating a New() of their class. This doesn't work for me because 1) I'm not using a class for my controller, and 2) even when I try converting it it doesn't work.
Sorry if this seems off topic. I just feel like if I post elsewhere people will be like 'What's this typescript thing you're talking about?'
Unfortunately I don't personally use any of those technologies (angular, karma, ~~mocha~~ (doh)). I'll leave this open in case anybody else can help and I'll try to take a deeper look at some point but I'm not sure how much help I can be.
Here's the fix.
/// <reference path="../typings/tsd.d.ts" />
const expect = chai.expect;
import AppModule from './app.ts';
import AppService from './app-service.ts';
describe('App', function() {
let makeController : Function;
let makeService : Function;
let scope : any;
let $rootScope : any;
beforeEach(angular.mock.module('paynoattnApp'));
let module = AppModule
it('App Module should be properly configured with UI router', function() {
expect(module.requires[0]).to.equal('ui.router');
});
describe('Controller', () => {
beforeEach(angular.mock.inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
makeController = function() {
return $controller('AppController',{
'$scope': scope
});
};
}));
it('AppController should have social media accounts', function() {
let controller = makeController();
console.log(controller);
expect(controller).to.have.property('socialMediaAccounts');
});
})
describe('Service', () => {
beforeEach(angular.mock.inject((_$rootScope_) => {
$rootScope = _$rootScope_;
makeService = () => {
return new AppService();
};
}));
it('AppService should have social media accounts', function() {
let service = makeService();
expect(service).to.have.property('socialMediaAccounts');
})
})
});
Notice to invoke your module
beforeEach(angular.mock.module('myAppNameHere'));
then
let module = AppModule
where AppModule is the name for the module you are importing. This is the only way I've found for everything to work correctly together.
If you are using a class (for instance my Service)
All you have to do is create a new instance of the class, like so:
beforeEach(angular.mock.inject((_$rootScope_) => {
$rootScope = _$rootScope_;
makeService = () => {
return new AppService();
};
}));
But if you want to test a controller (or directive, filter, etc) as a function:
beforeEach(angular.mock.inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
makeController = function() {
return $controller('AppController',{
'$scope': scope
});
};
}));
This is powerful because in this case you are testing the Angular output of your controller, directive, etc after the modules have been bundled, rather then your module itself.
@jbrantly Feel free to include my project in the examples section of the documentation. (I can do a pull request if you need).
Thanks for this awesome plugin!!
Feel free to include my project in the examples section of the documentation.
Done.
Glad you got it figured out!