Skip to content

Commit

Permalink
Tests passed, need to improve setup for each
Browse files Browse the repository at this point in the history
  • Loading branch information
pxai committed May 7, 2018
1 parent c68b30d commit 9ba7d1d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
3 changes: 1 addition & 2 deletions miniapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Miniapi {

stop () {
this.data = data;
console.log('Stopped: , data: ' , this.data);
log.info(`[miniapi Stopped] data: ${JSON.stringify(this.data)}`);
this.server.close();
}

Expand Down Expand Up @@ -152,7 +152,6 @@ class Miniapi {

persistIfEnabled() {
if (this.getPersist()) {
console.log('PErsisting to: ', this.persist, this.file);
fs.writeFileSync(this.file, JSON.stringify(this.data));
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec \"npm test\"",
"test-watch": "nodemon --ignore '**/*.json' --exec \"npm test\"",
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
},
"repository": {
Expand Down
10 changes: 8 additions & 2 deletions test/miniapi.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const expect = require('expect');
const http = require('http');
const fs = require('fs');
const miniapi = require('../miniapi');
const PORT = 3200;
const GET_URL = `http://localhost:${PORT}`;
const DATA = [ { id: 1, name: 'Bob' }, { id: 2, name: 'Alice' } ];
const FILE = './sample.test.json';

fs.writeFileSync(FILE, JSON.stringify(DATA));

describe('basic tests working', () => {
afterEach(() => {
miniapi.stop;
miniapi.persist = false;
});

it('should not be null', () => {
expect(miniapi).toBeTruthy();
});
Expand Down Expand Up @@ -44,7 +48,7 @@ describe('basic tests working', () => {
});

it('should load contents from file', () => {
miniapi.withDataFrom('./sample.json');
miniapi.withDataFrom('./sample.test.json');
expect(miniapi.getData().length).toBe(2);
});

Expand Down Expand Up @@ -97,7 +101,7 @@ describe('web server testing', () => {
describe('persist mode testing', () => {
it('should store and restore data correctly', (done) => {
let payload = JSON.stringify({ name: 'Node persists'});
miniapi.withPort(PORT).withDataFrom('./sample.json').start();
miniapi.withPort(PORT).withDataFrom('./sample.test.json').withPersist().start();
http.request({
host: 'localhost',
port: PORT,
Expand All @@ -115,6 +119,8 @@ describe('persist mode testing', () => {
resp.on('end', () => {
expect(JSON.parse(data)).toEqual({ id: 3, name: 'Node persists'});
expect(resp.statusCode).toBe(200);
const d = JSON.parse(fs.readFileSync(FILE));
expect(d.length).toEqual(3);
done();
miniapi.stop();
});
Expand Down

0 comments on commit 9ba7d1d

Please sign in to comment.