How to run a single test file with Karma/Jasmine? - angularjs

I'm using Karma and Jasmine for testing my Angular JS code. I managed to run all the tests in my project using Karma, but if I try to run just a single file I'm getting the following error: ReferenceError: describe is not defined. Is it possible to run just a single test file and if yes then how?
P.S. I'm using Intellij IDEA.

Although it's not ideal, you can replace describe (and it) with fdescribe (and fit) so only those tests will be executed
On the other hand, xdescribe / xit excludes the tests from your suite
If you use Gulp, you can combine gulp-karma & yargs to pass in a pattern as argument.
source: https://stackoverflow.com/a/27696472/1782659

If you are like me and you've inherited a suite of tests that are controlled by a task runner (e.g. gulp) and you are getting failures but are unsure which file is actually failing you can sanity check an individual file by running jasmine directly against it:
1.) Make sure you have jasmine installed (it may not be if wrapped inside your task runner configuration)
npm install jasmine
2.) Run jasmine against file:
./node_modules/.bin/jasmine /path/to/my/spec/file.js
or
./node_modules/jasmine/bin/jasmine.js /path/to/my/spec/file.js

Related

Jasmine2 is not a valid framework error

I recently followed a tutorial on running protractor tests inside vs. To my surprise it works however i had to remove jasmine2 as the framework in the conf file or I would get this error
config.framework jasmine2 is not a valid framework.
This is also my first time running the chromedriver through vs and it cannot sendKeys as i was before.
Ex: this.passwordTextField.clear().sendKeys(browser.params.password);
I'm not sure how to get protractor to recognize jasmine2. I have installed jasmine manually also with the --save-dev tag. Protractor is at version 2.5.1
and jasmine is at 2.1.4
Any idea what is going on? Or if there is a better way of running protractor in vs?
I'm using this tutorial which didn't exactly work so I borrowed her sample code and edited that, that worked. Here is the site:
https://misaxionsoftware.wordpress.com/2014/01/30/angularjs-protractor-visual-studio-super-dry/
This error can only happen if protractor <= 1.5.0 is used. Check if tests are executed by the correct protractor executable. FYI, this is where it fails.
For instance, you may have an old globally installed protractor which is used instead of the one installed locally inside project's node_modules.
Or, what I've also seen in practice, the protractor executable located at .bin/protractor was updated by one of the packages that has protractor as a dependency (like grunt-protractor-runner, for instance).

Is there an example of AngularJS app generated by Yeoman with e2e tests and $httpBackend?

Is there somewhere an example of AngularJS app generated by yo angular generator that has e2e tests with $httpBackend from ngMockE2E module? Preferably with single and continuous versions for CI and development.
It looks like using $httpBackend requires one to create a new app that depends on the original app module and ngMockE2E module and requires new index.html file that loads this new app.
If tests use a different app, does it mean that I should modify configuration to store files generated for tests somewhere else than files generated by grunt server command (.tmp), or will these files be exactly the same? I'd like to be able to have grunt server running for development while running e2e tests in the background with PhantomJS.
Has anyone created a task that automatically generates modified index-e2e.html file based on index.html? This way it would be always up to date and it could also be used with watch to automatically regenerate it whenever original index.html file changes.
You should notice that angular is depreacting e2e in favor of the protractor framework.. Also notice that e2e (and protractor also) is quite slow. so running continuously in the background like we do with unittesting isn't recommended. That said, for your question - No you don't need a different app, index file etc. (unless you need coverage data from e2e, in that case you'll need to instrument the js files, and that would require a different index.html, that can be created in grunt task with sed). what you do need is a different karma.conf.js file, a different grunt karma task ro reference it, including ng-scenario in the files section of the karma.conf. and running some kind of grunt testServer task that would run a test server, which isn't the same as the dev grunt server. You can run both with foreman or something similiar (as explained in this SO [question]. (How can I automate both E2E and unit tests with Yeoman & AngularJS?) and answer). If this is what you are looking for - you can find a karma.conf.js example for both e2e an unit in this PR. and again, don't invest heavily into the current e2e framework. better working the the new and shiny protractor

Is it necessary to have a runner.html to run an e2e test?

Is it necessary to have a runner.html to run an e2e test?
Related to Selector [ng\:model="query"] did not match any elements
I am trying to get my e2e test running, but it keeps on failing.
The errors make it look like the test "engine" can't find any elements to assert against (see linked question).
I'm assuming that by having my karma config set up correctly, it will scan the included files specified by the karma config and run the test.
I'm assuming this because the karma runner does in fact detect the test, but the test itself fails.
Please see the linked question for the relevant code examples.
The runner is just a convenient way to do testing.
Karma will run headless (without a browser).

How can I run Jasmine tests with Karma (was Testacular) from Bamboo?

While building a single page app with AngularJS, I'm trying to integrate Jasmine tests in my build.
I did something similar before with the Maven Jasmine plugin, but I don't like to wrap my project in maven just to run the Jasmine tests. It seems cleaner to use Karma (was Testacular) for this somehow.
I'm comfortable that I'll get things running from a shell command, and my guess is that I can then run the command from Bamboo.
My questions:
Am I on the right track?
How can I best fail the build from a script, or does Bamboo recognize the Karma output automatically?
Great question. Make sure testacular.conf.js is configured to output junit xml for consumption by bamboo
junitReporter = {
// will be resolved to basePath (in the same way as files/exclude patterns)
outputFile: 'test-results.xml'
};
You can configure Testacular to run against many browsers and is pre-configured to use Chrome, we've chosen to start going headless with PhantomJS to do unit testing. Testacular already has jasmine inside.
For CI we are following the recommendation in
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = true;
If you use Ant a lot (and we do) sometimes you just want to stick with what you know... so you may want to checkout ANT, Windows and NodeJS Modules. to run node modules (ie testacular).
One note, if you are running testacular on windows, the npm install of testacular fails on hiredis module, which seems to be just *nix friendly. So, far it works fine without it.
It took us a couple of hours to prove all of this works. Hope this helps
--dan

Runnig JSUnit tests without a test suite

Is there a way to run JSUnit tests without specifying test suite file.
I want to run all the tests in a particular package so that one does not have to remember to add any new test to the existing suite.
I believe both Eclipse and Maven will do this for you.

Resources