1 test("module without setup/teardown (default)", function() { 2 expect(1); 3 ok(true); 4 }); 5 6 test("expect in test", 3, function() { 7 ok(true); 8 ok(true); 9 ok(true); 10 }); 11 12 test("expect in test", 1, function() { 13 ok(true); 14 }); 15 16 module("setup test", { 17 setup: function() { 18 ok(true); 19 } 20 }); 21 22 test("module with setup", function() { 23 expect(2); 24 ok(true); 25 }); 26 27 test("module with setup, expect in test call", 2, function() { 28 ok(true); 29 }); 30 31 var state; 32 33 module("setup/teardown test", { 34 setup: function() { 35 state = true; 36 ok(true); 37 }, 38 teardown: function() { 39 ok(true); 40 } 41 }); 42 43 test("module with setup/teardown", function() { 44 expect(3); 45 ok(true); 46 }); 47 48 module("setup/teardown test 2"); 49 50 test("module without setup/teardown", function() { 51 expect(1); 52 ok(true); 53 }); 54 55 if (typeof setTimeout !== 'undefined') { 56 state = 'fail'; 57 58 module("teardown and stop", { 59 teardown: function() { 60 equal(state, "done", "Test teardown."); 61 } 62 }); 63 64 test("teardown must be called after test ended", function() { 65 expect(1); 66 stop(); 67 setTimeout(function() { 68 state = "done"; 69 start(); 70 }, 13); 71 }); 72 73 module("async setup test", { 74 setup: function() { 75 stop(); 76 setTimeout(function(){ 77 ok(true); 78 start(); 79 }, 500); 80 } 81 }); 82 83 asyncTest("module with async setup", function() { 84 expect(2); 85 ok(true); 86 start(); 87 }); 88 89 module("async teardown test", { 90 teardown: function() { 91 stop(); 92 setTimeout(function(){ 93 ok(true); 94 start(); 95 }, 500); 96 } 97 }); 98 99 asyncTest("module with async teardown", function() { 100 expect(2); 101 ok(true); 102 start(); 103 }); 104 105 module("asyncTest"); 106 107 asyncTest("asyncTest", function() { 108 expect(2); 109 ok(true); 110 setTimeout(function() { 111 state = "done"; 112 ok(true); 113 start(); 114 }, 13); 115 }); 116 117 asyncTest("asyncTest", 2, function() { 118 ok(true); 119 setTimeout(function() { 120 state = "done"; 121 ok(true); 122 start(); 123 }, 13); 124 }); 125 126 test("sync", 2, function() { 127 stop(); 128 setTimeout(function() { 129 ok(true); 130 start(); 131 }, 13); 132 stop(); 133 setTimeout(function() { 134 ok(true); 135 start(); 136 }, 125); 137 }); 138 } 139 140 module("save scope", { 141 setup: function() { 142 this.foo = "bar"; 143 }, 144 teardown: function() { 145 deepEqual(this.foo, "bar"); 146 } 147 }); 148 test("scope check", function() { 149 expect(2); 150 deepEqual(this.foo, "bar"); 151 }); 152 153 module("simple testEnvironment setup", { 154 foo: "bar", 155 bugid: "#5311" // example of meta-data 156 }); 157 test("scope check", function() { 158 deepEqual(this.foo, "bar"); 159 }); 160 test("modify testEnvironment",function() { 161 this.foo="hamster"; 162 }); 163 test("testEnvironment reset for next test",function() { 164 deepEqual(this.foo, "bar"); 165 }); 166 167 module("testEnvironment with object", { 168 options:{ 169 recipe:"soup", 170 ingredients:["hamster","onions"] 171 } 172 }); 173 test("scope check", function() { 174 deepEqual(this.options, {recipe:"soup",ingredients:["hamster","onions"]}) ; 175 }); 176 test("modify testEnvironment",function() { 177 // since we do a shallow copy, the testEnvironment can be modified 178 this.options.ingredients.push("carrots"); 179 }); 180 test("testEnvironment reset for next test",function() { 181 deepEqual(this.options, {recipe:"soup",ingredients:["hamster","onions","carrots"]}, "Is this a bug or a feature? Could do a deep copy") ; 182 }); 183 184 185 module("testEnvironment tests"); 186 187 function makeurl() { 188 var testEnv = QUnit.current_testEnvironment; 189 var url = testEnv.url || 'http://example.com/search'; 190 var q = testEnv.q || 'a search test'; 191 return url + '?q='+encodeURIComponent(q); 192 } 193 194 test("makeurl working",function() { 195 equal( QUnit.current_testEnvironment, this, 'The current testEnvironment is global'); 196 equal( makeurl(), 'http://example.com/search?q=a%20search%20test', 'makeurl returns a default url if nothing specified in the testEnvironment'); 197 }); 198 199 module("testEnvironment with makeurl settings", { 200 url: 'http://google.com/', 201 q: 'another_search_test' 202 }); 203 test("makeurl working with settings from testEnvironment", function() { 204 equal( makeurl(), 'http://google.com/?q=another_search_test', 'rather than passing arguments, we use test metadata to form the url'); 205 }); 206 test("each test can extend the module testEnvironment", { 207 q:'hamstersoup' 208 }, function() { 209 equal( makeurl(), 'http://google.com/?q=hamstersoup', 'url from module, q from test'); 210 }); 211 212 module("jsDump"); 213 test("jsDump output", function() { 214 equals( QUnit.jsDump.parse([1, 2]), "[\n 1,\n 2\n]" ); 215 equals( QUnit.jsDump.parse({top: 5, left: 0}), "{\n \"top\": 5,\n \"left\": 0\n}" ); 216 if (typeof document !== 'undefined' && document.getElementById("qunit-header")) { 217 equals( QUnit.jsDump.parse(document.getElementById("qunit-header")), "<h1 id=\"qunit-header\"></h1>" ); 218 equals( QUnit.jsDump.parse(document.getElementsByTagName("h1")), "[\n <h1 id=\"qunit-header\"></h1>\n]" ); 219 } 220 }); 221 222 module("assertions"); 223 test("raises",function() { 224 function CustomError( message ) { 225 this.message = message; 226 } 227 228 CustomError.prototype.toString = function() { 229 return this.message; 230 }; 231 232 raises( 233 function() { 234 throw new Error("error") 235 } 236 ); 237 238 raises( 239 function() { 240 throw new Error("error") 241 }, 242 'raises with just a message, no expected' 243 ); 244 245 raises( 246 function() { 247 throw new CustomError(); 248 }, 249 CustomError, 250 'raised error is an instance of CustomError' 251 ); 252 253 raises( 254 function() { 255 throw new CustomError("some error description"); 256 }, 257 /description/, 258 "raised error message contains 'description'" 259 ); 260 261 raises( 262 function() { 263 throw new CustomError("some error description"); 264 }, 265 function( err ) { 266 if ( (err instanceof CustomError) && /description/.test(err) ) { 267 return true; 268 } 269 }, 270 "custom validation function" 271 ); 272 273 }); 274 275 if (typeof document !== "undefined") { 276 277 module("fixture"); 278 test("setup", function() { 279 document.getElementById("qunit-fixture").innerHTML = "foobar"; 280 }); 281 test("basics", function() { 282 equal( document.getElementById("qunit-fixture").innerHTML, "test markup", "automatically reset" ); 283 }); 284 285 } 286 287 module("custom assertions"); 288 (function() { 289 function mod2(value, expected, message) { 290 var actual = value % 2; 291 QUnit.push(actual == expected, actual, expected, message); 292 } 293 test("mod2", function() { 294 mod2(2, 0, "2 % 2 == 0"); 295 mod2(3, 1, "3 % 2 == 1"); 296 }) 297 })(); 298 299 (function() { 300 var reset = QUnit.reset; 301 function afterTest() { 302 ok( false, "reset should not modify test status" ); 303 } 304 module("reset"); 305 test("reset runs assertions", function() { 306 QUnit.reset = function() { 307 afterTest(); 308 reset.apply( this, arguments ); 309 }; 310 }); 311 test("reset runs assertions2", function() { 312 QUnit.reset = reset; 313 }); 314 })(); 315