Skip to content

Instantly share code, notes, and snippets.

@saitodisse
Last active December 29, 2015 16:08
Show Gist options
  • Save saitodisse/7695059 to your computer and use it in GitHub Desktop.
Save saitodisse/7695059 to your computer and use it in GitHub Desktop.
Jasmine + node = async samples
// This test verifies if an async function takes long time to execute.
// For that, I used the "waitsFor" for wait a maximum of 1500 ms
describe("Async specs", function(){
it("timeout after some time amount", function(){
var getTimeoutMs = function(){
var timeout = (Math.floor(Math.random() * 5) + 1) * 100;
console.log("timeout:", timeout);
return timeout;
};
var count = 0;
runs(function(){
setTimeout(function(){
count += 1;
}, getTimeoutMs());
setTimeout(function(){
count += 1;
}, getTimeoutMs());
setTimeout(function(){
count += 1;
}, getTimeoutMs());
});
waitsFor(function(){
return count === 3;
}, "too much time has gone", 1500);
});
})
// An event emmiter test
// wasCalled
// wasCalledWith: check the parameters
// callback.mostRecentCall.object: verifies the context
require("./spec_helper");
describe("EventEmitter specs", function(){
var emitter, callback;
beforeEach(function(){
emitter = new EventEmitter();
// jamine's spy object. very useful for callback tests
callback = jasmine.createSpy("callback spy")
})
it("trigger event", function(){
emitter.on("eventName", callback);
emitter.trigger("eventName");
expect(callback).wasCalled();
})
it("trigger event with arguments", function(){
emitter.on("eventName", callback);
emitter.trigger("eventName", 1,2,3);
expect(callback).wasCalledWith(1,2,3);
})
it("trigger event with a context", function(){
var context = {};
emitter.on("eventName", callback, context);
emitter.trigger("eventName", 1,2,3);
expect(callback.calls[0].object).toEqual(context);
expect(callback.mostRecentCall.object).toEqual(context);
})
it("trigger calls several callbacks", function(){
emitter.on("eventName", callback);
emitter.on("eventName", callback);
emitter.on("eventName", callback);
emitter.trigger("eventName");
expect(callback.callCount).toEqual(3);
})
it("trigger calls callback and callback2", function(){
callback2 = jasmine.createSpy("callback spy 2")
emitter.on("eventName", callback);
emitter.on("eventName", callback2);
emitter.trigger("eventName");
expect(callback).wasCalled();
expect(callback2).wasCalled();
})
})
/* Defines node dependencies */
{
"name": "EventEmitter",
"version": "0.1.0",
"private": true,
"devDependencies":{
"jasmine-node": "*"
}
}
// prepares global object
if(global !== "undefined"){
global.EventEmitter = require("../emitter").EventEmitter;
}
<!--
These lines below makes the "jasmine" be able to run on command-line and, yet, on browser
<script type="text/javascript">
function require(){};
function module(){};
</script>
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Spec Runner</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-1.3.1/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="lib/jasmine-1.3.1/jasmine.css">
<script type="text/javascript" src="lib/jasmine-1.3.1/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-1.3.1/jasmine-html.js"></script>
<script type="text/javascript">
function require(){};
function module(){};
</script>
<!-- include source files here... -->
<script type="text/javascript" src="../emitter.js"></script>
<!-- include spec files here... -->
<script type="text/javascript" src="emitter.spec.js"></script>
<script type="text/javascript" src="async.spec.js"></script>
<script type="text/javascript">
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment