Running mocha tests from node - javascript

I am trying to run my mocha tests from node . The ultimate goal is to add code-coverage via istanbul/blanket and generate a lcov file for input into sonar for code coverage.
This is a sample project on which I am trying
https://github.com/rajarshigoswami/Todos
The mocha tests are under
https://github.com/rajarshigoswami/Todos/tree/master/test/mocha
The tests are running from browser, but when I am trying via node, it wont pick up any of the spec files.
How to use :
run : npm install
then : grunt
My questions are :
What am I missing or doing wrong here?
How do I integrate blanket.js/istanbul to generate lcov files

I would change a few things. Simply add this to package.json
"scripts": {
"test":
"./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha -- -R spec"
}
Now, when you do npm test it runs mocha and istambul
This article will help you

There's a few options depending what you need to do:
1) So firstly it would be good to know what you actually mean by running tests in node? For example I use to mocha to test server-side code for my node.js applications. It just runs the tests without relying on any specific environment (except node.js obviously). Without any other tools simply run mocha your/custom/test/directory/ (that assume your mocha is installed globally).
So as long as your view test don't rely on being run in a Web Browser you're good.
2) If what you just want run your front-end tests without need of opening SpecRunner.html manually in a web browser, I highly recommend using karma test runner, with a headless browser like PhantomJS.
3) Finally if you already have the grunt installed just grab the mocha-istanbul npm module. The configuration is really simple:
mocha_istanbul: {
coverage: {
// where your tests leave
src: 'server/tests',
options: {
// instrument only spec files
mask: '*.spec.js',
// output a human readable website
// (along site regular test summary produced by mocha)
reportFormats: ['html']
}
}
}
So as you can see there are some options out there. Just grab the one that suits your project the best.

