Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mohammed/test framework #21

Merged
merged 5 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
assets/dependencies/**/*.js
views/**/*.ejs

node_modules/
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
"sails-mongo": "^1.2.0"
},
"devDependencies": {
"eslint": "5.16.0"
"eslint": "5.16.0",
"mocha": "^8.1.1",
"supertest": "^4.0.2"
},
"scripts": {
"start": "NODE_ENV=production node app.js",
"test": "npm run lint && npm run custom-tests && echo 'Done.'",
"test": "node ./node_modules/mocha/bin/mocha test/lifecycle.test.js test/integration/**/*.test.js sails_port=${PORT:1337}",
"lint": "./node_modules/eslint/bin/eslint.js . --max-warnings=0 --report-unused-disable-directives && echo '✔ Your .js files look good.'",
"custom-tests": "echo \"(No other custom tests yet.)\" && echo"
},
Expand Down
30 changes: 30 additions & 0 deletions test/integration/actions/Blob.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const supertest = require('supertest');
const stubBlobs = require('../../stub');

describe('Blob', () => {
it('Gets existing blob', (done) => {
supertest(sails.hooks.http.app)
.get(`/api/v1/blobs/${stubBlobs.blob.id}`)
.expect(200)
.then(() => done())
.catch((err) => done(err));
});

it('Creates a new blob', (done) => {
supertest(sails.hooks.http.app)
.post('/api/v1/blobs/create')
.send(stubBlobs.blob1)
.expect(200)
.then(() => done())
.catch((err) => done(err));
});

it('Updates an existing blob', (done) => {
supertest(sails.hooks.http.app)
.put(`/api/v1/blobs/update/${stubBlobs.blob.id}`)
.send(stubBlobs.blob2)
.expect(200)
.then(() => done())
.catch((err) => done(err));
});
});
44 changes: 44 additions & 0 deletions test/lifecycle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const sails = require('sails');
const stubBlobs = require('./stub');
const supertest = require('supertest');

// Global before hook
before((done) => {
sails.lift(
{
// These are sails configs that are over ridden
// for the purposes of testing

// wipe/drop ALL data and rebuild models every time test is run
models: { migrate: 'drop' },

// One level higher than silent
log: { level: 'error' },

// Disables console logs like: `GET /api/v1/blobs/view 200 7.822 ms - 438`
requestlogger: false,

// Grunt should disabled by default but this is extra precaution :/
hooks: { grunt: false },
},
(err) => {
if (err) {return done(err);}

// Create an existing record in the db
supertest(sails.hooks.http.app)
.post('/api/v1/blobs/create')
.send(stubBlobs.blob)
.expect(200)
.then((res) => {
// Set the id so that it can be queried
stubBlobs.blob.id = res.body.id;
done();
})
.catch((err) => done(err));
}
);
});

after((done) => {
sails.lower(done);
});
28 changes: 28 additions & 0 deletions test/stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This file contains all test blobs needed for testing

module.exports = {

// `blob` exists in db before tests are run
blob : {
source: 'source',
plugin: 'plugin',
configs: [
'config',
]
},

blob1: {
source: 'source_1',
plugin: 'plugin_1',
configs: [
'config_1',
'config_2'
]
},

blob2: {
source: 'source_2',
plugin: 'plugin_2',
},

};