09: Testing!
describe('GET /messages', () => {
it('should require a token', (done) => {
// do something
});
});import { greet } from '../Greeting.js';
describe('Greeting', () => {
it('should greet on the form "Hello, <name>!', () => {
const result = greet('Liam');
const expected = 'Hello, Liam!';
// now what?
});
});Simple JS module
API endpoint
import { expect } from 'chai'; // !
import { greet } from '../Greeting.js';
describe('Greeting', () => {
it('should greet on the form "Hello, <name>!', () => {
const result = greet('Liam');
const expected = 'Hello, Liam!';
// this is what
expect(result).to.equal('Hello, ' + name + '!');
});
});
$ mocha --reporter <reporter>
(the important things)
describe('GET /messages', () => {
it('should return a JSON array', () => {
// Make the API available
// Send a request
// Verify the response
// Notify Mocha you are done
});
});import express from 'express';
import api from '../api.js';
const app = express();
describe('GET /messages', () => {
it('should return a JSON array', () => {
// Make the API available
app.use(api);
// Send a request
// Verify the response
// Notify Mocha you are done
});
});import express from 'express';
import request from 'supertest';
import api from '../api.js';
const app = express();
describe('GET /messages', () => {
it('should return a JSON array', () => {
// Make the API available
app.use(api);
// Send a request
request(app)
.get('/messages')
// Verify the response
// Notify Mocha you are done
});
});import express from 'express';
import request from 'supertest';
import api from '../api.js';
const app = express();
describe('GET /messages', () => {
it('should return a JSON array', () => {
// Make the API available
app.use(api);
// Send a request
request(app)
.get('/messages')
// Verify the response
.expect(200)
.expect('Content-Type', /json/)
// Notify Mocha you are done
});
});import express from 'express';
import request from 'supertest';
import api from '../api.js';
const app = express();
describe('GET /messages', () => {
it('should return a JSON array', done => {
// Make the API available
app.use(api);
// Send a request
request(app)
.get('/messages')
// Verify the response
.expect(200)
.expect('Content-Type', /json/)
// Notify Mocha you are done
.end(done);
});
});import express from 'express';
import request from 'supertest';
import { expect } from 'chai';
import api from '../api.js';
const app = express();
describe('GET /messages', () => {
it('should return a JSON array', done => {
// Make the API available
app.use(api);
// Send a request
request(app)
.get('/messages')
// Verify the response
.expect(200)
.expect('Content-Type', /json/)
.expect(res => {
expect(res.body).to.be.instanceOf(Array);
})
// Notify Mocha you are done
.end(done);
});
});npm install --save-dev babel-jest \
babel-preset-jest \
jest-clijest.mock('XHR'); // note: by default, this is done automatically in Jest
doWork();
const MockXHR = require('XHR');
// assert that MockXHR got called with the right argumentslet oldXHR = XHR;
XHR = function MockXHR() {};
doWork();
// assert that MockXHR got called with the right arguments
XHR = oldXHR; // if you forget this bad things will happenimport React from 'react';
import TestUtils from 'react-addons-test-utils';
import EmailInput from '../../../src/web/components/EmailInput.jsx';
describe('EmailInput', () => {
it('is a div that contains a label', () => {
// ...
});
});import React from 'react';
import TestUtils from 'react-addons-test-utils';
import { expect } from 'chai';
describe('EmailInput', () => {
it('should be a div', () => {
const renderer = TestUtils.createRenderer();
renderer.render(
<EmailInput label={label}
placeholder={placeholder}
onChange={onChange}/>
);
const component = renderer.getRenderOutput();
// Verify that a div is displayed
expect(component.type).to.equal('div');
});
});import { expect } from 'chai';
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import EmailInput from '../../../src/web/components/EmailInput.jsx';
describe('EmailInput', () => {
it('contains expected tags & label', () => {
const label = 'Email goes here';
const placeholder = 'someone@example.xyz';
const onChange = email => {};
const renderer = TestUtils.createRenderer();
renderer.render(
<EmailInput label={label}
placeholder={placeholder}
onChange={onChange}/>
);
const component = renderer.getRenderOutput();
// Verify that a div is displayed
expect(component.type).to.equal('div');
// ... that contains only a label
const labelElement = component.props.children;
expect(labelElement.type).to.equal('label');
expect(labelElement.props.children[0]).to.equal(label);
const inputElement = labelElement.props.children[1];
expect(inputElement.type).to.equal('input');
});
});