your problem is that you are trying to test code that using requirejs
in order for the you mocha spec to work with phantom and requirejs on a grunt task runner you can use grunt-mocha-phantomjs
for you other issue of using istambol check this link (this link is more suitable for your purpose because its use backbone also (-: )

Related

How to run a single .feature file in a Playwright + Cucumber automation testing framework?

I create Automation testing framework with Playwright + Cucumber.js written in Javascript language.
Everytime I type npm run test in the Console all the Cucumber scenarios (.feature files) are ran. But how can I run only one .feature scenario? Is there a command for this?
It would be really nice if you give me a tip how to run tests with tags, too.
I tried to run different commands but didn't succeed in running a single .feature test.
I tried to run tests with tags, but couldn't do it.
Sure you can, for running one feature it`s
npm run test <FullPathOfFeature>.feature
For running even only one scenario in feature it`s
npm run test <FullPathOfFeature>.feature:<LineOfCodeWhereScenarioIsDefined>

Triggering Karma tests in the test runner

I am having a small issue with WebStorm that I am hoping someone has experienced and solved before.
I am using WebStorm to build a angular.js app and I have it set up to use Karma to run my tests. This is fine for the most part: I have a Karma configuration setup and I can get to to run the tests or debug them with no issue.
My problem is that when I try to run a test individually by clicking on one of the test in the "Test Run" tree it goes off to a node configuration, tries to run it and fails (because its looking for js dependencies). After that I just go back to my 'karma config' and it runs through the whole of the test no problem.
Does anyone know how I can get the IDE hooked up so that I can trigger my tests from the UI?
Running tests from file right-click menu is only supported for those runners that allow executing individual tests (JSTestDriver, for example). There is currently no such possibility for Karma (WEB-13173). See the discussion at https://github.com/karma-runner/karma/issues/1235.
to run individual test files, you can have several karma configuration files with different sets of tests included. Plus you can rename individual tests/suits in the way mentioned in https://github.com/karma-runner/karma/issues/553

How to make karma.js crash on test fail?

I am looking for a way to make karma.js crash when my tests fail so that my build process gets interrupted since that is easier to monitor in a remote building server.
I am using mocha as a reporter and jasmine as the test processor engine.
Is there any option or particular variable in the karma.conf.js file maybe that would allow me to "crash on test fail"
There is no need for a "crash on failure".
Your build process should check the exit code of the Karma process that it launches. If all test pass, the code will be 0. If there is a test failure, the code will be non-zero. (I've just checked and it was 1 when I tried.)
You may be able to get a plugin for karma for your build server that will report the results of your tests.
For example, there's a plugin for karma/teamcity (karma-teamcity-reporter) which allows you to fail the build step, preventing the application from publishing if you have it set up that way.
you can use grunt as automation tool to run your karma test case so that whenever any grunt job(test case) failed it will by default stop execution.

Run/debug node.js testcases with jasmine in Webstorm 11

I install Webstorm 11 and want to run my tests (for node.js app) implemented with Jasmine.
However it's not easy to do that. I could just type in command line 'jasmine' command and test will be runned, but in this case I'm not able to debug code.
So is there a way to configure Webstorm to deal with jasmine specs as it should?
Ok, so while no one answer at the moment I will try to provide my version:
This flow will allow to run jasmine testsute from Webstrom and debug testcases
install jasmine (ither locally or globally)
in project folder create folder 'spec/support' In this folder place jasmine.json
tests configuration example:
{
"spec_dir": "tests",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
]
}
Create node.js configuration in Webstorm
In this configuration select source file - jasmine executable file (for localy installed
jasmine it will be 'node_modules\jasmine\bin\jasmine.js' )
So you are ready. However at the current moment when trying to debug this configuration - it fails with error:
Cannot stop on breakpoint due to internal error org.jetbrains.v8.V8CommandProcessor$1:
If you faced with it - you need to change Webstom configuration and set this settings:
-Dnodejs.debugger.use.jb.support=false For more details check there:
So this allow you to run jasmine tests and debug them. However there is still some things which this solution not able to do:
Run individual testcases
Run individual testcases with right click button and Run command from menu
Jetbrains, if you reading this - please fix this already. I started play with node in Webstorm 3 years ago and since that moment and dozens of version there is still no nice way to run tests. It's ridiculous.
Jasmine works with the JSTestDriver which you get out-of-the-box with WebStorm 11:
https://www.jetbrains.com/webstorm/help/enabling-javascript-unit-testing-support.html
This page also details how to add Jasmine within JSTestDriver:
https://www.jetbrains.com/webstorm/help/preparing-to-use-jstestdriver-test-runner.html
At a high level you're going to:
Install JSTestDriver from JetBrains plugin repo
Configure it as a WebStorm JavaScript library (https://www.jetbrains.com/webstorm/help/configuring-javascript-libraries.html#configure)
Open jsTestDriver.conf and type the following code in it:
load:
lib/jasmine/jasmine.js
lib/jasmine-jstd-adapter/JasmineAdapter.js
WebStorm doesn't manage test running directly. This job is done by a test runner. WebStorm supports several test runners - Mocha, Karma, JsTestDriver, nodeunit. Most of them can execute Jasmine tests

What is the correct way to test javascript with unit AND integrations tests, using Visual Studio and Team City?

I have got javascript tests running in visual studio using Resharper and Jasmine. It all works fine. Now however I want to run an integration test. After looking into this I just can't work out whether I should be using Karma or PhantomJs or both? And whether they link to resharper?
I then have to run all my tests on the continuous integration server using Team City. Doesn't seem like there is a clear logical way to architect front end testing from dev right through to the CI server.
For example do I really need to use Karma locally and on the CI server? I don't want to because it is not baked into visual studio and I will have to keep opening the command line and running my tests manually. This will annoy other developers and they won't bother running them I imagine. Help!
We are using Team City by the way....
Neither of these test runners are connected to Resharper in any form. I don't think there is a one and only "correct" way of running Jasmine tests in TeamCity currently - for all approaches I am aware of there is a bit of assembly required.
Personally I am using a combination of PhantomJS and the Jasmine TeamCity Reporter which works like this:
Get a list of unique test URLs from server - each of these test suites will use the Jasmine TeamCity Reporter to log results
for each test URL dynamically generate a test JS file to be used for phantom and write it to disk
use PhantomJS to run the JS file which now will load the test suite page and its test result will be written to console where they are picked up by TeamCity.

Categories

Resources