From cc1d41f5e34348a2de7bff52b6f46916c4124b28 Mon Sep 17 00:00:00 2001 From: Slobodan Stojanovic Date: Sat, 24 Oct 2015 18:24:38 +0200 Subject: [PATCH] Refactor tests - add arrow functions Refactor tests - move .should to the same line as arrow function --- test/application/onerror.js | 20 +++++--------------- test/application/respond.js | 8 ++------ test/context/cookies.js | 13 ++++--------- test/helpers/context.js | 14 +++++--------- test/request/href.js | 2 +- 5 files changed, 17 insertions(+), 40 deletions(-) diff --git a/test/application/onerror.js b/test/application/onerror.js index bb47026..3d2b0ac 100644 --- a/test/application/onerror.js +++ b/test/application/onerror.js @@ -9,9 +9,7 @@ describe('app.onerror(err)', function(){ it('should throw an error if a non-error is given', function(done){ const app = new Koa(); - (function(){ - app.onerror('foo'); - }).should.throw(AssertionError, {message: 'non-error thrown: foo'}); + (() => app.onerror('foo')).should.throw(AssertionError, {message: 'non-error thrown: foo'}); done(); }); @@ -22,9 +20,7 @@ describe('app.onerror(err)', function(){ err.status = 404; - const output = stderr.inspectSync(function(){ - app.onerror(err); - }); + const output = stderr.inspectSync(() => app.onerror(err)); output.should.eql([]); @@ -36,9 +32,7 @@ describe('app.onerror(err)', function(){ app.silent = true; const err = new Error(); - const output = stderr.inspectSync(function(){ - app.onerror(err); - }); + const output = stderr.inspectSync(() => app.onerror(err)); output.should.eql([]); @@ -52,9 +46,7 @@ describe('app.onerror(err)', function(){ const err = new Error(); err.stack = 'Foo'; - const output = stderr.inspectSync(function(){ - app.onerror(err); - }); + const output = stderr.inspectSync(() => app.onerror(err)); output.should.eql(['\n', ' Foo\n', '\n']); @@ -68,9 +60,7 @@ describe('app.onerror(err)', function(){ const err = new Error('mock stack null'); err.stack = null; - const output = stderr.inspectSync(function(){ - app.onerror(err); - }); + const output = stderr.inspectSync(() => app.onerror(err)); output.should.eql(['\n', ' Error: mock stack null\n', '\n']); diff --git a/test/application/respond.js b/test/application/respond.js index 42779a1..982367b 100644 --- a/test/application/respond.js +++ b/test/application/respond.js @@ -207,16 +207,12 @@ describe('app.respond', function(){ ctx.status = 200; res.setHeader('Content-Type', 'text/html'); res.write('Hello'); - setTimeout(function(){ - res.end('Goodbye'); - }, 0); + setTimeout(() => res.end('Goodbye'), 0); }); let errorCaught = false; - app.on('error', function(err){ - errorCaught = err; - }); + app.on('error', err => errorCaught = err); const server = app.listen(); diff --git a/test/context/cookies.js b/test/context/cookies.js index a3661fa..38957f0 100644 --- a/test/context/cookies.js +++ b/test/context/cookies.js @@ -21,9 +21,8 @@ describe('ctx.cookies.set()', function(){ .end(function(err, res){ if (err) return done(err); - res.headers['set-cookie'].some(function(cookie){ - return /^name=/.test(cookie); - }).should.be.ok; + res.headers['set-cookie'].some(cookie => /^name=/.test(cookie)) + .should.be.ok; done(); }); @@ -68,13 +67,9 @@ describe('ctx.cookies.set()', function(){ const cookies = res.headers['set-cookie']; - cookies.some(function(cookie){ - return /^name=/.test(cookie); - }).should.be.ok; + cookies.some(cookie => /^name=/.test(cookie)).should.be.ok; - cookies.some(function(cookie){ - return /^name\.sig=/.test(cookie); - }).should.be.ok; + cookies.some(cookie => /^name\.sig=/.test(cookie)).should.be.ok; done(); }); diff --git a/test/helpers/context.js b/test/helpers/context.js index 5b1e10f..7ef180b 100644 --- a/test/helpers/context.js +++ b/test/helpers/context.js @@ -8,16 +8,12 @@ module.exports = function(req, res){ const socket = new Stream.Duplex(); req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype }; res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype }; - res.getHeader = function(k){ return res._headers[k.toLowerCase()]; }; - res.setHeader = function(k, v){ res._headers[k.toLowerCase()] = v; }; - res.removeHeader = function(k, v){ delete res._headers[k.toLowerCase()]; }; + res.getHeader = k => res._headers[k.toLowerCase()]; + res.setHeader = (k, v) => res._headers[k.toLowerCase()] = v; + res.removeHeader = (k, v) => delete res._headers[k.toLowerCase()]; return (new Koa()).createContext(req, res); }; -module.exports.request = function(req, res){ - return module.exports(req, res).request; -}; +module.exports.request = (req, res) => module.exports(req, res).request; -module.exports.response = function(req, res){ - return module.exports(req, res).response; -}; +module.exports.response = (req, res) => module.exports(req, res).response; diff --git a/test/request/href.js b/test/request/href.js index b9d018d..d5524a7 100644 --- a/test/request/href.js +++ b/test/request/href.js @@ -39,7 +39,7 @@ describe('ctx.href', function(){ res.statusCode.should.equal(200); let buf = ''; res.setEncoding('utf8'); - res.on('data', function(s){ buf += s; }); + res.on('data', s => buf += s); res.on('end', function(){ buf.should.equal('http://example.com/foo'); done();