EnvJasmine allows you to run headless JavaScript tests.
Add your Jasmine Spec files to the specs directory to be run.
IMPORTANT: Make sure to load the file you are planning to test as the first line of the spec file using the load("file.js");
function.
To run the JavaScript test suite, execute the following:
bin/run_all_tests.sh
To run an individual spec file, execute the following:
bin/run_test.sh specs/[your spec file].js
Sometimes you will have libraries that you need to load for any of your JavaScript files to work. To make sure these are loaded before your tests are run, include them in the include/dependencies.js
file using the load("file.js");
function.
Originally created by Jeff Avallone and Trevor Lalish-Menagh. See LICENSE for lincensing information.
See CHANGELOG for changelog information.
Current version is found in the VERSION file.
How to set up EnvJasmine within a basic web project. Note that this tutorial assumes you’re working within a *nix environment.
Create a directory in which to house your project. Let’s call this demo
. After creating the directory, enter it:
mkdir demo
cd demo
Create a js
directory inside demo
to house JavaScript files:
mkdir js
Create a directory in demo
to house your tests. As is often customary, let’s call this directory tests
:
mkdir tests
Clone EnvJasmine into your tests
directory:
git clone https://github.com/trevmex/EnvJasmine.git tests
or
git clone git://github.com/trevmex/EnvJasmine.git tests
For the purposes of this example, let’s assume we need to create some JavaScript that adds two numbers and returns their sum. Let’s create a JavaScript file to house this code:
touch js/add-numbers.js
Create an EnvJasmine spec file, inside of which we’ll write a test against the functionality of js/add-numbers.js
:
touch tests/EnvJasmine/specs/add-numbers.spec.js
Inside tests/EnvJasmine/specs/add-numbers.spec.js
, let’s load the file containing the code we’re testing, in this case js/add-numbers.js
. This can be done by adding the following line:
EnvJasmine.load(EnvJasmine.jsDirectory + "add-numbers.js");
But what’s this EnvJasmine.jsDirectory
business? It’s a configuratioon variable used to define the primary directory housing your JavaScript. To define this variable, open tests/EnvJasmine/include/dependencies.js
and change line 6 to read:
EnvJasmine.jsDirectory = EnvJasmine.rootDir + "/../../js/";
Please note the need for the leading slash.
In add-numbers.spec.js
, sketch out the basic framework for a unit test against the functionality within js/add-numbers.js
. Let’s assume this file contains a single function, addNumbers()
:
describe("addNumbers", function () {
it("returns the sum of the two integers it's passed", function() {
// test specifics will go here
});
});
Utilizing Jasmine syntax and matchers, write the code testing the expected behavior of addNumbers()
:
describe("addNumbers", function () {
it("returns the sum of the two integers it's passed", function() {
expect(addNumbers(1, 2)).toEqual(3);
});
});
Run the test from the command line:
./tests/EnvJasmine/bin/run_test.sh tests/EnvJasmine/specs/add-numbers.spec.js
Note that the above command will run just the add-numbers.spec.js
code. To run all tests:
./tests/EnvJasmine/bin/run_all_tests.sh
Also note that at this stage, the test should fail as we have not yet written the addNumbers()
function.
Write just enough code in js/add-numbers.js
to make the test pass:
function addNumbers(a, b) {
return a + b;
}
Run the test again and confirm that it passes:
./tests/EnvJasmine/bin/run_test.sh tests/EnvJasmine/specs/add-numbers.spec.js