Cross-browser Karma JavaScript test results in Visual Studio Test Explorer Click to see a demo of this extension used for Karma cross-browser JavaScript testing.
Installation and Setup- Install my Karma Test Adapter from Visual Studio Gallery
- Install/download nodejs (I grabbed the exe from here and added it to my PATH)
- Install/download npm (I grabbed the zip from here and put it in the same folder as node.exe)
- Install the karma-cli node module globally
Usage- Create/navigate to the folder that contains/will contain your tests
- Create a package.json to hold your node dependencies
- PowerShell:
"{}" | Out-File -Encoding ascii package.json - or CMD:
echo '{}' > package.json
- Install karma and some dependencies
npm install karma --save-dev npm install karma-xml-reporter --save-dev npm install karma-jasmine --save-dev npm install karma-phantomjs-launcher --save-dev (required only if you want to use PhantomJS)npm install karma-ie-launcher --save-dev (required only if you want Karma to be able to launch IE)npm installkarma-chrome-launcher --save-dev (required only if you want Karma to be able to launch Chrome)npm installkarma-firefox-launcher --save-dev (required only if you want Karma to be able to launch Firefox)
- Create a karma config file in the folder with your tests
karma init - answer the questions, for example:
- Test framework: jasmine
- RequireJS: no
- Browsers:
- JavaScript and Test files:
- Exclusions: (none)
- Watch for changes: yes
- Open up the generated karma.config.js in your text editor and add 'xml' as a reporter
reporters: ['progress'] becomes reporters: ['progress', 'xml']
- Open Visual Studio and create a project that lives in the folder that contains your tests/karma config file
- I usually choose File -> Open Web Site and choose the FileSystem option, pointing at the folder
- Create a simple test we can use to verify everything works. The filename must match the filter you gave to
karma init earlier (eg. MyTestSpecs.js )describe("a test", function() { it("passes", function() { expect(true).toBe(true); }); });
- From the command line, start karma
karma start - note: This will fire up the browsers that you configured; these must be left open to execute tests
- In Visual Studio, toggle the "Run Tests on Build" option on in the Test Explorer Window
- Right-click -> Refresh on the project, to ensure Visual Studio spots the test-results.testxml file
- As you make changes to your js files and press save, Karma will now automatically execute your tests and the results will magically update in Visual Studio!
- Bonus: You can open up other browsers and point them at the url that's loaded in the existing browsers and they will automatically be used for tests. Eg. enter the URL into your smartphone browser, and now it will show up in the test results too!
FeedbackPlease send your feedback/issues/feature requests! :-)
| |