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