diff --git a/jasmine.md b/jasmine.md index 561da32d..626ba667 100644 --- a/jasmine.md +++ b/jasmine.md @@ -1,138 +1,206 @@ --- title: Jasmine category: JavaScript libraries -layout: default-ad +layout: 2017/sheet +weight: -1 --- - describe("A suite", function() { - it("contains spec with an expectation", function() { - expect(true).toBe(true); - }); - }); +## Tests + +### Writing tests + +```js +describe('A suite', () => { + it('works', () => { + expect(true).toBe(true) + }) +}) +``` + +Note: This cheatsheet may be a little outdated. Also see the [Jest cheatsheet](./jest). Jest uses Jasmine, and therefore has similar API. ### Expectations - expect(true).toBe(true) - expect(true).not.toBe(true) +```js +expect(true).toBe(true) +expect(true).not.toBe(true) +``` - expect(a).toEqual(bar) +```js +expect(a).toEqual(bar) +``` - expect(message).toMatch(/bar/) - expect(message).toMatch('bar') +```js +expect(message).toMatch(/bar/) +expect(message).toMatch('bar') +``` - expect(a.foo).toBeDefined() - expect(a.foo).toBeUndefined() - expect(a.foo).toBeNull() +```js +expect(a.foo).toBeDefined() +expect(a.foo).toBeUndefined() +expect(a.foo).toBeNull() +``` - expect(a.foo).toBeTruthy() - expect(a.foo).toBeFalsy() +```js +expect(a.foo).toBeTruthy() +expect(a.foo).toBeFalsy() +``` - expect(message).toContain('hello') +```js +expect(message).toContain('hello') +``` - expect(pi).toBeGreaterThan(3) - expect(pi).toBeLessThan(4) - expect(pi).toBeCloseTo(3.1415, 0.1) +```js +expect(pi).toBeGreaterThan(3) +expect(pi).toBeLessThan(4) +expect(pi).toBeCloseTo(3.1415, 0.1) +``` - expect(func).toThrow() +```js +expect(func).toThrow() +``` -### Blocks +### Hooks - beforeEach(function() { ... }); - afterEach(function() { ... }); +```js +beforeEach(() => { + ··· +}) +``` + +```js +afterEach(() => { + ··· +}) +``` ### Pending - xit("this is a pending test", function() { ... }) - xdescribe("this is a pending block", function() { ... }) +```js +xit('this is a pending test', () => { + ··· +}) +``` + +```js +xdescribe('this is a pending block', () => { + ··· +}) +``` ### Spies - spyOn(foo, 'setBar') - spyOn(foo, 'setBar').andReturn(123) - spyOn(foo, 'getBar').andCallFake(function() { return 1001; }) - foo.setBar(123) +```js +spyOn(foo, 'setBar') +spyOn(foo, 'setBar').andReturn(123) +spyOn(foo, 'getBar').andCallFake(function() { return 1001; }) +foo.setBar(123) +``` - expect(foo.setBar).toHaveBeenCalled() - expect(foo.setBar).toHaveBeenCalledWith(123) - expect(foo.setBar.calls.length).toEqual(2) - expect(foo.setBar.calls[0].args[0]).toEqual(123) +```js +expect(foo.setBar).toHaveBeenCalled() +expect(foo.setBar).toHaveBeenCalledWith(123) +expect(foo.setBar.calls.length).toEqual(2) +expect(foo.setBar.calls[0].args[0]).toEqual(123) +``` ### Creating spies - stub = jasmine.createSpy('stub') - stub("hello") +```js +stub = jasmine.createSpy('stub') +stub('hello') +``` - expect(stub.identity).toEqual("stub") - expect(stub).toHaveBeenCalled() +```js +expect(stub.identity).toEqual('stub') +expect(stub).toHaveBeenCalled() +``` ### Async - it("should run async", function() { - var flag = false, value = 0; +```js +test('works with promises', () => { + return new Promise((resolve, reject) => { + ··· + }) +}) +``` - runs(function() { - setTimeout(function() { flag = true; }, 500); - }); - - waitsFor(function() { - value++; - return flag; - }, "increment", 750); - - runs(function() { - expect(value).toBeGreaterThan(0); - }); - }); +Make your test return a promise. ### HTML runner - var jasmineEnv = jasmine.getEnv(); - jasmineEnv.updateInterval = 250; +```js +var jasmineEnv = jasmine.getEnv() +jasmineEnv.updateInterval = 250 - var htmlReporter = new jasmine.HtmlReporter(); - jasmineEnv.addReporter(htmlReporter); +var htmlReporter = new jasmine.HtmlReporter() +jasmineEnv.addReporter(htmlReporter) - $(function() { jasmineEnv.execute(); }); +$(function() { jasmineEnv.execute() }) +``` Jasmine jQuery -============== +-------------- -[Jasmin jQuery](https://github.com/velesin/jasmine-jquery). +### Expectations - expect($('#id')).toBe('div') - expect($('input[type=checkbox]')).toBeChecked() - expect($('input[type=checkbox]')).toBeDisabled() - expect($('input[type=checkbox]')).toBeFocused() - expect($('#menu ul')).toBeEmpty() +```js +expect($('#id')).toBe('div') +expect($('input[type=checkbox]')).toBeChecked() +expect($('input[type=checkbox]')).toBeDisabled() +expect($('input[type=checkbox]')).toBeFocused() +expect($('#menu ul')).toBeEmpty() +``` - expect($('#toolbar')).toBeHidden() - expect($('#toolbar')).toBeVisible() +```js +expect($('#toolbar')).toBeHidden() +expect($('#toolbar')).toBeVisible() +``` - expect($('#popup')).toHaveCss({ margin: "10px" }) - expect($('option')).toBeSelected() +```js +expect($('#popup')).toHaveCss({ margin: "10px" }) +expect($('option')).toBeSelected() +``` - expect($('.foo')).toExist() +```js +expect($('.foo')).toExist() +``` - expect($('a')).toHaveAttr('rel') - expect($('a')).toHaveAttr('rel', 'nofollow') +```js +expect($('a')).toHaveAttr('rel') +expect($('a')).toHaveAttr('rel', 'nofollow') +``` - expect($('a')).toHaveClass('rel') - expect($('a')).toHaveId('home') +```js +expect($('a')).toHaveClass('rel') +expect($('a')).toHaveId('home') +``` - expect($('a')).toHaveHtml('') - expect($('a')).toContainHtml('') - expect($('a')).toHaveText('hi') +```js +expect($('a')).toHaveHtml('') +expect($('a')).toContainHtml('') +expect($('a')).toHaveText('hi') +``` - expect($form).toHandle('submit') // event - expect($form).toHandleWith('submit', onSumbit) +```js +expect($form).toHandle('submit') // event +expect($form).toHandleWith('submit', onSumbit) +``` + +See: [jasmine-jquery](https://github.com/velesin/jasmine-jquery) ### Event spies - spyOnEvent($('#some_element'), 'click'); - $('#some_element').click(); - expect('click').toHaveBeenPreventedOn($('#some_element')); - expect('click').toHaveBeenTriggeredOn($('#some_element')); +```js +spyOnEvent($('#some_element'), 'click') +$('#some_element').click() +expect('click').toHaveBeenPreventedOn($('#some_element')) +expect('click').toHaveBeenTriggeredOn($('#some_element')) +``` -### Reference +## References +{: .-one-column} - * http://pivotal.github.com/jasmine/ +* Also see the [Jest cheatsheet](./jest). Jest uses Jasmine, and therefore has similar API. +* diff --git a/jest.md b/jest.md index 2a93cc82..74603696 100644 --- a/jest.md +++ b/jest.md @@ -23,13 +23,11 @@ npm install --save-dev jest babel-jest "test": "jest" } ``` -{: .-setup} ```bash # Run your tests npm test -- --watch ``` -{: .-setup} See: [Getting started](http://facebook.github.io/jest/docs/en/getting-started.html) @@ -120,8 +118,10 @@ expect(value) .not .toBe(value) .toEqual(value) + .toBeTruthy() ``` +Note that `toEqual` is a deep equality check. See: [expect()](http://facebook.github.io/jest/docs/en/expect.html#expectvalue) ### Snapshots