Skip to content

Instantly share code, notes, and snippets.

@dgb
Created October 8, 2015 05:58
Show Gist options
  • Save dgb/83c56c9cee3e6c33c2bb to your computer and use it in GitHub Desktop.
Save dgb/83c56c9cee3e6c33c2bb to your computer and use it in GitHub Desktop.
import { Post } from './database';
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/posts', function(req, res) {
Post.fetchAll().then(function(posts) {
res.send(posts);
});
});
app.post('/posts', function(req, res) {
new Post(req.body).save().then(function(post) {
res.status(201);
res.send(post.toJSON());
});
});
export default app;
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from './app';
import { setupSchema, Post } from './database';
chai.use(chaiHttp);
let { request, expect } = chai;
before(setupSchema);
describe('GET /posts', function() {
it('responds OK', async function() {
let res = await request(app).get('/posts');
expect(res).to.have.status(200);
});
})
describe('POST /posts', function() {
it('makes a post', async function() {
let params = {
title: 'ES7 async functions',
body: "they're super tight!"
}
let res = await request(app).post('/posts').send(params);
expect(res).to.have.status(201);
expect(res.body.body).to.equal(params.body);
expect(res.body.title).to.equal(params.title);
let post = await new Post({id: res.body.id}).fetch();
expect(post.attributes.body).to.equal(params.body);
expect(post.attributes.title).to.equal(params.title);
});
});
import knex from 'knex';
import Bookshelf from 'bookshelf';
const db = knex({
client: 'sqlite3',
connection: {
filename: ':memory:'
}
});
const bookshelf = Bookshelf(db);
export function setupSchema() {
return db.schema.createTable('posts', function (table) {
table.increments('id').primary();
table.string('title');
table.text('body');
})
}
export const Post = bookshelf.Model.extend({
tableName: 'posts'
});
{
"name": "async-testing",
"private": true,
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha --compilers js:babel/register app_test.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.14.1",
"bookshelf": "^0.8.2",
"express": "^4.13.3",
"knex": "^0.8.6",
"sqlite3": "^3.1.0"
},
"devDependencies": {
"babel": "^5.8.23",
"chai": "^3.3.0",
"chai-http": "^1.0.0",
"mocha": "^2.3.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment