Skip to content

Instantly share code, notes, and snippets.

@lucassus
Created March 8, 2012 12:01
Show Gist options
  • Save lucassus/2000714 to your computer and use it in GitHub Desktop.
Save lucassus/2000714 to your computer and use it in GitHub Desktop.
describe('built-in matchers', function() {
describe('toBeTruthy', function() {
it('passes if subject is true', function() {
expect(true).toBeTruthy();
expect(false).not.toBeTruthy();
});
});
describe('toBeFalsy', function() {
it('passes if subject is false', function() {
expect(false).toBeFalsy();
expect(true).not.toBeFalsy();
});
});
describe('toBeDefined', function() {
it('passes if subject is not undefined', function() {
expect({}).toBeDefined();
expect(undefined).not.toBeDefined();
});
});
describe('toBeNull', function() {
it('passes if subject is null', function() {
expect(null).toBeNull();
expect(undefined).not.toBeNull();
expect({}).not.toBeNull();
});
});
describe('toEqual', function() {
it('passes if subject and expectation are equivalent', function() {
expect('Hello World!').toEqual('Hello World!');
expect('Hello World!').not.toEqual('Goodbye!');
expect('Hello World!').toNotEqual('Hi!');
expect([1, 2, 3]).toEqual([1, 2, 3]);
expect(1).toEqual(1);
expect({
foo: 1
}).toEqual({
foo: 1
});
});
});
describe('toBeCloseTo', function() {
it('checks that the expected item is equal to the actual item up to a given level of decimal precision ', function() {
expect(1.223).toBeCloseTo(1.22);
expect(1.233).not.toBeCloseTo(1.22);
expect(1.23326).toBeCloseTo(1.23324, 3);
});
});
describe('toContain', function() {
it('passes if the expected item is an element in the actual array', function() {
expect([1, 2, 3]).toContain(2);
expect([1, 2, 3]).not.toContain(4);
});
});
// TODO toMatch
// TODO toThrow(expected)
// TODO toBeGreaterThan(expected)
// TODO toBeLessThan(expected)
});
describe('creating custom matchers', function() {
beforeEach(function() {
this.addMatchers({
toBeGET: function() {
var actual = this.actual.method;
return actual === 'GET';
},
toHaveUrl: function(expected) {
var actual = this.actual.url;
this.message = function() {
return "Expected request to have url " + expected + " but was " + actual
};
return actual === expected;
}
});
});
it('should be GET', function() {
var request = {
method: 'GET'
};
expect(request).toBeGET();
});
it('should have url /products.json', function() {
var request = {
url: '/products.json'
};
expect(request).toHaveUrl('/products.json');
});
it('should have url /tasks.json', function() {
var request = {
url: '/projects.json'
};
expect(request).toHaveUrl('/tasks.json');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment