From a355664e642cade7d92db56a1fcc28639a11229a Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Wed, 23 Nov 2016 11:30:01 -0800 Subject: [PATCH] Update packages --- .nycrc | 3 --- package.json | 12 ++++++------ test/test.js | 51 +++++++++++++++++++++++---------------------------- 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/.nycrc b/.nycrc index ad1e790..d8d9c14 100644 --- a/.nycrc +++ b/.nycrc @@ -1,7 +1,4 @@ { - "include": [ - "src/*.js" - ], "require": [ "babel-register" ], diff --git a/package.json b/package.json index 2b514ac..3c693ec 100644 --- a/package.json +++ b/package.json @@ -31,26 +31,26 @@ }, "homepage": "https://github.com/bitinn/node-fetch", "devDependencies": { - "babel-plugin-istanbul": "^2.0.1", + "babel-plugin-istanbul": "^3.0.0", "babel-plugin-transform-runtime": "^6.15.0", "babel-preset-es2015": "^6.16.0", "babel-register": "^6.16.3", "bluebird": "^3.3.4", "chai": "^3.5.0", - "chai-as-promised": "^5.2.0", + "chai-as-promised": "^6.0.0", "chai-iterator": "^1.1.1", "codecov": "^1.0.1", - "cross-env": "2.0.1", + "cross-env": "^3.1.3", "form-data": ">=1.0.0", - "mocha": "^2.1.0", - "nyc": "^8.3.0", + "mocha": "^3.1.2", + "nyc": "^10.0.0", "parted": "^0.1.1", "promise": "^7.1.1", "resumer": "0.0.0", "rollup": "^0.36.4", "rollup-plugin-babel": "^2.6.1", "rollup-plugin-node-resolve": "^2.0.0", - "whatwg-url": "^3.0.0" + "whatwg-url": "^4.0.0" }, "dependencies": { "babel-runtime": "^6.11.6", diff --git a/test/test.js b/test/test.js index 5dd5d5b..6ad17df 100644 --- a/test/test.js +++ b/test/test.js @@ -1039,53 +1039,36 @@ describe(`node-fetch with FOLLOW_SPEC = ${defaultFollowSpec}`, () => { }); }); - it('should allow piping response body as stream', function(done) { + it('should allow piping response body as stream', function() { url = `${base}hello`; - fetch(url).then(res => { + return fetch(url).then(res => { expect(res.body).to.be.an.instanceof(stream.Transform); - res.body.on('data', chunk => { + return streamToPromise(res.body, chunk => { if (chunk === null) { return; } expect(chunk.toString()).to.equal('world'); }); - res.body.on('end', () => { - done(); - }); }); }); - it('should allow cloning a response, and use both as stream', function(done) { + it('should allow cloning a response, and use both as stream', function() { url = `${base}hello`; return fetch(url).then(res => { - let counter = 0; const r1 = res.clone(); expect(res.body).to.be.an.instanceof(stream.Transform); expect(r1.body).to.be.an.instanceof(stream.Transform); - res.body.on('data', chunk => { + const dataHandler = chunk => { if (chunk === null) { return; } expect(chunk.toString()).to.equal('world'); - }); - res.body.on('end', () => { - counter++; - if (counter == 2) { - done(); - } - }); - r1.body.on('data', chunk => { - if (chunk === null) { - return; - } - expect(chunk.toString()).to.equal('world'); - }); - r1.body.on('end', () => { - counter++; - if (counter == 2) { - done(); - } - }); + }; + + return Promise.all([ + streamToPromise(res.body, dataHandler), + streamToPromise(r1.body, dataHandler) + ]); }); }); @@ -1698,3 +1681,15 @@ describe(`node-fetch with FOLLOW_SPEC = ${defaultFollowSpec}`, () => { }); }); + +function streamToPromise(stream, dataHandler) { + return new Promise((resolve, reject) => { + stream.on('data', (...args) => { + Promise.resolve() + .then(() => dataHandler(...args)) + .catch(reject); + }); + stream.on('end', resolve); + stream.on('error', reject); + }); +}