Angular test reporting options? - angularjs

I find the Angular console test reporting awkward to read, it just a big pile of console text with next to no formatting.
Is it possible to get the Angular unit testing reporting to appear in the browser using html for formatting? I noticed this github repo the other day - https://github.com/larrymyers/jasmine-reporters
Is it possible to use the html reporter in that library for Angular unit test reporting..can I have the results of Angular unit tests shown in a browser?
I know there is a 'reporters' config option in the karma test runner file used for Angular testing and it has the following options -
dots,
progress,
junit,
growl,
coverage
However these seem to do absolutely nothing, no matter what I set them to, and I couldn't find any documentation on them.
So what is the purpose of the reporters option in karma.conf.js?

I personally use the coverage option. But for this to work you need to setup Istanbul ( https://github.com/yahoo/istanbul ) Coverage reporter separately. This will give you in HTML format the status of what files and lines in files are being tested, it will not show the actual tests written.
I don't think any of the reporters will actually print out the individual tests in HTML format. Try using Webstorm, it displays the individual tests in an easy to read format.
I've tried the karma-html-reporter module from npm, but it hasn't worked for me, so maybe see if they've updated that one.

I run karma from IntelliJ 13.x and am able to get a clean html format for the test output using the following configuration options in karma.conf.js in the config.set section :
reporters: ['progress', 'junit', 'html']
plugins : ['karma-htmlfile-reporter', ...] (karma-jasmine, etc...)
htmlReporter: { outputFile: 'tests/units.html' }
After my tests run I can just right click on the test/units.html and open in browser to see a formatted version of the results including color coding of results. Of course you will need to install any plugins or dependencies to get the test to run.

If you've been developing your project using the Angular-CLI, you can just run a
ng test --code-coverage
with the following (or similar) config in your karma.conf.js file
coverageIstanbulReporter: {
reports: ['html', 'lcovonly'],
fixWebpackSourcePaths: true,
thresholds: {
global: { // thresholds for all files
statements: 0,
lines: 0,
branches: 0,
functions: 0
},
each: { // thresholds per file
statements: 0,
lines: 0,
branches: 0,
functions: 0
}
}
},
honestly, if not, the Karma Istanbul coverage tool is excellent and relatively simple to install.

For code coverage I use the newer version of karma-coverage-istanbul-reporter. It builds a report website based on the angular-cli code coverage output. You can drill down into your components and services to see where your unit test coverage is lacking. There is also an aggregation of coverage at the top. The output will create a coverage folder in your defined destination, just load the index.html for the report.
I wanted to note that by default this will include all files without .spec.ts. Use the following to exclude models and other files from coverage in your .angular-cli.json test section:
"codeCoverage" : {
"exclude": ["./src/test/*mocks.ts",
"./src/environments/*.ts",
"./src/app/models/*model.ts"
]
}

Related

Jest coverage report in html missing coverage values

I am unit testing my ReactJS app with Jest and have added configuration to get the code coverage. The coverage works well on cli but when I try to export coverage report as html the values corresponding to each file are not shown in the report. Here is my jest.config.json:
{
"roots": [
"test"
],
"collectCoverage": true,
"coverageReporters": ["text","html"],
"coverageDirectory": "<rootDir>/coverage/"
}
And here is my report generated in coverage folder inside index.html file:
As can be seen the numbers are missing. What coud be the reason when the text report has correct values shown as well. It happens only with html report.
I had this as well. Updating the package istanbul-reports to the latest version (3.0.0 at the time) fixed the issue.

configure remap-istanbul to include all source files

I am configuring my application developed on Ionic 2 with Angular 2 and TS to generate code coverage reports generated for my test files.
I am using Jasmine , Karma and remap-istanbul for Unit Testing and generating coverage reports.
refering to this wonderful post: twofuckingdevelopers.com/2016/01/testing-angular-2-with-karma-and-jasmine/
However, I am not able to list out files that do not have a spec file written for them. Is there a way to include the same in istanbul reports and generate overall coverage accordingly.
Thank You!!!
In your Karma config file set the following option:
coverageReporter: {
includeAllSources: true
}

jest testing with es6 + jspm + systemjs + reactJS

I'm trying to figure out how to make my unit tests in my reactJS ES6 application. My application is already using es6 module system, transpiled with jspm/babel to systemJs.
I found babel-jest as preprocessor but even with it, I can't run my tests because jest can't find SystemJs. ("System is not defined" error is shown in the console)
In the browser, as explained in jspm documentation, SystemJs is loaded globally. I guess I should load SystemJs inside my preprocessor, but How can I make systemJs available for loading additional modules in my tests?
Thanks in advance
Unfortunately, Jest does not support SystemJS ES6 modules at the moment.
See the following comments:
So it sounds like jest assumes that your modules resolve based on the Node resolution algorithm.
Unfortunately this isn't compatible with SystemJS's resolution algorithm.
If there was a way in jest to set a "custom resolver" algorithm through an API then we could plug jspm into jest, but I'm not sure if this is currently possible.
-- Comment by Guy Bedford, creator of SystemJS, Jun 2015
It is unlikely there'll be official support for [SystemJS] unless it is a community contribution.
-- Comment by #cpojer, Jest Collaborator, Jan 2016
Also see the following issue: Invalid URL is thrown when requiring systemjs in jest test cases
in essence to get Jest to play nice with an app running on JSPM/SystemJS you need to "teach" it about all the mapping it holds in the config.js file (or however you call System.config())
the long answer is that you need to create an entry for each dependency you have installed with JSPM like this:
//jest configuration
moduleNameMapper: {
...
"^redux$": "redux#3.6.0",
...
}
for each alias you have, you need at least one entry:
moduleNameMapper: {
...
"^common\\/(.*)": "<rootDir>/src/common/$1", //for a dir alias
"^actions$": "<rootDir>/src/actions/index", //for a file alias
...
}
you also need to have these mappings in your nodeNameMapper:
moduleNameMapper: {
...
"^npm:(.*)": "<rootDir>/jspm_packages/npm/$1",
"^github:(.*)": "<rootDir>/jspm_packages/github/$1",
...
}
and finally, you need to have this moduleDirectories config:
moduleDirectories: ["node_modules", "jspm_packages/npm", "jspm_packages/github"]
this isnt really practical because you dont want to keep two copies of all your dependencies registry and have to sync them when they change...
so the short and better answer, you use my gulp-jest-jspm plugin :)
even if you dont use gulp, you can use its getJestConfig() method to generate the config for you before you run Jest.

