IT:AD:Jasmine:HowTo:Write Tests
- See also:
Summary
Once installed and configured, much like - but with a different syntax - you create Test Suites, that contain Tests, that contain Assertions.
But instead of using
* IT:AD:MSTest's syntax of [TestClass],[ClassInitialize] [TestMethod], and Assert.IsTrue(...), or
* IT:AD:NUnit's syntax of [TestFixture],[TestFixtureSetUp], [Test],and Assert.IsTrue,
you use describe, it, and expect/toBe:
//Test Suite
describe("A suite", function() {
//when entering and exiting suite:
beforeAll(function() {...});
afterAll(function() {...});
//run before and after each test:
beforeEach(function() {...});
afterEach(function() {...});
//Test #1
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
//Test #2
it("contains another spec with an expectation", function() {
expect(false).toBe(false);
});
//Test #3
it("contains an inverse assertion", function() {
expect(false).not.toBe(true);
});
it("contains various comparisons", function() {
//compare with null:
var foo = null;
expect(foo).toBeUndefined();
//compare using ===
expect(false).not.toBe(true);
//compare using ==
foo = 1;
expect(foo).toEqual(1);
//match using Regex:
foo = "foo bar";
expect(foo).toMatch(/bar/);
expect(foo).toMatch("bar");
//match against 'undefined':
expect(bar).toBeUndefined();
//loose boolean compare:
foo = 0;
expect(foo).toBeFalsy();
//less and greater than:
expect(1).toBeLessThan(3);
expect(3).toBeGreaterThan(1);
});
});
Notice:
- It's a fluent syntax (as opposed to
Attributebased as it is in C#) - Both
describeandittake an argument pair ofstring,function expectcan be negated by prefixing it withnot