MENU
Jasmine
Alexa's Ranking: 289,607
The syntax of Jasmine is identical to that of Jest.
Use Jasmine locally:
> npm install --save-dev jasmine> npx jasmine init> npx jasmine examples> npm test> npx jasmine singleFile.js
or globally:
> npm i -g jasmine> jasmine init> jasmine examples> jasmine> jasmine singleFile.js
where:
// package.json//…"scripts": {
"test": "mocha"}
Customize spec/support/jasmine.jsonspec/support/jasmine.json spec/support/jasmine.json to enumerate the source files and spec files you would like the Jasmine runner to include. You may use dir glob strings.
{
/* Spec directory path relative to the current working dir when jasmine is executed. */
"spec_dir": "spec",
/* Array of filepaths (and globs) relative to spec_dir to include and exclude */
"spec_files": [
"**/*[sS]pec.js",
"!**/*nospec.js"
],
/* Array of filepaths (and globs) relative to spec_dir to include before jasmine specs */
"helpers": [
"helpers/**/*.js"
],
/* Stop execution of a spec after the first expectation failure in it */
"stopSpecOnExpectationFailure": false,
/* Run specs in semi-random order */
"random": false}
Jasmine calls it() instead of test() to conduct tests. A 'spy' can be used to stub any function and track calls to it and all arguments. The Jasmine Clock is used to test time-dependent code.
describe("A suite", function() {
var value=0;
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
it("should support async execution of test preparation and expectations", function(done) {
value++;
expect(value).toBeGreaterThan(0);
done();
});
beforeEach(function() {
timerCallback = jasmine.createSpy("timerCallback");
jasmine.clock().install();
});
afterEach(function() {
jasmine.clock().uninstall();
});
it("causes a timeout to be called synchronously", function() {
setTimeout(function() {
timerCallback();
}, 100);
expect(timerCallback).not.toHaveBeenCalled();
jasmine.clock().tick(101);
expect(timerCallback).toHaveBeenCalled();
});
it("causes an interval to be called synchronously", function() {
setInterval(function() {
timerCallback();
}, 100);
expect(timerCallback).not.toHaveBeenCalled();
jasmine.clock().tick(101);
expect(timerCallback.calls.count()).toEqual(1);
jasmine.clock().tick(50);
expect(timerCallback.calls.count()).toEqual(1);
jasmine.clock().tick(50);
expect(timerCallback.calls.count()).toEqual(2);
});});describe("A spy", function() {
var foo, bar = null;
beforeEach(function() {
foo = {
setBar: function(value) {bar = value;}
};
spyOn(foo, 'setBar');
foo.setBar(123);
foo.setBar(456, 'another param');
});
it("tracks that the spy was called x times", function() {
expect(foo.setBar).toHaveBeenCalledTimes(2);
});
it("tracks all the arguments of its calls", function() {
expect(foo.setBar).toHaveBeenCalledWith(123);
expect(foo.setBar).toHaveBeenCalledWith(456,
'another param');
});
it("stops all execution on a function", function() {
expect(bar).toBeNull();
});
it("tracks if it was called at all", function() {
foo.setBar();
expect(foo.setBar.calls.any()).toEqual(true);
});});
D:\demo\jasmine-demo>jasmineRandomized with seed 77466Started.............13 specs, 0 failuresFinished in 0.025 secondsRandomized with seed 77466 (jasmine --random=true --seed=77466)D:\demo\jasmine-demo>jasmineRandomized with seed 59496Started.............13 specs, 0 failuresFinished in 0.023 secondsRandomized with seed 59496 (jasmine --random=true --seed=59496)
To use Jasmine on a browser, add the following to your HTML file, substituting {#.#.#} with the release number.
<link rel="shortcut icon" type="image/png"
href="lib/jasmine-{#.#.#}/jasmine_favicon.png"><link rel="stylesheet" type="text/css"
href="lib/jasmine-{#.#.#}/jasmine.css"><script type="text/javascript"
src="lib/jasmine-{#.#.#}/jasmine.js"></script><script type="text/javascript"
src="lib/jasmine-{#.#.#}/jasmine-html.js"></script><script type="text/javascript"
src="lib/jasmine-{#.#.#}/boot.js"></script>