How do I exclude files from unit test

Hi I am trying to exclude a file from the karma coverage report
I have looked through the web and people say the following line should work
in Karma.conf.js
preprocessors: {
'web/scripts/**/*.js': ['coverage'],
'app/scripts/exp/!(expfile|fun).js': ['coverage']
},
However, I still see them in my code coverage report.
I am not sure how to exclude them as my coverage percentage are greatly affected by these two files.
Thanks for the help!
Make sure that the directories - files you want to exclude are not loaded by any other include.

How to run angularJS tests in intellij idea 11.1.3?

I am new to AngularJS to JS in general. Now I want to use JSTestDriver and behavior driven development framework Jasmin . As I understood AngularJS works with Jasmine and test driver. I am working with Ideal Intellij 11.1.3. I added the plug in for JsTestdriver to It and run some test as described here http://code.google.com/p/js-test-driver/wiki/IntelliJPlugin. Now here is the problem. When I write some Jasmine tests like this one
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});
, for the first time indea intellij asked me to download the jasmine adapter and I did, but when I run the test I got this message : unable to attach test reporter to test framework intellij. I searched google for solutions, some guy posted how to run the angular tutorials: here : https://groups.google.com/forum/?fromgroups=#!topic/angular/LdjNsZD69Uk.
he uses a configuration files that comes with Angular js. and Node.js should I install them too ? isn't there any way to automatically do this from ideal intellij ?
What files structure should I have ?
Any help, link or suggestion will be great.
I fixed my jsTestDriver.conf file and it's paths, but now I get this problem :
Testing started at 1:31 PM ...
Cannot read [
/tmp/.m2/repository/org/testng/testng/6.7/testng-6.7.jar!/testng-reports.js
/home/clouway/.m2/repository/org/testng/testng/6.7/testng-6.7.jar!/testng-reports.js
] derived from .m2/repository/org/testng/testng/6.7/testng-6.7.jar!/testng-reports.js
Cannot read [
/tmp/.m2/repository/org/testng/testng/6.7/testng-6.7.jar!/jquery-1.7.1.min.js
/home/clouway/.m2/repository/org/testng/testng/6.7/testng-6.7.jar!/jquery-1.7.1.min.js
] derived from .m2/repository/org/testng/testng/6.7/testng-6.7.jar!/jquery-1.7.1.min.js
at com.google.jstestdriver.PathResolver.resolve(PathResolver.java:98)
at com.google.jstestdriver.config.ParsedConfiguration.resolvePaths(ParsedConfiguration.java:99)
at com.google.jstestdriver.config.Initializer.initialize(Initializer.java:86)
at com.google.jstestdriver.embedded.JsTestDriverImpl.createRunnerInjector(JsTestDriverImpl.java:368)
at com.google.jstestdriver.embedded.JsTestDriverImpl.runConfigurationWithFlags(JsTestDriverImpl.java:342)
at com.google.jstestdriver.embedded.JsTestDriverImpl.runConfiguration(JsTestDriverImpl.java:233)
at com.google.jstestdriver.idea.TestRunner.runTests(TestRunner.java:195)
at com.google.jstestdriver.idea.TestRunner.executeTestCase(TestRunner.java:131)
at com.google.jstestdriver.idea.TestRunner.unsafeExecuteConfig(TestRunner.java:122)
at com.google.jstestdriver.idea.TestRunner.executeConfig(TestRunner.java:97)
at com.google.jstestdriver.idea.TestRunner.executeAll(TestRunner.java:88)
at com.google.jstestdriver.idea.TestRunner.main(TestRunner.java:330)
Empty test suite.
I Use maven as my build tool.
I haven't used JSTestDriver in IntelliJ, so I can't address your question directly. However, AngularJS has moved from JSTestDriver to using Testacular (http://vojtajina.github.com/testacular/), so you might want to do the same. The link includes some setup help getting it running in WebStorm which should be the same.
Looks like he's renamed it "Karma" (http://karma-runner.github.com/)

Resources