Skip to content

Instantly share code, notes, and snippets.

@snewell92
Last active September 15, 2017 18:08
Show Gist options
  • Select an option

  • Save snewell92/6b44658beccbc1c88152f00aad9f9d09 to your computer and use it in GitHub Desktop.

Select an option

Save snewell92/6b44658beccbc1c88152f00aad9f9d09 to your computer and use it in GitHub Desktop.
Failing ava test
/* TYPESCRIPT */
import * as path from 'path';
import * as favicon from 'serve-favicon';
import * as compress from 'compression';
import * as cors from 'cors';
import * as helmet from 'helmet';
import * as bodyParser from 'body-parser';
import * as cookieParser from 'cookie-parser';
import * as feathers from 'feathers';
const configuration: any = require('feathers-configuration');
import * as hooks from 'feathers-hooks';
import * as rest from 'feathers-rest';
const primus: any = require('feathers-primus')
import { Application } from 'feathers';
import middleware from './middleware/';
import services from './services/';
import appHooks from './app.hooks';
import routes from './routes';
import authentication from './services/authentication';
import mysql from './mysql';
// Instantiate the feathers app
const feathersApp = feathers();
const isProd = process.env.ENV == 'PROD';
// set this to true to drop all tables and recreate them based on changes in the model.
// this is quick an dirty code-first migrations, only for early in dev before the first migration
// is created.
// TODO: DELETE THIS AFTER MIGRATIONS HAVE STARTED
const RECREATE_DB = false;
const configureApp = async (app: Application) => {
// set up the app - configuration
app = app.configure(configuration(path.join(__dirname, '..'))) as Application;
let authConfig = app.get('authentication');
app.use(cors())
.use(helmet()) // best security practices
.use(compress())
.use(favicon(path.join(app.get('public'), 'favicon.ico'))) // match favicon first. Most important. :D
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.use(cookieParser(authConfig.cookie.secret, authConfig.cookie))
.configure(rest())
.configure(primus({ transformer: 'uws' })) // Primus will require in uws
.configure(hooks())
.configure(authentication.bind(null, app));
let db = await mysql(RECREATE_DB );
app.set('sequelizeClient', db);
app = await services(app);
app.configure(routes)
.configure(middleware) // middleware last (as per normal ExpressJS rules) from middleware\index.js
.configure(appHooks);
return await app;
};
// return the app (configureApp is an async function, returns a promise)
export default configureApp(feathersApp);
{
"name": "newhotness",
"description": "Tennesse Solo and Ensemble",
"version": "0.0.0",
"repository": "ssh://git@git.geezer.dev:10022/dev-1/newhotness.git",
"license": "UNLICENSED",
"main": "dist/index.js",
"keywords": [
"feathers"
],
"author": {
"name": "Sean Newell",
"email": "sean.newell@dorianbusiness.com"
},
"contributors": [],
"directories": {
"lib": "src"
},
"engines": {
"node": ">= 8.0.0"
},
"ava": {
"files": [
"test/lib/test/*.js",
"test/lib/test/**/*.js"
]
},
"scripts": {
"clean-build": "npm run clean && npm run build",
"clean": "rimraf dist/ test/lib public/dist",
"deep-clean": "echo 'WARNING - REMOVING NODE_MODULES' && npm run clean && rm -rf node_modules/ && echo 'node_modules/ removed'",
"build": "tsc -p src",
"pretest": "npm run build-test",
"test": "npm run run-test",
"run-test": "ava",
"build-test": "tsc -p test",
"watch-tests": " run-p -c \"build-test -- -w\" \"run-test -- -w\"",
"start": "node dist/",
"start:watch": "nodemon",
"start-turbo": "node --turbo --ignition dist/",
"dev": "run-p -c \"build -- -w\" start:watch",
"minify-login-css": "cleancss -O 2 -o public/styles/login-styles.min.css src/ssr/login-styles.css"
},
"dependencies": {
"ava": "0.20.0",
"bluebird": "3.5.0",
"body-parser": "1.17.2",
"compression": "1.6.2",
"cookie-parser": "1.4.3",
"cors": "2.8.3",
"eslint": "3.19.0",
"eventemitter3": "2.0.3",
"express-vue": "3.14.3",
"feathers": "2.2.0",
"feathers-authentication": "1.2.7",
"feathers-authentication-hooks": "0.1.4",
"feathers-authentication-jwt": "0.3.2",
"feathers-authentication-local": "0.4.4",
"feathers-client": "2.4.0",
"feathers-configuration": "0.4.1",
"feathers-errors": "2.9.2",
"feathers-hooks": "2.0.2",
"feathers-hooks-common": "3.7.2",
"feathers-primus": "2.2.0",
"feathers-rest": "1.8.0",
"feathers-sequelize": "2.3.1",
"helmet": "3.6.1",
"lodash": "4.17.4",
"long": "3.2.0",
"moment": "2.18.1",
"moment-es6": "1.0.0",
"mysql2": "1.3.5",
"reflect-metadata": "0.1.10",
"request": "2.81.0",
"request-promise": "4.2.1",
"sequelize": "4.8.3",
"sequelize-typescript": "0.4.0",
"serve-favicon": "2.4.3",
"uws": "^8.14.1",
"vue": "2.3.3",
"vue-server-renderer": "2.3.3",
"winston": "2.3.1"
},
"devDependencies": {
"@types/bluebird": "^3.5.11",
"@types/body-parser": "1.16.3",
"@types/compression": "0.0.33",
"@types/cookie-parser": "1.3.30",
"@types/cors": "2.8.1",
"@types/helmet": "0.0.35",
"@types/lodash": "4.14.71",
"@types/long": "3.0.31",
"@types/nedb": "^1.8.3",
"@types/node": "^7.0.29",
"@types/request-promise": "4.1.34",
"@types/sequelize": "4.0.59",
"@types/serve-favicon": "2.2.28",
"@types/webpack": "3.0.10",
"@types/winston": "^2.3.3",
"clean-css-cli": "4.1.5",
"feathers-nedb": "^2.7.0",
"nedb": "^1.8.0",
"nodemon": "1.11.0",
"npm-run-all": "4.0.2",
"rimraf": "2.6.1",
"ts-mockito": "2.0.0",
"ts-node": "3.3.0",
"typescript": "2.5.2"
}
}
/* TYPESCRIPT */
import test from 'ava';
import { Pagination, Service } from 'feathers';
import * as path from 'path';
import * as NeDB from 'nedb';
import * a _ from 'lodash';
// service is untyped - fine for tests
const service = require('feathers-nedb');
import { getApp } from '../setup-tests';
import { Festival } from '../../src/models';
interface FestivalServiceReturn {
data: Festival[],
toatl: number
}
test('pagination-is-correct', async t => {
const app = await getApp();
const paginate = app.get('paginate');
// SUCCESS - configs are set properly
t.true(paginate.default == 10 && paginate.max == 50);
})
test.only('services-are-actually-paginated', async t => {
const app = await getApp();
const Model = createTestModel();
const paginate = app.get('paginate');
app.use('/test-service', service({ Model, paginate }));
let testService = app.service('/test-service');
let createABob = makeACreateABob(testService);
await Promise.all(_.times(15, createABob));
let pagedReturn = await testService.find!({ query: {} }) as Pagination<{}>;
// SUCCESS fake NeDB service is working as expected
t.true(pagedReturn.limit == 10);
let numOfItems = pagedReturn.data.length;
t.true(numOfItems <= 10);
});
test.only('paginate-festivals', async t => {
const app = await getApp();
let festivals = app.service('festivals');
let pagedReturn = await festivals.find!({ query: {} }) as Pagination<{}>;
// SUCCESS fake NeDB service is working as expected
t.true(pagedReturn.limit == 10);
let numOfItems = pagedReturn.data.length;
// FAILURE - my service is borked!
t.true(numOfItems <= 10);
});
/***** test-helpers ****/
const createTestModel = () => {
return new NeDB({
autoload: true,
inMemoryOnly: true
});
}
const makeACreateABob = (service: Service<{}>) => () => service.create!({ name: 'bob', type: 'test' });
/* TYPESCRIPT */
import test from 'ava';
import * as bluebird from 'bluebird';
import { Application } from 'feathers';
import { Server } from 'http';
import * as rp from 'request-promise';
import mysql from '../src/mysql';
import setupApp from '../src/app';
/********* Helpers to store the app in a promise all tests can refer to *********/
interface AppInstance {
App: Application,
Server: Server
}
let resolveApp: (appInstance: AppInstance) => void;
export let getInstance = new Promise<AppInstance>((res, rej) => resolveApp = res);
export let getApp = async () => (await getInstance).App;
export let getServer = async () => (await getInstance).Server;
const port = 9145;
let buildURL = (uri: string) => `http://localhost:${port}${uri}`;
test.before('app-setup', async t => {
try {
let app = await setupApp;
let server = app.listen(port) // listen on a high port
await bluebird.promisify(server.on, {context: server})('listening');
resolveApp({App: app, Server: server});
} catch(err) {
t.fail('Application setup failure');
}
});
test('normal-http', async t => {
await getInstance;
let resp = await rp(buildURL('/'));
t.true(resp.indexOf('</html>') !== -1);
});
test('404-error', async t => {
await getInstance;
try {
let resp = await rp(buildURL('/path/to/nowhere'))
t.fail();
} catch(err) {
t.pass();
}
});
test.after.always('cleanup', async t => {
let server = await getServer();
server.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment