diff --git a/package.json b/package.json index 2d91f63e..93e81d74 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "private": false, "devDependencies": { "eslint": "^4.19.1", + "eslint-config-prettier": "^2.9.0", "husky": "^0.14.3", "jest": "^22.4.3", "lint-staged": "^7.1.0", @@ -16,7 +17,9 @@ "puppeteer": "^1.3.0" }, "scripts": { - "lint": "eslint", + "prettier:write": + "prettier --single-quote --trailing-comma es5 --write src/**/*", + "lint": "eslint src", "test": "jest", "precommit": "lint-staged" }, diff --git a/src/__tests__/index.test.js b/src/__tests__/index.test.js index 79653062..f879a07c 100644 --- a/src/__tests__/index.test.js +++ b/src/__tests__/index.test.js @@ -1,9 +1,9 @@ -const PendingXHR = require('../index').PendingXHR; -const http = require('http'); -const puppeteer = require('puppeteer'); +const PendingXHR = require("../index").PendingXHR; +const http = require("http"); +const puppeteer = require("puppeteer"); const port = 8907; const xhrBackendPort = 8908; -const util = require('util'); +const util = require("util"); // const sleep = util.promisify(setTimeout); function timeout(ms) { @@ -14,7 +14,6 @@ async function sleep(delay) { return; } - const OK_NO_XHR = ` <html> OK_NO_XHR @@ -66,13 +65,13 @@ setTimeout(() => { `; const requestHandler = (request, response) => { - if (request.url === '/with_infinite_xhr') { + if (request.url === "/with_infinite_xhr") { response.statusCode = 200; response.end(OK_WITH_1_SLOW_XHR); - } else if (request.url === '/with_xhr') { + } else if (request.url === "/with_xhr") { response.statusCode = 200; response.end(OK_WITH_1_XHR); - } else if (request.url === '/with_xhr_failing') { + } else if (request.url === "/with_xhr_failing") { response.statusCode = 200; response.end(OK_WITH_1_XHR); } else { @@ -82,15 +81,15 @@ const requestHandler = (request, response) => { }; const backendRequestHandler = (request, response) => { - if (request.url === '/infinite') { + if (request.url === "/infinite") { // This xhr will never end - } else if (request.url === '/fail') { + } else if (request.url === "/fail") { response.statusCode = 500; - response.end('boom'); + response.end("boom"); } else { response.end(); } -} +}; let server; let backendServer; @@ -115,17 +114,16 @@ afterEach(async () => { await backendServer.close(); }); -describe('PendingXHR', () => { - - describe('pendingXhrCount', () => { - it('returns 0 if no xhr pending count', async () => { +describe("PendingXHR", () => { + describe("pendingXhrCount", () => { + it("returns 0 if no xhr pending count", async () => { const page = await browser.newPage(); const pendingXHR = new PendingXHR(page); await page.goto(`http://localhost:${port}/no_xhr`); expect(pendingXHR.pendingXhrCount()).toEqual(0); }); - it('returns the xhr pending count', async () => { + it("returns the xhr pending count", async () => { const page = await browser.newPage(); const pendingXHR = new PendingXHR(page); await page.goto(`http://localhost:${port}/with_infinite_xhr`); @@ -134,15 +132,15 @@ describe('PendingXHR', () => { }); }); - describe('waitForAllXhrFinished', async () => { - it('returns immediatly if no xhr pending count', async () => { + describe("waitForAllXhrFinished", async () => { + it("returns immediatly if no xhr pending count", async () => { const page = await browser.newPage(); const pendingXHR = new PendingXHR(page); await page.goto(`http://localhost:${port}/no_xhr`); await pendingXHR.waitForAllXhrFinished(); }); - it('waits for all xhr to end', async () => { + it("waits for all xhr to end", async () => { const page = await browser.newPage(); const pendingXHR = new PendingXHR(page); await page.goto(`http://localhost:${port}/with_xhr`); @@ -151,7 +149,7 @@ describe('PendingXHR', () => { expect(pendingXHR.pendingXhrCount()).toEqual(0); }); - it('handle correctly failed xhr', async () => { + it("handle correctly failed xhr", async () => { const page = await browser.newPage(); const pendingXHR = new PendingXHR(page); await page.goto(`http://localhost:${port}/with_xhr_failing`); @@ -160,4 +158,4 @@ describe('PendingXHR', () => { expect(pendingXHR.pendingXhrCount()).toEqual(0); }); }); -}); \ No newline at end of file +}); diff --git a/src/index.js b/src/index.js index 21cfed3e..143a080f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,44 +1,55 @@ class PendingXHR { - constructor(page) { this.page = page; - this.resourceType = 'xhr'; + this.resourceType = "xhr"; // page.setRequestInterception(true); this.pendingXhrs = new Set(); this.finishedWithSuccessXhrs = new Set(); this.finishedWithErrorsXhrs = new Set(); - page.on('request', request => { - console.log(Date.now(), 'new request', request.resourceType(), ' to ', request.url()); + page.on("request", request => { + console.log( + Date.now(), + "new request", + request.resourceType(), + " to ", + request.url() + ); if (request.resourceType() === this.resourceType) { this.pendingXhrs.add(request); - if(!this.promise) { + if (!this.promise) { this.promise = new Promise((resolve, reject) => { this.resolver = resolve; }); this.promise.then(() => { - console.log('All xhr are ended'); + console.log("All xhr are ended"); }); - } + } } }); - page.on('requestfailed', request => { + page.on("requestfailed", request => { if (request.resourceType() === this.resourceType) { console.log(this.pendingXhrs); this.pendingXhrs.delete(request); this.finishedWithErrorsXhrs.add(request); - if(this.resolver) { + if (this.resolver) { this.resolver(); delete this.promise; delete this.resolver; } } }); - page.on('requestfinished', request => { - console.log(Date.now(), 'request finished', request.resourceType(), ' to ', request.url()); + page.on("requestfinished", request => { + console.log( + Date.now(), + "request finished", + request.resourceType(), + " to ", + request.url() + ); if (request.resourceType() === this.resourceType) { this.pendingXhrs.delete(request); this.finishedWithSuccessXhrs.add(request); - if(this.resolver) { + if (this.resolver) { this.resolver(); delete this.promise; delete this.resolver; @@ -51,8 +62,8 @@ class PendingXHR { if (this.pendingXhrCount() === 0) { return true; } - if(this.promise) { - console.log('promise exists', this.promise); + if (this.promise) { + console.log("promise exists", this.promise); await this.promise; } }