TL;DR
Just follow this post:Using Browserify To Enhance Your AngularJS Tests
And change all module in your test code to angular.mock.module, then it's good to go
This idea came across my mind when I was tired enough of writing and maintaining same codes in 2 different files for Protractor and Karma unit-test.
At the beginning I need some mocked data for my Karma unit-test, without a second thought I wrote a service specifically for generating fake data.
I felt very comfortable with the FakeData service because I can use it not only in unit-test, it also enables me to visually see some outcomes halfway in my development process.
Afterwards I started writing Protractor test and also mocked some data for http requests. Protractor use require to import some external file so I have to write another fake-data.js file with module.exports to make it available for Protractor tests.
Besides the different Angular Service and the require - module.exports mechanisms to include external javascript files, these 2 files contains exactly the same fake data for test. This totally ruins the DRY principle.
Since Karma and Protractor use different API to load dependencies, AngularJs Dependency Injection and NodeJs require-exports respectively, there seems to be no way to use a single mocked module for tests code in both frameworks.
Fortunately there is karma-browserify to the resort.
Although there is a very small cost in using it, you have to change all your module calls to angular.mock.module, otherwise you'll get the error below which frightened the shit out of me.
TypeError: '[object Object]' is not a function (evaluating 'module('spirit99')')
This is caused by name confilction of module, refer to this post
No comments:
Post a Comment