source: pyenvjasmine/pyenvjasmine/envjasmine/README.textile@ 19:ab5f65372038

Last change on this file since 19:ab5f65372038 was 19:ab5f65372038, checked in by Borja Lopez <borja@…>, 9 years ago

Imported latest envjasmine version, the initial import from darcs had the wrong version

File size: 6.4 KB
Line 
1h1. EnvJasmine: Jasmine test runner for EnvJS. !https://secure.travis-ci.org/trevmex/EnvJasmine.png!
2
3EnvJasmine allows you to run headless JavaScript tests.
4
5h2. Creating New Specs
6
7Add your Jasmine Spec files to the specs directory to be run.
8
9IMPORTANT: Make sure to load the file you are planning to test as the first line of the spec file using the @EnvJasmine.load("file.js");@ function.
10
11h2. Running JavaScript tests
12
13To run the JavaScript test suite, execute the following:
14
15bc. bin/run_all_tests.sh
16
17To run an individual spec file, execute the following:
18
19bc. bin/run_test.sh specs/[your spec file].js
20
21In Windows you do the same by running:
22
23bc. bin/run_all_tests.bat
24
25bc. bin/run_test.bat specs/[your spec file].js
26
27h2. Adding Dependencies
28
29Sometimes 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 @EnvJasmine.load("file.js");@ function.
30
31You can alsodefine a custom config file location to replace @include/dependencies.js@ by calling run_test or run_all_tests with the @--configFile=<config js file>@ option.
32
33h2. Based on info from:
34
35 * "JavaScript BDD, with Jasmine, without a browser":http://www.build-doctor.com/2010/12/08/javascript-bdd-jasmine/
36 * "Rhino":http://www.mozilla.org/rhino/
37 * "EnvJS":http://www.envjs.com/
38 * "Jasmine":http://pivotal.github.com/jasmine/
39 * "Jasmine Jquery":https://github.com/velesin/jasmine-jquery
40
41Originally created by Jeff Avallone and Trevor Lalish-Menagh. See "LICENSE":https://github.com/trevmex/EnvJasmine/blob/master/LICENSE for lincensing information.
42
43See "CHANGELOG":https://github.com/trevmex/EnvJasmine for changelog information.
44
45Current version is found in the "VERSION":https://github.com/trevmex/EnvJasmine/blob/master/VERSION file.
46
47h2. A Basic Tutorial
48
49How to set up EnvJasmine within a basic web project. Note that this tutorial assumes you're working within a *nix environment.
50
51h3. 1. Create a project directory.
52
53Create a directory in which to house your project. Let's call this @demo@. After creating the directory, enter it:
54
55bc. mkdir demo
56cd demo
57
58h3. 2. Create a JavaScript directory.
59
60Create a @js@ directory inside @demo@ to house JavaScript files:
61
62bc. mkdir js
63
64h3. 3. Create a tests directory.
65
66Create a directory in @demo@ to house your tests. As is often customary, let's call this directory @tests@:
67
68bc. mkdir tests
69
70h3. 4. Integrate EnvJasmine into the project.
71
72Clone EnvJasmine into your @tests@ directory:
73
74bc. git clone https://github.com/trevmex/EnvJasmine.git tests/EnvJasmine
75
76or
77
78bc. git clone git://github.com/trevmex/EnvJasmine.git tests/EnvJasmine
79
80h3. 5. Create a JavaScript file.
81
82For 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:
83
84bc. touch js/add-numbers.js
85
86h3. 6. Create a spec file.
87
88Create an EnvJasmine spec file, inside of which we'll write a test against the functionality of @js/add-numbers.js@:
89
90bc. touch tests/EnvJasmine/specs/add-numbers.spec.js
91
92h3. 7. Load the necessary JavaScript into the test spec.
93
94Inside @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:
95
96bc. EnvJasmine.load(EnvJasmine.jsDir + "/add-numbers.js");
97
98h3. 8. Configure your EnvJasmine.
99
100But what's this @EnvJasmine.jsDir@ business? It's a configuration variable used to define the primary directory housing your JavaScript. It's defined in @tests/EnvJasmine/include/dependencies.js@ and defaults to a top-level directory of @js@. Change this if you're using a different scheme:
101
102bc. mkdir -p tests/EnvJasmine/etc/conf
103
104bc. cp tests/EnvJasmine/include/dependencies.js tests/EnvJasmine/etc/conf/demo.conf.js
105
106And edit the following line:
107
108bc. EnvJasmine.jsDir = EnvJasmine.rootDir + "/../../js";
109
110in @tests/EnvJasmine/etc/conf/demo.conf.js@ with your favourite text editor.
111
112Please note the need for the leading slash.
113
114h3. 9. Write the framework for a basic test.
115
116In @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()@:
117
118bc. describe("addNumbers", function () {
119 it("returns the sum of the two integers it's passed", function() {
120 // test specifics will go here
121 });
122});
123
124h3. 10. Write the code testing outlining expected behavior of a unit of JavaScript.
125
126Utilizing Jasmine "syntax":https://github.com/pivotal/jasmine/wiki and "matchers":https://github.com/pivotal/jasmine/wiki/Matchers, write the code testing the expected behavior of @addNumbers()@:
127
128bc. describe("addNumbers", function () {
129 it("returns the sum of the two integers it's passed", function() {
130 expect(addNumbers(1, 2)).toEqual(3);
131 });
132});
133
134h3. 11. Run the test.
135
136Run the test from the command line:
137
138bc. ./tests/EnvJasmine/bin/run_test.sh --configFile=</absolute/path/to/demo/tests/EnvJasmine/etc/conf/demo.conf.js> specs/add-numbers.spec.js
139
140Note that the above command will run just the @add-numbers.spec.js@ code.
141
142Also note that at this stage, the test should fail as we have not yet written the @addNumbers()@ function.
143
144h3. 12. Write your JavaScript
145
146Write just enough code in @js/add-numbers.js@ to make the test pass:
147
148bc. function addNumbers(a, b) {
149 return a + b;
150}
151
152h3. 13. Run the test again.
153
154Run the test again and confirm that it passes:
155
156bc. ./tests/EnvJasmine/bin/run_test.sh --configFile=</absolute/path/to/demo/tests/EnvJasmine/etc/conf/demo.conf.js> specs/add-numbers.spec.js
157
158
159h2. Code Coverage
160
161See @lib/jscover/README.textile@
162
163
164h2. EnvJasmine as a Ruby gem
165
166EnvJasmine can also be compiled to a Ruby gem.
167
168h3. How to build and install the EnvJasmine gem
169
170 git clone git://github.com/trevmex/EnvJasmine.git
171 cd EnvJasmine
172 gem build EnvJasmine.gemspec
173 gem install EnvJasmine-1.7.1.gem
174
175h3. Using the EnvJasmine Ruby gem
176
177Note that, when used as a Ruby gem, EnvJasmine requires a few command line arguments be specified.
178
179Here is a Ruby usage example:
180
181 envjs_run_test --configFile='some_spec_helper.js' --testDir='specs' --rootDir='some_project'
182
183That's it! For more help on Jasmine docs at https://github.com/pivotal/jasmine/wiki
184
185Please contact Trevor Lalish-Menagh through github (https://github.com/trevmex) with any defects or feature requests!
186
Note: See TracBrowser for help on using the repository browser.