Skip to content

Commit

Permalink
feat: support supertest
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-lemon committed Jul 18, 2019
1 parent 4115088 commit bbe3a66
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@
"nodemon": "^1.18.10",
"serverless-aws-documentation": "^0.8.0",
"serverless-offline": "^3.24.5",
"serverless-pseudo-parameters": "^2.4.0"
"serverless-pseudo-parameters": "^2.4.0",
"superagent": "^5.1.0",
"supertest": "^4.0.2"
},
"jest": {
"testMatch": [
Expand Down
9 changes: 5 additions & 4 deletions src/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ const app = express();

// app.use(bodyParser.json({limit:'10mb'})); //default limit 100kb
// const WebSocket = require('ws');
const uploader = multer({ dest: '../uploads/' });
const uploader = multer({ dest: '../tmp/' });

//! load configuration.
const handler = require('./index')(global, { env: $env });

// eslint-disable-next-line no-underscore-dangle
const _inf = (global && global.console.log) || (() => {});

//! middle ware
const middle = (req, res, next) => {
//! prepare event
Expand Down Expand Up @@ -99,7 +102,7 @@ app.get('', (req, res) => {
const handle_hello = (req, res) => handler.hello(req.$event, req.$context, req.$callback, res);

//! WARN - MUST sync with 'serverless.yml'
//! hello
//! manual route map of 'api/hello-api'
app.get('/hello', middle, handle_hello);
app.get('/hello/:id', middle, handle_hello);
app.get('/hello/:id/:cmd', middle, handle_hello);
Expand Down Expand Up @@ -138,8 +141,6 @@ const createServer = (options = null) => {
const port = getRunParam('-port', 8081, options.argv);

//! list port.
// eslint-disable-next-line no-underscore-dangle
const _inf = console.log;
server.listen(port, () => {
_inf(NS, 'Server Listen on Port =', server.address().port);
}).on('error', (e) => {
Expand Down
19 changes: 19 additions & 0 deletions test/hello.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ test('returns the alternative if an error was thrown', () => {
expect(returnOnError(() => {throw 'Foo'}, 'bar')).toEqual('bar');
});

//! with supertest via express.app
const request = require('supertest');
const app = require('../src/express')['app'];
describe('Test the root path', () => {

test('It should response the GET method', (done) => {
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(200);
done();
});
});

test('It should response the GET method (w/o done)', () => {
return request(app).get('/').expect(200);
});
});


//!WARN! - deperecated test with 'chai'
// //During the test the env variable is set to test
// process.env.NODE_ENV = 'test';

Expand Down

0 comments on commit bbe3a66

Please sign in to comment.