From 5a4df8ebaaedefef4203cc95de57bd23f09da99a Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Thu, 7 Sep 2017 13:41:30 -0400 Subject: [PATCH 01/13] Use Promise style test examples instead of callbacks --- test/basic-auth.test.js | 6 +-- test/body-parsing.test.js | 87 +++++++++++++++--------------------- test/cache-control.test.js | 55 ++++++++++------------- test/conditional-get.test.js | 16 +++---- test/headers.test.js | 70 +++++++++++++---------------- test/jsonp.test.js | 12 ++--- test/middleware.test.js | 14 +++--- test/object-streams.test.js | 6 +-- test/query-string.test.js | 12 ++--- 9 files changed, 121 insertions(+), 157 deletions(-) diff --git a/test/basic-auth.test.js b/test/basic-auth.test.js index 854353c..07c746c 100644 --- a/test/basic-auth.test.js +++ b/test/basic-auth.test.js @@ -2,19 +2,19 @@ const koala = require('../lib'); const request = require('supertest'); describe('Basic Auth', () => { - test('should return the value', done => { + test('should return the value', () => { const app = koala(); app.use(function * (next) { this.body = this.request.basicAuth; }); - request(app.listen()) + return request(app.listen()) .get('/') .auth('username', 'password') .expect(200) .expect({ name: 'username', pass: 'password' - }, done); + }); }); }); diff --git a/test/body-parsing.test.js b/test/body-parsing.test.js index 5dbf909..7b2e569 100644 --- a/test/body-parsing.test.js +++ b/test/body-parsing.test.js @@ -1,71 +1,70 @@ const koala = require('../lib'); const request = require('supertest'); -const http = require('http'); describe('Body Parsing', () => { describe('.request.json()', () => { - test('should parse a json body', done => { + test('should parse a json body', () => { const app = koala(); app.use(function * () { this.body = yield * this.request.json(); }); - request(app.listen()) + return request(app.listen()) .post('/') .send({ message: 'lol' }) .expect(200) .expect(/"message"/) - .expect(/"lol"/, done); + .expect(/"lol"/); }); - test('should throw on non-objects in strict mode', done => { + test('should throw on non-objects in strict mode', () => { const app = koala(); app.use(function * () { this.body = yield * this.request.json(); }); - request(app.listen()) + return request(app.listen()) .post('/') .type('json') .send('"lol"') - .expect(400, done); + .expect(400); }); - test('should not throw on non-objects in non-strict mode', done => { + test('should not throw on non-objects in non-strict mode', () => { const app = koala(); app.jsonStrict = false; app.use(function * () { this.body = yield * this.request.json(); }); - request(app.listen()) + return request(app.listen()) .post('/') .type('json') .send('"lol"') .expect(200) - .expect('lol', done); + .expect('lol'); }); }); describe('.request.urlencoded()', () => { - test('should parse a urlencoded body', done => { + test('should parse a urlencoded body', () => { const app = koala(); app.use(function * () { this.body = yield * this.request.urlencoded(); }); - request(app.listen()) + return request(app.listen()) .post('/') .send('message=lol') .expect(200) .expect(/"message"/) - .expect(/"lol"/, done); + .expect(/"lol"/); }); - test('should not support nested query strings by default', done => { + test('should not support nested query strings by default', () => { const app = koala(); app.use(function * () { this.body = yield * this.request.urlencoded(); }); - request(app.listen()) + return request(app.listen()) .post('/') .type('form') .send({ @@ -74,17 +73,17 @@ describe('Body Parsing', () => { } }) .expect(200) - .expect(/something\[nested\]/, done); + .expect(/something\[nested\]/); }); - test('should support nested query strings with options.qs=true', done => { + test('should support nested query strings with options.qs=true', () => { const app = koala({ qs: true }); app.use(function * () { this.body = yield * this.request.urlencoded(); }); - request(app.listen()) + return request(app.listen()) .post('/') .type('form') .send({ @@ -97,89 +96,75 @@ describe('Body Parsing', () => { something: { nested: 'true' } - }, done); + }); }); }); describe('.request.text()', () => { - test('should get the raw text body', done => { + test('should get the raw text body', () => { const app = koala(); app.use(function * () { this.body = yield * this.request.text(); expect(typeof this.body).toBe('string'); }); - request(app.listen()) + return request(app.listen()) .post('/') .send('message=lol') .expect(200) - .expect('message=lol', done); + .expect('message=lol'); }); - test('should throw if the body is too large', done => { + test('should throw if the body is too large', () => { const app = koala(); app.use(function * () { yield * this.request.text('1kb'); this.body = 204; }); - request(app.listen()) + return request(app.listen()) .post('/') .send(Buffer.alloc(2048)) - .expect(413, done); + .expect(413); }); }); describe('.request.buffer()', () => { - test('should get the raw buffer body', done => { + test('should get the raw buffer body', () => { const app = koala(); app.use(function * () { this.body = yield * this.request.buffer(); expect(Buffer.isBuffer(this.body)).toBeTruthy(); }); - request(app.listen()) + return request(app.listen()) .post('/') .send('message=lol') .expect(200) - .expect('message=lol', done); + .expect('message=lol'); }); - test('should throw if the body is too large', done => { + test('should throw if the body is too large', () => { const app = koala(); app.use(function * () { yield * this.request.buffer('1kb'); this.body = 204; }); - request(app.listen()) + return request(app.listen()) .post('/') .send(Buffer.alloc(2048)) - .expect(413, done); + .expect(413); }); }); describe('Expect: 100-continue', () => { - test('should send 100-continue', done => { + test('should send 100-continue', () => { const app = koala(); app.use(function * () { this.body = yield * this.request.json(); }); - app.listen(function() { - http.request({ - port: this.address().port, - path: '/', - headers: { - 'expect': '100-continue', - 'content-type': 'application/json' - } - }) - .once('continue', function() { - this.end(JSON.stringify({ - message: 'lol' - })); - }) - .once('response', res => { - done(); - }) - .once('error', done); - }); + return request(app.listen()) + .get('/') + .set('expect', '100-continue') + .set('content-type', 'application/json') + .send('message=lol'); }); }); }); diff --git a/test/cache-control.test.js b/test/cache-control.test.js index 4a892fe..bfa1b75 100644 --- a/test/cache-control.test.js +++ b/test/cache-control.test.js @@ -3,124 +3,117 @@ const request = require('supertest'); describe('Cache-Control', () => { describe('should be available as', () => { - test('this.cc()', done => { + test('this.cc()', () => { const app = koala(); app.use(function * (next) { this.cc(1000); this.status = 204; }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(204) - .expect('Cache-Control', 'public, max-age=1') - .end(done); + .expect('Cache-Control', 'public, max-age=1'); }); - test('this.cacheControl()', done => { + test('this.cacheControl()', () => { const app = koala(); app.use(function * (next) { this.cacheControl(1000); this.status = 204; }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(204) - .expect('Cache-Control', 'public, max-age=1') - .end(done); + .expect('Cache-Control', 'public, max-age=1'); }); - test('this.response.cc()', done => { + test('this.response.cc()', () => { const app = koala(); app.use(function * (next) { this.response.cc(1000); this.status = 204; }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(204) - .expect('Cache-Control', 'public, max-age=1') - .end(done); + .expect('Cache-Control', 'public, max-age=1'); }); }); describe('when the value is a number', () => { - test('should set "public, max-age="', done => { + test('should set "public, max-age="', () => { const app = koala(); app.use(function * (next) { this.response.cacheControl(1000000); this.status = 204; }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(204) - .expect('Cache-Control', 'public, max-age=1000') - .end(done); + .expect('Cache-Control', 'public, max-age=1000'); }); }); describe('when the value is a time string', () => { - test('should set "public, max-age="', done => { + test('should set "public, max-age="', () => { const app = koala(); app.use(function * (next) { this.response.cacheControl('1 hour'); this.status = 204; }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(204) - .expect('Cache-Control', 'public, max-age=3600') - .end(done); + .expect('Cache-Control', 'public, max-age=3600'); }); }); describe('when the value is "false"', () => { - test('should set "private, no-cache"', done => { + test('should set "private, no-cache"', () => { const app = koala(); app.use(function * (next) { this.response.cacheControl(false); this.status = 204; }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(204) - .expect('Cache-Control', 'private, no-cache') - .end(done); + .expect('Cache-Control', 'private, no-cache'); }); }); describe('when the value is a string', () => { - test('should just set it', done => { + test('should just set it', () => { const app = koala(); app.use(function * (next) { this.response.cacheControl('foo'); this.status = 204; }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(204) - .expect('Cache-Control', 'foo') - .end(done); + .expect('Cache-Control', 'foo'); }); }); describe('when the value is anything else', () => { - test('should throw', done => { + test('should throw', () => { const app = koala(); app.use(function * (next) { this.response.cacheControl(true); this.status = 204; }); - request(app.listen()) + return request(app.listen()) .get('/') - .expect(500, done); + .expect(500); }); }); }); diff --git a/test/conditional-get.test.js b/test/conditional-get.test.js index deae8a1..92297a0 100644 --- a/test/conditional-get.test.js +++ b/test/conditional-get.test.js @@ -12,22 +12,20 @@ describe('Conditional-Get', () => { const server = app.listen(); - test('should set an etag', done => { - request(server) + test('should set an etag', () => { + return request(server) .get('/') - .expect(200, (err, res) => { - if (err) return done(err); - + .expect(200) + .expect(res => { etag = res.headers.etag.slice(1, -1); - done(); }); }); - test('should response 304 w/ if-none-match header', done => { - request(server) + test('should response 304 w/ if-none-match header', () => { + return request(server) .get('/') .set('If-None-Match', '"' + etag + '"') - .expect(304, done); + .expect(304); }); }); }); diff --git a/test/headers.test.js b/test/headers.test.js index a725769..f74f0e3 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -3,59 +3,55 @@ const request = require('supertest'); describe('Set headers', () => { describe('X-Response-Time', () => { - test('should get X-Response-Time correctly by default', done => { + test('should get X-Response-Time correctly by default', () => { const app = koala(); - request(app.listen()) + return request(app.listen()) .get('/') .expect(404) - .expect('X-Response-Time', /s$/) - .end(done); + .expect('X-Response-Time', /s$/); }); test( 'should not get X-Response-Time by options.responseTime = false', - done => { + () => { const app = koala({ responseTime: false }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(404) - .end((err, res) => { + .expect(res => { expect(res.headers['X-Response-Time']).toBe(undefined); - done(err); }); } ); }); describe('Strict-Transport-Security', () => { - test('should not set Strict-Transport-Security by default', done => { + test('should not set Strict-Transport-Security by default', () => { const app = koala(); - request(app.listen()) + return request(app.listen()) .get('/') .expect(404) - .end((err, res) => { + .expect(res => { expect(res.headers['Strict-Transport-Security']).toBe(undefined); - done(err); }); }); - test('should set Strict-Transport-Security if `hsts` is a number', done => { + test('should set Strict-Transport-Security if `hsts` is a number', () => { const app = koala({ security: { hsts: 1000 } }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(404) - .expect('Strict-Transport-Security', 'max-age=1') - .end(done); + .expect('Strict-Transport-Security', 'max-age=1'); }); - test('should set Strict-Transport-Security if `hsts.maxAge` is present', done => { + test('should set Strict-Transport-Security if `hsts.maxAge` is present', () => { const app = koala({ security: { hsts: { @@ -64,13 +60,12 @@ describe('Set headers', () => { } }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(404) - .expect('Strict-Transport-Security', 'max-age=1') - .end(done); + .expect('Strict-Transport-Security', 'max-age=1'); }); - test('should set Strict-Transport-Security with `includeSubDomains`', done => { + test('should set Strict-Transport-Security with `includeSubDomains`', () => { const app = koala({ security: { hsts: 1000, @@ -78,64 +73,59 @@ describe('Set headers', () => { } }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(404) - .expect('Strict-Transport-Security', 'max-age=1; includeSubDomains') - .end(done); + .expect('Strict-Transport-Security', 'max-age=1; includeSubDomains'); }); }); describe('X-Frame-Options', () => { - test('should get X-Frame-Options DENY by default', done => { + test('should get X-Frame-Options DENY by default', () => { const app = koala(); - request(app.listen()) + return request(app.listen()) .get('/') .expect(404) - .expect('X-Frame-Options', 'DENY') - .end(done); + .expect('X-Frame-Options', 'DENY'); }); - test('should not get X-Frame-Options by xframe = false', done => { + test('should not get X-Frame-Options by xframe = false', () => { const app = koala({ security: { xframe: false } }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(404) - .end((err, res) => { + .expect(res => { expect(res.headers['X-Frame-Options']).toBe(undefined); - done(err); }); }); - test('should get X-Frame-Options DENY by xframe = true', done => { + test('should get X-Frame-Options DENY by xframe = true', () => { const app = koala({ security: { xframe: true } }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(404) - .expect('X-Frame-Options', 'DENY') - .end(done); + .expect('X-Frame-Options', 'DENY'); }); - test('should get X-Frame-Options SAMEORIGIN by xframe = same', done => { + test('should get X-Frame-Options SAMEORIGIN by xframe = same', () => { const app = koala({ security: { xframe: 'same' } }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(404) - .expect('X-Frame-Options', 'SAMEORIGIN') - .end(done); + .expect('X-Frame-Options', 'SAMEORIGIN'); }); }); }); diff --git a/test/jsonp.test.js b/test/jsonp.test.js index 34f420e..1dc6e78 100644 --- a/test/jsonp.test.js +++ b/test/jsonp.test.js @@ -2,7 +2,7 @@ const koala = require('../lib'); const request = require('supertest'); describe('jsonp', () => { - test('should return jsonp response', done => { + test('should return jsonp response', () => { const app = koala({ jsonp: { callback: '_callback' @@ -12,13 +12,13 @@ describe('jsonp', () => { this.jsonp = {foo: 'bar'}; }); - request(app.listen()) + return request(app.listen()) .get('/user.json?_callback=fn') .expect(200) - .expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});', done); + .expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); }); - test('should return json response', done => { + test('should return json response', () => { const app = koala({ jsonp: { callback: '_callback' @@ -28,9 +28,9 @@ describe('jsonp', () => { this.jsonp = {foo: 'bar'}; }); - request(app.listen()) + return request(app.listen()) .get('/user.json') .expect(200) - .expect({'foo': 'bar'}, done); + .expect({'foo': 'bar'}); }); }); diff --git a/test/middleware.test.js b/test/middleware.test.js index 1a0b600..d6ebdf7 100644 --- a/test/middleware.test.js +++ b/test/middleware.test.js @@ -3,19 +3,18 @@ const request = require('supertest'); describe('Middlewares', () => { describe('Session', () => { - test('should has this.session by default', done => { + test('should has this.session by default', () => { const app = koala(); app.use(function * () { this.body = this.session; }); - request(app.listen()) + return request(app.listen()) .get('/') - .expect('{}') - .end(done); + .expect('{}'); }); - test('should has no this.session by options.session = false', done => { + test('should has no this.session by options.session = false', () => { const app = koala({ session: false }); @@ -24,10 +23,9 @@ describe('Middlewares', () => { this.body = this.session === undefined; }); - request(app.listen()) + return request(app.listen()) .get('/') - .expect('true') - .end(done); + .expect('true'); }); }); }); diff --git a/test/object-streams.test.js b/test/object-streams.test.js index ab55202..d136126 100644 --- a/test/object-streams.test.js +++ b/test/object-streams.test.js @@ -3,7 +3,7 @@ const request = require('supertest'); const PassThrough = require('stream').PassThrough; describe('Object Streams', () => { - test('should be supported', done => { + test('should be supported', () => { const app = koala(); app.use(function * (next) { const body = this.body = new PassThrough({ @@ -21,13 +21,13 @@ describe('Object Streams', () => { body.end(); }); - request(app.listen()) + return request(app.listen()) .get('/') .expect(200) .expect([{ message: 'a' }, { message: 'b' - }], done); + }]); }); }); diff --git a/test/query-string.test.js b/test/query-string.test.js index f38c5fc..9b98802 100644 --- a/test/query-string.test.js +++ b/test/query-string.test.js @@ -3,13 +3,13 @@ const request = require('supertest'); describe('Nested Query Strings', () => { describe('when options.qs = false', () => { - test('should not support nested query strings', done => { + test('should not support nested query strings', () => { const app = koala(); app.use(function * (next) { this.response.body = this.request.query; }); - request(app.listen()) + return request(app.listen()) .get('/') .query({ something: { @@ -19,12 +19,12 @@ describe('Nested Query Strings', () => { .expect(200) .expect({ 'something[nested]': 'true' - }, done); + }); }); }); describe('when options.qs = true', () => { - test('should support nested query strings', done => { + test('should support nested query strings', () => { const app = koala({ qs: true }); @@ -32,7 +32,7 @@ describe('Nested Query Strings', () => { this.response.body = this.request.query; }); - request(app.listen()) + return request(app.listen()) .get('/') .query({ something: { @@ -44,7 +44,7 @@ describe('Nested Query Strings', () => { something: { nested: 'true' } - }, done); + }); }); }); }); From 0c118858961c8c47615c77f4eabdc94b51073679 Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Thu, 7 Sep 2017 15:03:52 -0400 Subject: [PATCH 02/13] Move test files to lib --- .eslintignore | 1 + .eslintrc.yml | 11 ++++++++++- {test => lib}/basic-auth.test.js | 2 +- {test => lib}/body-parsing.test.js | 2 +- {test => lib}/cache-control.test.js | 2 +- {test => lib}/conditional-get.test.js | 2 +- {test => lib}/headers.test.js | 2 +- {test => lib}/jsonp.test.js | 2 +- {test => lib}/middleware.test.js | 2 +- {test => lib}/object-streams.test.js | 2 +- {test => lib}/query-string.test.js | 2 +- test/.eslintrc.yml | 6 ------ 12 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 .eslintignore rename {test => lib}/basic-auth.test.js (92%) rename {test => lib}/body-parsing.test.js (99%) rename {test => lib}/cache-control.test.js (98%) rename {test => lib}/conditional-get.test.js (95%) rename {test => lib}/headers.test.js (99%) rename {test => lib}/jsonp.test.js (95%) rename {test => lib}/middleware.test.js (95%) rename {test => lib}/object-streams.test.js (94%) rename {test => lib}/query-string.test.js (97%) delete mode 100644 test/.eslintrc.yml diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..ed9f9cc --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +coverage \ No newline at end of file diff --git a/.eslintrc.yml b/.eslintrc.yml index 58539da..938340f 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -1,4 +1,8 @@ -extends: standard +extends: + - standard + - plugin:jest/recommended + +plugins: [ jest ] rules: arrow-parens: [2, as-needed] @@ -12,3 +16,8 @@ rules: dot-location: [2, property] prefer-arrow-callback: 2 prefer-const: 2 + +overrides: + files: "*.test.js" + env: + jest/globals: true diff --git a/test/basic-auth.test.js b/lib/basic-auth.test.js similarity index 92% rename from test/basic-auth.test.js rename to lib/basic-auth.test.js index 07c746c..57a3aa4 100644 --- a/test/basic-auth.test.js +++ b/lib/basic-auth.test.js @@ -1,4 +1,4 @@ -const koala = require('../lib'); +const koala = require('.'); const request = require('supertest'); describe('Basic Auth', () => { diff --git a/test/body-parsing.test.js b/lib/body-parsing.test.js similarity index 99% rename from test/body-parsing.test.js rename to lib/body-parsing.test.js index 7b2e569..0c60d22 100644 --- a/test/body-parsing.test.js +++ b/lib/body-parsing.test.js @@ -1,4 +1,4 @@ -const koala = require('../lib'); +const koala = require('.'); const request = require('supertest'); describe('Body Parsing', () => { diff --git a/test/cache-control.test.js b/lib/cache-control.test.js similarity index 98% rename from test/cache-control.test.js rename to lib/cache-control.test.js index bfa1b75..48aa07c 100644 --- a/test/cache-control.test.js +++ b/lib/cache-control.test.js @@ -1,4 +1,4 @@ -const koala = require('../lib'); +const koala = require('.'); const request = require('supertest'); describe('Cache-Control', () => { diff --git a/test/conditional-get.test.js b/lib/conditional-get.test.js similarity index 95% rename from test/conditional-get.test.js rename to lib/conditional-get.test.js index 92297a0..7c8e3f9 100644 --- a/test/conditional-get.test.js +++ b/lib/conditional-get.test.js @@ -1,4 +1,4 @@ -const koala = require('../lib'); +const koala = require('.'); const request = require('supertest'); describe('Conditional-Get', () => { diff --git a/test/headers.test.js b/lib/headers.test.js similarity index 99% rename from test/headers.test.js rename to lib/headers.test.js index f74f0e3..acd0828 100644 --- a/test/headers.test.js +++ b/lib/headers.test.js @@ -1,4 +1,4 @@ -const koala = require('../lib'); +const koala = require('.'); const request = require('supertest'); describe('Set headers', () => { diff --git a/test/jsonp.test.js b/lib/jsonp.test.js similarity index 95% rename from test/jsonp.test.js rename to lib/jsonp.test.js index 1dc6e78..f54211a 100644 --- a/test/jsonp.test.js +++ b/lib/jsonp.test.js @@ -1,4 +1,4 @@ -const koala = require('../lib'); +const koala = require('.'); const request = require('supertest'); describe('jsonp', () => { diff --git a/test/middleware.test.js b/lib/middleware.test.js similarity index 95% rename from test/middleware.test.js rename to lib/middleware.test.js index d6ebdf7..91cf488 100644 --- a/test/middleware.test.js +++ b/lib/middleware.test.js @@ -1,4 +1,4 @@ -const koala = require('../lib'); +const koala = require('.'); const request = require('supertest'); describe('Middlewares', () => { diff --git a/test/object-streams.test.js b/lib/object-streams.test.js similarity index 94% rename from test/object-streams.test.js rename to lib/object-streams.test.js index d136126..e353560 100644 --- a/test/object-streams.test.js +++ b/lib/object-streams.test.js @@ -1,4 +1,4 @@ -const koala = require('../lib'); +const koala = require('.'); const request = require('supertest'); const PassThrough = require('stream').PassThrough; diff --git a/test/query-string.test.js b/lib/query-string.test.js similarity index 97% rename from test/query-string.test.js rename to lib/query-string.test.js index 9b98802..42d4898 100644 --- a/test/query-string.test.js +++ b/lib/query-string.test.js @@ -1,4 +1,4 @@ -const koala = require('../lib'); +const koala = require('.'); const request = require('supertest'); describe('Nested Query Strings', () => { diff --git a/test/.eslintrc.yml b/test/.eslintrc.yml deleted file mode 100644 index 57cff84..0000000 --- a/test/.eslintrc.yml +++ /dev/null @@ -1,6 +0,0 @@ -extends: [ plugin:jest/recommended ] - -env: - jest/globals: true - -plugins: [ jest ] From 014909c77920b57d46ab32c3d168edd7fe547bac Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Thu, 7 Sep 2017 15:15:30 -0400 Subject: [PATCH 03/13] Move eslint config to package.json --- .eslintignore | 1 - .eslintrc.yml | 23 ----------------------- package.json | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 24 deletions(-) delete mode 100644 .eslintignore delete mode 100644 .eslintrc.yml diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index ed9f9cc..0000000 --- a/.eslintignore +++ /dev/null @@ -1 +0,0 @@ -coverage \ No newline at end of file diff --git a/.eslintrc.yml b/.eslintrc.yml deleted file mode 100644 index 938340f..0000000 --- a/.eslintrc.yml +++ /dev/null @@ -1,23 +0,0 @@ -extends: - - standard - - plugin:jest/recommended - -plugins: [ jest ] - -rules: - arrow-parens: [2, as-needed] - eqeqeq: 0 - no-return-assign: 0 - no-var: 2 - semi: [2, always] - space-before-function-paren: [2, never] - yoda: 0 - arrow-spacing: 2 - dot-location: [2, property] - prefer-arrow-callback: 2 - prefer-const: 2 - -overrides: - files: "*.test.js" - env: - jest/globals: true diff --git a/package.json b/package.json index 23563b9..a8f61fc 100644 --- a/package.json +++ b/package.json @@ -75,5 +75,48 @@ "promises", "push" ], + "eslintConfig": { + "extends": [ + "standard", + "plugin:jest/recommended" + ], + "plugins": [ + "jest" + ], + "rules": { + "arrow-parens": [ + 2, + "as-needed" + ], + "eqeqeq": 0, + "no-return-assign": 0, + "no-var": 2, + "semi": [ + 2, + "always" + ], + "space-before-function-paren": [ + 2, + "never" + ], + "yoda": 0, + "arrow-spacing": 2, + "dot-location": [ + 2, + "property" + ], + "prefer-arrow-callback": 2, + "prefer-const": 2 + }, + "overrides": { + "files": "*.test.js", + "env": { + "jest/globals": true + } + } + }, + "eslintIgnore": [ + "coverage" + ], "snyk": true } From 10f8dd373af2c81d48e6285e5cb093f919df183d Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Thu, 7 Sep 2017 16:14:30 -0400 Subject: [PATCH 04/13] Add coveralls to package.json --- .travis.yml | 2 +- package-lock.json | 151 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 153 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a51c800..aec992e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ node_js: sudo: false before_install: "npm install --global npm" script: "npm run validate" -after_script: "npm install coveralls && cat ./coverage/lcov.info | coveralls" +after_script: "cat ./coverage/lcov.info | coveralls" notifications: webhooks: urls: diff --git a/package-lock.json b/package-lock.json index b70fa9a..74b540c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1049,6 +1049,106 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, + "coveralls": { + "version": "2.13.1", + "resolved": "https://registry.npmjs.org/coveralls/-/coveralls-2.13.1.tgz", + "integrity": "sha1-1wu5rMGDXsTwY/+drFQjwXsR8Xg=", + "dev": true, + "requires": { + "js-yaml": "3.6.1", + "lcov-parse": "0.0.10", + "log-driver": "1.2.5", + "minimist": "1.2.0", + "request": "2.79.0" + }, + "dependencies": { + "caseless": { + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.11.0.tgz", + "integrity": "sha1-cVuW6phBWTzDMGeSP17GDr2k99c=", + "dev": true + }, + "form-data": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.1.4.tgz", + "integrity": "sha1-M8GDrPGTJ27KqYFDpp6Uv+4XUNE=", + "dev": true, + "requires": { + "asynckit": "0.4.0", + "combined-stream": "1.0.5", + "mime-types": "2.1.17" + } + }, + "har-validator": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/har-validator/-/har-validator-2.0.6.tgz", + "integrity": "sha1-zcvAgYgmWtEZtqWnyKtw7s+10n0=", + "dev": true, + "requires": { + "chalk": "1.1.3", + "commander": "2.11.0", + "is-my-json-valid": "2.16.1", + "pinkie-promise": "2.0.1" + } + }, + "js-yaml": { + "version": "3.6.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.6.1.tgz", + "integrity": "sha1-bl/mfYsgXOTSL60Ft3geja3MSzA=", + "dev": true, + "requires": { + "argparse": "1.0.9", + "esprima": "2.7.3" + } + }, + "minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", + "dev": true + }, + "qs": { + "version": "6.3.2", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.3.2.tgz", + "integrity": "sha1-51vV9uJoEioqDgvaYwslUMFmUCw=", + "dev": true + }, + "request": { + "version": "2.79.0", + "resolved": "https://registry.npmjs.org/request/-/request-2.79.0.tgz", + "integrity": "sha1-Tf5b9r6LjNw3/Pk+BLZVd3InEN4=", + "dev": true, + "requires": { + "aws-sign2": "0.6.0", + "aws4": "1.6.0", + "caseless": "0.11.0", + "combined-stream": "1.0.5", + "extend": "3.0.0", + "forever-agent": "0.6.1", + "form-data": "2.1.4", + "har-validator": "2.0.6", + "hawk": "3.1.3", + "http-signature": "1.1.1", + "is-typedarray": "1.0.0", + "isstream": "0.1.2", + "json-stringify-safe": "5.0.1", + "mime-types": "2.1.17", + "oauth-sign": "0.8.2", + "qs": "6.3.2", + "stringstream": "0.0.5", + "tough-cookie": "2.3.2", + "tunnel-agent": "0.4.3", + "uuid": "3.1.0" + } + }, + "tunnel-agent": { + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.4.3.tgz", + "integrity": "sha1-Y3PbdpCf5XDgjXNYM2Xtgop07us=", + "dev": true + } + } + }, "crc": { "version": "3.4.4", "resolved": "https://registry.npmjs.org/crc/-/crc-3.4.4.tgz", @@ -3203,6 +3303,21 @@ "integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=", "dev": true }, + "generate-function": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.0.0.tgz", + "integrity": "sha1-aFj+fAlpt9TpCTM3ZHrHn2DfvnQ=", + "dev": true + }, + "generate-object-property": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/generate-object-property/-/generate-object-property-1.2.0.tgz", + "integrity": "sha1-nA4cQDCM6AT0eDYYuTf6iPmdUNA=", + "dev": true, + "requires": { + "is-property": "1.0.2" + } + }, "get-caller-file": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.2.tgz", @@ -3696,6 +3811,18 @@ "is-extglob": "1.0.0" } }, + "is-my-json-valid": { + "version": "2.16.1", + "resolved": "https://registry.npmjs.org/is-my-json-valid/-/is-my-json-valid-2.16.1.tgz", + "integrity": "sha512-ochPsqWS1WXj8ZnMIV0vnNXooaMhp7cyL4FMSIPKTtnV0Ha/T19G2b9kkhcNsabV9bxYkze7/aLZJb/bYuFduQ==", + "dev": true, + "requires": { + "generate-function": "2.0.0", + "generate-object-property": "1.2.0", + "jsonpointer": "4.0.1", + "xtend": "4.0.1" + } + }, "is-npm": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-npm/-/is-npm-1.0.0.tgz", @@ -3768,6 +3895,12 @@ "integrity": "sha1-eaKp7OfwlugPNtKy87wWwf9L8/o=", "dev": true }, + "is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=", + "dev": true + }, "is-redirect": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-redirect/-/is-redirect-1.0.0.tgz", @@ -4982,6 +5115,12 @@ "resolved": "https://registry.npmjs.org/jsonp-body/-/jsonp-body-1.0.0.tgz", "integrity": "sha1-5hD7b86nnPDMnye6p7Vjd9SwuzY=" }, + "jsonpointer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/jsonpointer/-/jsonpointer-4.0.1.tgz", + "integrity": "sha1-T9kss04OnbPInIYi7PUfm5eMbLk=", + "dev": true + }, "jsprim": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz", @@ -5254,6 +5393,12 @@ "invert-kv": "1.0.0" } }, + "lcov-parse": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/lcov-parse/-/lcov-parse-0.0.10.tgz", + "integrity": "sha1-GwuP+ayceIklBYK3C3ExXZ2m2aM=", + "dev": true + }, "leven": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/leven/-/leven-2.1.0.tgz", @@ -5342,6 +5487,12 @@ "integrity": "sha1-FQzwoWeR9ZA7iJHqsVRgknS96lU=", "dev": true }, + "log-driver": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/log-driver/-/log-driver-1.2.5.tgz", + "integrity": "sha1-euTsJXMC/XkNVXyxDJcQDYV7AFY=", + "dev": true + }, "longest": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz", diff --git a/package.json b/package.json index a8f61fc..667b72f 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "qs": ">= 2" }, "devDependencies": { + "coveralls": "*", "eslint": "^4.6.1", "eslint-config-standard": "^10.2.1", "eslint-plugin-import": "^2.7.0", From 3a15bff1c076a00e99c973cf0defd6ff983b1a1c Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Thu, 7 Sep 2017 16:17:01 -0400 Subject: [PATCH 05/13] Simplify Travis config syntax --- .travis.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index aec992e..55c0f5f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,8 @@ language: node_js node_js: - 6 - 8 -sudo: false -before_install: "npm install --global npm" -script: "npm run validate" -after_script: "cat ./coverage/lcov.info | coveralls" +before_install: npm install --global npm +after_script: coveralls < coverage/lcov.info notifications: webhooks: urls: From 668f4801d8a7ea4d92e8e429434e2ef6480238fc Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Sat, 9 Sep 2017 04:17:12 -0400 Subject: [PATCH 06/13] Adjust scripts so test includes lint --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 667b72f..2418baa 100644 --- a/package.json +++ b/package.json @@ -57,9 +57,9 @@ "template" ], "scripts": { - "test": "jest --forceExit", - "coverage": "npm run test -- --coverage", - "validate": "npm-run-all lint coverage", + "tests": "jest --forceExit", + "coverage": "npm run tests -- --coverage", + "test": "npm-run-all lint coverage", "snyk-protect": "snyk protect", "prepare": "npm run snyk-protect", "lint": "eslint . bin/koala" From 87de09933fbaa5a13907828f7910bf11b5635cfc Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Sat, 9 Sep 2017 04:40:04 -0400 Subject: [PATCH 07/13] Use one __tests__ directory per source directory --- lib/{ => __tests__}/basic-auth.test.js | 2 +- lib/{ => __tests__}/body-parsing.test.js | 2 +- lib/{ => __tests__}/cache-control.test.js | 2 +- lib/{ => __tests__}/conditional-get.test.js | 2 +- lib/{ => __tests__}/headers.test.js | 2 +- lib/{ => __tests__}/jsonp.test.js | 2 +- lib/{ => __tests__}/object-streams.test.js | 2 +- lib/{ => __tests__}/query-string.test.js | 2 +- lib/{ => middleware/__tests__}/middleware.test.js | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) rename lib/{ => __tests__}/basic-auth.test.js (93%) rename lib/{ => __tests__}/body-parsing.test.js (99%) rename lib/{ => __tests__}/cache-control.test.js (99%) rename lib/{ => __tests__}/conditional-get.test.js (95%) rename lib/{ => __tests__}/headers.test.js (99%) rename lib/{ => __tests__}/jsonp.test.js (96%) rename lib/{ => __tests__}/object-streams.test.js (95%) rename lib/{ => __tests__}/query-string.test.js (97%) rename lib/{ => middleware/__tests__}/middleware.test.js (95%) diff --git a/lib/basic-auth.test.js b/lib/__tests__/basic-auth.test.js similarity index 93% rename from lib/basic-auth.test.js rename to lib/__tests__/basic-auth.test.js index 57a3aa4..ad55931 100644 --- a/lib/basic-auth.test.js +++ b/lib/__tests__/basic-auth.test.js @@ -1,4 +1,4 @@ -const koala = require('.'); +const koala = require('..'); const request = require('supertest'); describe('Basic Auth', () => { diff --git a/lib/body-parsing.test.js b/lib/__tests__/body-parsing.test.js similarity index 99% rename from lib/body-parsing.test.js rename to lib/__tests__/body-parsing.test.js index 0c60d22..10355cf 100644 --- a/lib/body-parsing.test.js +++ b/lib/__tests__/body-parsing.test.js @@ -1,4 +1,4 @@ -const koala = require('.'); +const koala = require('..'); const request = require('supertest'); describe('Body Parsing', () => { diff --git a/lib/cache-control.test.js b/lib/__tests__/cache-control.test.js similarity index 99% rename from lib/cache-control.test.js rename to lib/__tests__/cache-control.test.js index 48aa07c..2c5e8e6 100644 --- a/lib/cache-control.test.js +++ b/lib/__tests__/cache-control.test.js @@ -1,4 +1,4 @@ -const koala = require('.'); +const koala = require('..'); const request = require('supertest'); describe('Cache-Control', () => { diff --git a/lib/conditional-get.test.js b/lib/__tests__/conditional-get.test.js similarity index 95% rename from lib/conditional-get.test.js rename to lib/__tests__/conditional-get.test.js index 7c8e3f9..132dcbc 100644 --- a/lib/conditional-get.test.js +++ b/lib/__tests__/conditional-get.test.js @@ -1,4 +1,4 @@ -const koala = require('.'); +const koala = require('..'); const request = require('supertest'); describe('Conditional-Get', () => { diff --git a/lib/headers.test.js b/lib/__tests__/headers.test.js similarity index 99% rename from lib/headers.test.js rename to lib/__tests__/headers.test.js index acd0828..a97810f 100644 --- a/lib/headers.test.js +++ b/lib/__tests__/headers.test.js @@ -1,4 +1,4 @@ -const koala = require('.'); +const koala = require('..'); const request = require('supertest'); describe('Set headers', () => { diff --git a/lib/jsonp.test.js b/lib/__tests__/jsonp.test.js similarity index 96% rename from lib/jsonp.test.js rename to lib/__tests__/jsonp.test.js index f54211a..8ea5fef 100644 --- a/lib/jsonp.test.js +++ b/lib/__tests__/jsonp.test.js @@ -1,4 +1,4 @@ -const koala = require('.'); +const koala = require('..'); const request = require('supertest'); describe('jsonp', () => { diff --git a/lib/object-streams.test.js b/lib/__tests__/object-streams.test.js similarity index 95% rename from lib/object-streams.test.js rename to lib/__tests__/object-streams.test.js index e353560..07e9fe9 100644 --- a/lib/object-streams.test.js +++ b/lib/__tests__/object-streams.test.js @@ -1,4 +1,4 @@ -const koala = require('.'); +const koala = require('..'); const request = require('supertest'); const PassThrough = require('stream').PassThrough; diff --git a/lib/query-string.test.js b/lib/__tests__/query-string.test.js similarity index 97% rename from lib/query-string.test.js rename to lib/__tests__/query-string.test.js index 42d4898..1409b51 100644 --- a/lib/query-string.test.js +++ b/lib/__tests__/query-string.test.js @@ -1,4 +1,4 @@ -const koala = require('.'); +const koala = require('..'); const request = require('supertest'); describe('Nested Query Strings', () => { diff --git a/lib/middleware.test.js b/lib/middleware/__tests__/middleware.test.js similarity index 95% rename from lib/middleware.test.js rename to lib/middleware/__tests__/middleware.test.js index 91cf488..d2fcbf9 100644 --- a/lib/middleware.test.js +++ b/lib/middleware/__tests__/middleware.test.js @@ -1,4 +1,4 @@ -const koala = require('.'); +const koala = require('../..'); const request = require('supertest'); describe('Middlewares', () => { From 264e3e14032222f0f8cc697b0293c68ca59703fa Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Sat, 9 Sep 2017 05:07:02 -0400 Subject: [PATCH 08/13] Remove .test extension --- lib/__tests__/{basic-auth.test.js => basic-auth.js} | 0 lib/__tests__/{body-parsing.test.js => body-parsing.js} | 0 lib/__tests__/{cache-control.test.js => cache-control.js} | 0 lib/__tests__/{conditional-get.test.js => conditional-get.js} | 0 lib/__tests__/{headers.test.js => headers.js} | 0 lib/__tests__/{jsonp.test.js => jsonp.js} | 0 lib/__tests__/{object-streams.test.js => object-streams.js} | 0 lib/__tests__/{query-string.test.js => query-string.js} | 0 lib/middleware/__tests__/{middleware.test.js => middleware.js} | 0 package.json | 2 +- 10 files changed, 1 insertion(+), 1 deletion(-) rename lib/__tests__/{basic-auth.test.js => basic-auth.js} (100%) rename lib/__tests__/{body-parsing.test.js => body-parsing.js} (100%) rename lib/__tests__/{cache-control.test.js => cache-control.js} (100%) rename lib/__tests__/{conditional-get.test.js => conditional-get.js} (100%) rename lib/__tests__/{headers.test.js => headers.js} (100%) rename lib/__tests__/{jsonp.test.js => jsonp.js} (100%) rename lib/__tests__/{object-streams.test.js => object-streams.js} (100%) rename lib/__tests__/{query-string.test.js => query-string.js} (100%) rename lib/middleware/__tests__/{middleware.test.js => middleware.js} (100%) diff --git a/lib/__tests__/basic-auth.test.js b/lib/__tests__/basic-auth.js similarity index 100% rename from lib/__tests__/basic-auth.test.js rename to lib/__tests__/basic-auth.js diff --git a/lib/__tests__/body-parsing.test.js b/lib/__tests__/body-parsing.js similarity index 100% rename from lib/__tests__/body-parsing.test.js rename to lib/__tests__/body-parsing.js diff --git a/lib/__tests__/cache-control.test.js b/lib/__tests__/cache-control.js similarity index 100% rename from lib/__tests__/cache-control.test.js rename to lib/__tests__/cache-control.js diff --git a/lib/__tests__/conditional-get.test.js b/lib/__tests__/conditional-get.js similarity index 100% rename from lib/__tests__/conditional-get.test.js rename to lib/__tests__/conditional-get.js diff --git a/lib/__tests__/headers.test.js b/lib/__tests__/headers.js similarity index 100% rename from lib/__tests__/headers.test.js rename to lib/__tests__/headers.js diff --git a/lib/__tests__/jsonp.test.js b/lib/__tests__/jsonp.js similarity index 100% rename from lib/__tests__/jsonp.test.js rename to lib/__tests__/jsonp.js diff --git a/lib/__tests__/object-streams.test.js b/lib/__tests__/object-streams.js similarity index 100% rename from lib/__tests__/object-streams.test.js rename to lib/__tests__/object-streams.js diff --git a/lib/__tests__/query-string.test.js b/lib/__tests__/query-string.js similarity index 100% rename from lib/__tests__/query-string.test.js rename to lib/__tests__/query-string.js diff --git a/lib/middleware/__tests__/middleware.test.js b/lib/middleware/__tests__/middleware.js similarity index 100% rename from lib/middleware/__tests__/middleware.test.js rename to lib/middleware/__tests__/middleware.js diff --git a/package.json b/package.json index 2418baa..c26a10b 100644 --- a/package.json +++ b/package.json @@ -110,7 +110,7 @@ "prefer-const": 2 }, "overrides": { - "files": "*.test.js", + "files": "**/__tests__/*", "env": { "jest/globals": true } From 6b3f8ea534c2dbf8decd93cdffee386272c500c2 Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Sat, 9 Sep 2017 08:31:14 -0400 Subject: [PATCH 09/13] Use Jest's package.json support instead of --coverage --- package.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c26a10b..bd5530b 100644 --- a/package.json +++ b/package.json @@ -58,8 +58,7 @@ ], "scripts": { "tests": "jest --forceExit", - "coverage": "npm run tests -- --coverage", - "test": "npm-run-all lint coverage", + "test": "npm-run-all lint tests", "snyk-protect": "snyk protect", "prepare": "npm run snyk-protect", "lint": "eslint . bin/koala" @@ -119,5 +118,8 @@ "eslintIgnore": [ "coverage" ], + "jest": { + "collectCoverage": true + }, "snyk": true } From 31b6f42609f6c0ec20713a84a6f2ea626e9a49ae Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Sat, 9 Sep 2017 08:50:01 -0400 Subject: [PATCH 10/13] Remove --forceExit antipattern by closing all servers in tests --- lib/__tests__/basic-auth.js | 2 +- lib/__tests__/body-parsing.js | 22 +++++++++++----------- lib/__tests__/cache-control.js | 16 ++++++++-------- lib/__tests__/conditional-get.js | 2 +- lib/__tests__/headers.js | 20 ++++++++++---------- lib/__tests__/jsonp.js | 4 ++-- lib/__tests__/object-streams.js | 2 +- lib/__tests__/query-string.js | 4 ++-- lib/middleware/__tests__/middleware.js | 4 ++-- package.json | 2 +- 10 files changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/__tests__/basic-auth.js b/lib/__tests__/basic-auth.js index ad55931..9ef542f 100644 --- a/lib/__tests__/basic-auth.js +++ b/lib/__tests__/basic-auth.js @@ -8,7 +8,7 @@ describe('Basic Auth', () => { this.body = this.request.basicAuth; }); - return request(app.listen()) + return request(app.callback()) .get('/') .auth('username', 'password') .expect(200) diff --git a/lib/__tests__/body-parsing.js b/lib/__tests__/body-parsing.js index 10355cf..2444bf5 100644 --- a/lib/__tests__/body-parsing.js +++ b/lib/__tests__/body-parsing.js @@ -8,7 +8,7 @@ describe('Body Parsing', () => { app.use(function * () { this.body = yield * this.request.json(); }); - return request(app.listen()) + return request(app.callback()) .post('/') .send({ message: 'lol' @@ -23,7 +23,7 @@ describe('Body Parsing', () => { app.use(function * () { this.body = yield * this.request.json(); }); - return request(app.listen()) + return request(app.callback()) .post('/') .type('json') .send('"lol"') @@ -36,7 +36,7 @@ describe('Body Parsing', () => { app.use(function * () { this.body = yield * this.request.json(); }); - return request(app.listen()) + return request(app.callback()) .post('/') .type('json') .send('"lol"') @@ -51,7 +51,7 @@ describe('Body Parsing', () => { app.use(function * () { this.body = yield * this.request.urlencoded(); }); - return request(app.listen()) + return request(app.callback()) .post('/') .send('message=lol') .expect(200) @@ -64,7 +64,7 @@ describe('Body Parsing', () => { app.use(function * () { this.body = yield * this.request.urlencoded(); }); - return request(app.listen()) + return request(app.callback()) .post('/') .type('form') .send({ @@ -83,7 +83,7 @@ describe('Body Parsing', () => { app.use(function * () { this.body = yield * this.request.urlencoded(); }); - return request(app.listen()) + return request(app.callback()) .post('/') .type('form') .send({ @@ -107,7 +107,7 @@ describe('Body Parsing', () => { this.body = yield * this.request.text(); expect(typeof this.body).toBe('string'); }); - return request(app.listen()) + return request(app.callback()) .post('/') .send('message=lol') .expect(200) @@ -120,7 +120,7 @@ describe('Body Parsing', () => { yield * this.request.text('1kb'); this.body = 204; }); - return request(app.listen()) + return request(app.callback()) .post('/') .send(Buffer.alloc(2048)) .expect(413); @@ -134,7 +134,7 @@ describe('Body Parsing', () => { this.body = yield * this.request.buffer(); expect(Buffer.isBuffer(this.body)).toBeTruthy(); }); - return request(app.listen()) + return request(app.callback()) .post('/') .send('message=lol') .expect(200) @@ -147,7 +147,7 @@ describe('Body Parsing', () => { yield * this.request.buffer('1kb'); this.body = 204; }); - return request(app.listen()) + return request(app.callback()) .post('/') .send(Buffer.alloc(2048)) .expect(413); @@ -160,7 +160,7 @@ describe('Body Parsing', () => { app.use(function * () { this.body = yield * this.request.json(); }); - return request(app.listen()) + return request(app.callback()) .get('/') .set('expect', '100-continue') .set('content-type', 'application/json') diff --git a/lib/__tests__/cache-control.js b/lib/__tests__/cache-control.js index 2c5e8e6..babdf7c 100644 --- a/lib/__tests__/cache-control.js +++ b/lib/__tests__/cache-control.js @@ -10,7 +10,7 @@ describe('Cache-Control', () => { this.status = 204; }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(204) .expect('Cache-Control', 'public, max-age=1'); @@ -23,7 +23,7 @@ describe('Cache-Control', () => { this.status = 204; }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(204) .expect('Cache-Control', 'public, max-age=1'); @@ -36,7 +36,7 @@ describe('Cache-Control', () => { this.status = 204; }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(204) .expect('Cache-Control', 'public, max-age=1'); @@ -51,7 +51,7 @@ describe('Cache-Control', () => { this.status = 204; }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(204) .expect('Cache-Control', 'public, max-age=1000'); @@ -66,7 +66,7 @@ describe('Cache-Control', () => { this.status = 204; }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(204) .expect('Cache-Control', 'public, max-age=3600'); @@ -81,7 +81,7 @@ describe('Cache-Control', () => { this.status = 204; }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(204) .expect('Cache-Control', 'private, no-cache'); @@ -96,7 +96,7 @@ describe('Cache-Control', () => { this.status = 204; }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(204) .expect('Cache-Control', 'foo'); @@ -111,7 +111,7 @@ describe('Cache-Control', () => { this.status = 204; }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(500); }); diff --git a/lib/__tests__/conditional-get.js b/lib/__tests__/conditional-get.js index 132dcbc..f1e9744 100644 --- a/lib/__tests__/conditional-get.js +++ b/lib/__tests__/conditional-get.js @@ -10,7 +10,7 @@ describe('Conditional-Get', () => { this.body = 'hello'; }); - const server = app.listen(); + const server = app.listen().close(); test('should set an etag', () => { return request(server) diff --git a/lib/__tests__/headers.js b/lib/__tests__/headers.js index a97810f..d028227 100644 --- a/lib/__tests__/headers.js +++ b/lib/__tests__/headers.js @@ -6,7 +6,7 @@ describe('Set headers', () => { test('should get X-Response-Time correctly by default', () => { const app = koala(); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(404) .expect('X-Response-Time', /s$/); @@ -18,7 +18,7 @@ describe('Set headers', () => { responseTime: false }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(404) .expect(res => { @@ -32,7 +32,7 @@ describe('Set headers', () => { test('should not set Strict-Transport-Security by default', () => { const app = koala(); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(404) .expect(res => { @@ -46,7 +46,7 @@ describe('Set headers', () => { } }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(404) .expect('Strict-Transport-Security', 'max-age=1'); @@ -60,7 +60,7 @@ describe('Set headers', () => { } }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(404) .expect('Strict-Transport-Security', 'max-age=1'); @@ -73,7 +73,7 @@ describe('Set headers', () => { } }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(404) .expect('Strict-Transport-Security', 'max-age=1; includeSubDomains'); @@ -84,7 +84,7 @@ describe('Set headers', () => { test('should get X-Frame-Options DENY by default', () => { const app = koala(); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(404) .expect('X-Frame-Options', 'DENY'); @@ -96,7 +96,7 @@ describe('Set headers', () => { } }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(404) .expect(res => { @@ -110,7 +110,7 @@ describe('Set headers', () => { } }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(404) .expect('X-Frame-Options', 'DENY'); @@ -122,7 +122,7 @@ describe('Set headers', () => { } }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(404) .expect('X-Frame-Options', 'SAMEORIGIN'); diff --git a/lib/__tests__/jsonp.js b/lib/__tests__/jsonp.js index 8ea5fef..1e413a4 100644 --- a/lib/__tests__/jsonp.js +++ b/lib/__tests__/jsonp.js @@ -12,7 +12,7 @@ describe('jsonp', () => { this.jsonp = {foo: 'bar'}; }); - return request(app.listen()) + return request(app.callback()) .get('/user.json?_callback=fn') .expect(200) .expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); @@ -28,7 +28,7 @@ describe('jsonp', () => { this.jsonp = {foo: 'bar'}; }); - return request(app.listen()) + return request(app.callback()) .get('/user.json') .expect(200) .expect({'foo': 'bar'}); diff --git a/lib/__tests__/object-streams.js b/lib/__tests__/object-streams.js index 07e9fe9..b30044c 100644 --- a/lib/__tests__/object-streams.js +++ b/lib/__tests__/object-streams.js @@ -21,7 +21,7 @@ describe('Object Streams', () => { body.end(); }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect(200) .expect([{ diff --git a/lib/__tests__/query-string.js b/lib/__tests__/query-string.js index 1409b51..b605ecd 100644 --- a/lib/__tests__/query-string.js +++ b/lib/__tests__/query-string.js @@ -9,7 +9,7 @@ describe('Nested Query Strings', () => { this.response.body = this.request.query; }); - return request(app.listen()) + return request(app.callback()) .get('/') .query({ something: { @@ -32,7 +32,7 @@ describe('Nested Query Strings', () => { this.response.body = this.request.query; }); - return request(app.listen()) + return request(app.callback()) .get('/') .query({ something: { diff --git a/lib/middleware/__tests__/middleware.js b/lib/middleware/__tests__/middleware.js index d2fcbf9..37176c9 100644 --- a/lib/middleware/__tests__/middleware.js +++ b/lib/middleware/__tests__/middleware.js @@ -10,7 +10,7 @@ describe('Middlewares', () => { this.body = this.session; }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect('{}'); }); @@ -23,7 +23,7 @@ describe('Middlewares', () => { this.body = this.session === undefined; }); - return request(app.listen()) + return request(app.callback()) .get('/') .expect('true'); }); diff --git a/package.json b/package.json index bd5530b..bf538f9 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "template" ], "scripts": { - "tests": "jest --forceExit", + "tests": "jest", "test": "npm-run-all lint tests", "snyk-protect": "snyk protect", "prepare": "npm run snyk-protect", From ff6191b2dffdaf7d667de4de973c8b9ca4d69aa6 Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Mon, 18 Sep 2017 08:50:10 -0400 Subject: [PATCH 11/13] Update package-lock.json --- package-lock.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/package-lock.json b/package-lock.json index 6ac0fbb..86f8f9a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1063,6 +1063,12 @@ "integrity": "sha1-cVuW6phBWTzDMGeSP17GDr2k99c=", "dev": true }, + "esprima": { + "version": "2.7.3", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-2.7.3.tgz", + "integrity": "sha1-luO3DVd59q1JzQMmc9HDEnZ7pYE=", + "dev": true + }, "form-data": { "version": "2.1.4", "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.1.4.tgz", From 92ce6fbb2d7543bd30d9479d74fd6155ccad13bc Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Mon, 18 Sep 2017 08:56:57 -0400 Subject: [PATCH 12/13] Rename tests to match their implementation directory structure --- lib/__tests__/{body-parsing.js => index.js} | 82 +++++++++++++++++ lib/__tests__/jsonp.js | 36 -------- lib/__tests__/object-streams.js | 33 ------- lib/__tests__/query-string.js | 50 ----------- lib/__tests__/{basic-auth.js => request.js} | 0 .../{cache-control.js => response.js} | 0 .../__tests__/conditional-get.js | 2 +- lib/{ => middleware}/__tests__/headers.js | 28 +----- lib/middleware/__tests__/index.js | 88 +++++++++++++++++++ lib/middleware/__tests__/middleware.js | 31 ------- 10 files changed, 172 insertions(+), 178 deletions(-) rename lib/__tests__/{body-parsing.js => index.js} (69%) delete mode 100644 lib/__tests__/jsonp.js delete mode 100644 lib/__tests__/object-streams.js delete mode 100644 lib/__tests__/query-string.js rename lib/__tests__/{basic-auth.js => request.js} (100%) rename lib/__tests__/{cache-control.js => response.js} (100%) rename lib/{ => middleware}/__tests__/conditional-get.js (95%) rename lib/{ => middleware}/__tests__/headers.js (79%) create mode 100644 lib/middleware/__tests__/index.js delete mode 100644 lib/middleware/__tests__/middleware.js diff --git a/lib/__tests__/body-parsing.js b/lib/__tests__/index.js similarity index 69% rename from lib/__tests__/body-parsing.js rename to lib/__tests__/index.js index 2444bf5..88d838b 100644 --- a/lib/__tests__/body-parsing.js +++ b/lib/__tests__/index.js @@ -167,4 +167,86 @@ describe('Body Parsing', () => { .send('message=lol'); }); }); + + describe('Nested Query Strings', () => { + describe('when options.qs = false', () => { + test('should not support nested query strings', () => { + const app = koala(); + app.use(function * (next) { + this.response.body = this.request.query; + }); + + return request(app.callback()) + .get('/') + .query({ + something: { + nested: true + } + }) + .expect(200) + .expect({ + 'something[nested]': 'true' + }); + }); + }); + + describe('when options.qs = true', () => { + test('should support nested query strings', () => { + const app = koala({ + qs: true + }); + app.use(function * (next) { + this.response.body = this.request.query; + }); + + return request(app.callback()) + .get('/') + .query({ + something: { + nested: true + } + }) + .expect(200) + .expect({ + something: { + nested: 'true' + } + }); + }); + }); + }); + + describe('jsonp', () => { + test('should return jsonp response', () => { + const app = koala({ + jsonp: { + callback: '_callback' + } + }); + app.use(function * (next) { + this.jsonp = {foo: 'bar'}; + }); + + return request(app.callback()) + .get('/user.json?_callback=fn') + .expect(200) + .expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); + }); + + test('should return json response', () => { + const app = koala({ + jsonp: { + callback: '_callback' + } + }); + app.use(function * (next) { + this.jsonp = {foo: 'bar'}; + }); + + return request(app.callback()) + .get('/user.json') + .expect(200) + .expect({'foo': 'bar'}); + }); + }); }); diff --git a/lib/__tests__/jsonp.js b/lib/__tests__/jsonp.js deleted file mode 100644 index 1e413a4..0000000 --- a/lib/__tests__/jsonp.js +++ /dev/null @@ -1,36 +0,0 @@ -const koala = require('..'); -const request = require('supertest'); - -describe('jsonp', () => { - test('should return jsonp response', () => { - const app = koala({ - jsonp: { - callback: '_callback' - } - }); - app.use(function * (next) { - this.jsonp = {foo: 'bar'}; - }); - - return request(app.callback()) - .get('/user.json?_callback=fn') - .expect(200) - .expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); - }); - - test('should return json response', () => { - const app = koala({ - jsonp: { - callback: '_callback' - } - }); - app.use(function * (next) { - this.jsonp = {foo: 'bar'}; - }); - - return request(app.callback()) - .get('/user.json') - .expect(200) - .expect({'foo': 'bar'}); - }); -}); diff --git a/lib/__tests__/object-streams.js b/lib/__tests__/object-streams.js deleted file mode 100644 index b30044c..0000000 --- a/lib/__tests__/object-streams.js +++ /dev/null @@ -1,33 +0,0 @@ -const koala = require('..'); -const request = require('supertest'); -const PassThrough = require('stream').PassThrough; - -describe('Object Streams', () => { - test('should be supported', () => { - const app = koala(); - app.use(function * (next) { - const body = this.body = new PassThrough({ - objectMode: true - }); - - body.write({ - message: 'a' - }); - - body.write({ - message: 'b' - }); - - body.end(); - }); - - return request(app.callback()) - .get('/') - .expect(200) - .expect([{ - message: 'a' - }, { - message: 'b' - }]); - }); -}); diff --git a/lib/__tests__/query-string.js b/lib/__tests__/query-string.js deleted file mode 100644 index b605ecd..0000000 --- a/lib/__tests__/query-string.js +++ /dev/null @@ -1,50 +0,0 @@ -const koala = require('..'); -const request = require('supertest'); - -describe('Nested Query Strings', () => { - describe('when options.qs = false', () => { - test('should not support nested query strings', () => { - const app = koala(); - app.use(function * (next) { - this.response.body = this.request.query; - }); - - return request(app.callback()) - .get('/') - .query({ - something: { - nested: true - } - }) - .expect(200) - .expect({ - 'something[nested]': 'true' - }); - }); - }); - - describe('when options.qs = true', () => { - test('should support nested query strings', () => { - const app = koala({ - qs: true - }); - app.use(function * (next) { - this.response.body = this.request.query; - }); - - return request(app.callback()) - .get('/') - .query({ - something: { - nested: true - } - }) - .expect(200) - .expect({ - something: { - nested: 'true' - } - }); - }); - }); -}); diff --git a/lib/__tests__/basic-auth.js b/lib/__tests__/request.js similarity index 100% rename from lib/__tests__/basic-auth.js rename to lib/__tests__/request.js diff --git a/lib/__tests__/cache-control.js b/lib/__tests__/response.js similarity index 100% rename from lib/__tests__/cache-control.js rename to lib/__tests__/response.js diff --git a/lib/__tests__/conditional-get.js b/lib/middleware/__tests__/conditional-get.js similarity index 95% rename from lib/__tests__/conditional-get.js rename to lib/middleware/__tests__/conditional-get.js index f1e9744..c5652fe 100644 --- a/lib/__tests__/conditional-get.js +++ b/lib/middleware/__tests__/conditional-get.js @@ -1,4 +1,4 @@ -const koala = require('..'); +const koala = require('../..'); const request = require('supertest'); describe('Conditional-Get', () => { diff --git a/lib/__tests__/headers.js b/lib/middleware/__tests__/headers.js similarity index 79% rename from lib/__tests__/headers.js rename to lib/middleware/__tests__/headers.js index d028227..87ab515 100644 --- a/lib/__tests__/headers.js +++ b/lib/middleware/__tests__/headers.js @@ -1,33 +1,7 @@ -const koala = require('..'); +const koala = require('../..'); const request = require('supertest'); describe('Set headers', () => { - describe('X-Response-Time', () => { - test('should get X-Response-Time correctly by default', () => { - const app = koala(); - - return request(app.callback()) - .get('/') - .expect(404) - .expect('X-Response-Time', /s$/); - }); - test( - 'should not get X-Response-Time by options.responseTime = false', - () => { - const app = koala({ - responseTime: false - }); - - return request(app.callback()) - .get('/') - .expect(404) - .expect(res => { - expect(res.headers['X-Response-Time']).toBe(undefined); - }); - } - ); - }); - describe('Strict-Transport-Security', () => { test('should not set Strict-Transport-Security by default', () => { const app = koala(); diff --git a/lib/middleware/__tests__/index.js b/lib/middleware/__tests__/index.js new file mode 100644 index 0000000..272a50b --- /dev/null +++ b/lib/middleware/__tests__/index.js @@ -0,0 +1,88 @@ +const koala = require('../..'); +const request = require('supertest'); +const PassThrough = require('stream').PassThrough; + +describe('Middlewares', () => { + describe('Response Time', () => { + test('should get X-Response-Time correctly by default', () => { + const app = koala(); + + return request(app.callback()) + .get('/') + .expect(404) + .expect('X-Response-Time', /s$/); + }); + test( + 'should not get X-Response-Time by options.responseTime = false', + () => { + const app = koala({ + responseTime: false + }); + + return request(app.callback()) + .get('/') + .expect(404) + .expect(res => { + expect(res.headers['X-Response-Time']).toBe(undefined); + }); + } + ); + }); + + describe('Object Streams', () => { + test('should be supported', () => { + const app = koala(); + app.use(function * (next) { + const body = this.body = new PassThrough({ + objectMode: true + }); + + body.write({ + message: 'a' + }); + + body.write({ + message: 'b' + }); + + body.end(); + }); + + return request(app.callback()) + .get('/') + .expect(200) + .expect([{ + message: 'a' + }, { + message: 'b' + }]); + }); + }); + + describe('Session', () => { + test('should has this.session by default', () => { + const app = koala(); + + app.use(function * () { + this.body = this.session; + }); + + return request(app.callback()) + .get('/') + .expect('{}'); + }); + test('should has no this.session by options.session = false', () => { + const app = koala({ + session: false + }); + + app.use(function * () { + this.body = this.session === undefined; + }); + + return request(app.callback()) + .get('/') + .expect('true'); + }); + }); +}); diff --git a/lib/middleware/__tests__/middleware.js b/lib/middleware/__tests__/middleware.js deleted file mode 100644 index 37176c9..0000000 --- a/lib/middleware/__tests__/middleware.js +++ /dev/null @@ -1,31 +0,0 @@ -const koala = require('../..'); -const request = require('supertest'); - -describe('Middlewares', () => { - describe('Session', () => { - test('should has this.session by default', () => { - const app = koala(); - - app.use(function * () { - this.body = this.session; - }); - - return request(app.callback()) - .get('/') - .expect('{}'); - }); - test('should has no this.session by options.session = false', () => { - const app = koala({ - session: false - }); - - app.use(function * () { - this.body = this.session === undefined; - }); - - return request(app.callback()) - .get('/') - .expect('true'); - }); - }); -}); From ee01a27210367ba8b1ad5a4d9d006d4aa2494e52 Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Mon, 18 Sep 2017 11:01:50 -0400 Subject: [PATCH 13/13] Add basic test for app.listen() --- lib/__tests__/app.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/__tests__/app.js diff --git a/lib/__tests__/app.js b/lib/__tests__/app.js new file mode 100644 index 0000000..5cebed1 --- /dev/null +++ b/lib/__tests__/app.js @@ -0,0 +1,15 @@ +const koala = require('..'); +const request = require('supertest'); + +describe('app', () => { + test('listen()', () => { + const app = koala(); + app.use(function * () { + this.body = 'Hello World'; + }); + return request(app.listen().close()) + .get('/') + .expect(200) + .expect('Hello World'); + }); +});