Skip to content

Commit

Permalink
chore(typescript): switching codebase to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jtassin committed Mar 26, 2019
1 parent b811de7 commit 09ce5a7
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 27 deletions.
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverageFrom: [
'src/*.{js,jsx}',
'!**/node_modules/**',
'!**/vendor/**',
],
};
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "1.0.17",
"description": "Small tool for wait that all xhr are finished in pupeteer",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"homepage": "https://github.com/jtassin/pending-xhr-puppeteer",
"repository": {
"type": "git",
Expand All @@ -24,22 +25,22 @@
"e2e"
],
"devDependencies": {
"@types/jest": "^24.0.11",
"@types/puppeteer": "^1.12.3",
"eslint": "5.15.3",
"eslint-config-prettier": "3.6.0",
"eslint-plugin-jest": "22.4.1",
"husky": "1.3.1",
"jest": "24.5.0",
"jest": "^24.5.0",
"lint-staged": "8.1.0",
"prettier": "1.15.3",
"puppeteer": "1.13.0",
"rollup": "1.0.0"
},
"jest": {
"collectCoverageFrom": [
"src/*.{js,jsx}",
"!**/node_modules/**",
"!**/vendor/**"
]
"puppeteer": "1.11.0",
"rollup": "1.0.0",
"rollup-plugin-typescript": "^1.0.0",
"ts-jest": "^24.0.0",
"tsc": "^1.20150623.0",
"tslib": "^1.9.3",
"typescript": "^3.3.4000"
},
"scripts": {
"prettier:write": "prettier --single-quote --trailing-comma es5 --write src/**/*",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('PendingXHR', () => {
});
});

describe('waitForAllXhrFinished', async () => {
describe('waitForAllXhrFinished', () => {
it('returns immediatly if no xhr pending count', async () => {
await startServerReturning(OK_NO_XHR);
const page = await browser.newPage();
Expand Down
26 changes: 20 additions & 6 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { Request, Page } from 'puppeteer';

interface ResolvableRequest extends Request {
resolver: () => void;
}

class PendingXHR {
constructor(page) {

page: Page;
resourceType: string;
pendingXhrs: Set<Request>;
finishedWithSuccessXhrs: Set<Request>;
finishedWithErrorsXhrs: Set<Request>;
promisees: Array<Promise<void>>

constructor(page: Page) {
this.promisees = [];
this.page = page;
this.resourceType = 'xhr';
// page.setRequestInterception(true);
this.pendingXhrs = new Set();
this.finishedWithSuccessXhrs = new Set();
this.finishedWithErrorsXhrs = new Set();
this.promisees = [];
page.on('request', request => {
page.on('request', (request: ResolvableRequest) => {
if (request.resourceType() === this.resourceType) {
this.pendingXhrs.add(request);
this.promisees.push(
Expand All @@ -17,7 +31,7 @@ class PendingXHR {
);
}
});
page.on('requestfailed', request => {
page.on('requestfailed', (request: ResolvableRequest) => {
if (request.resourceType() === this.resourceType) {
this.pendingXhrs.delete(request);
this.finishedWithErrorsXhrs.add(request);
Expand All @@ -27,7 +41,7 @@ class PendingXHR {
}
}
});
page.on('requestfinished', request => {
page.on('requestfinished', (request: ResolvableRequest) => {
if (request.resourceType() === this.resourceType) {
this.pendingXhrs.delete(request);
this.finishedWithSuccessXhrs.add(request);
Expand All @@ -41,7 +55,7 @@ class PendingXHR {

async waitForAllXhrFinished() {
if (this.pendingXhrCount() === 0) {
return true;
return;
}
await Promise.all(this.promisees);
}
Expand Down
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"outDir": "build",
"module": "commonjs",
"target": "es5",
"lib": ["es6"],
"sourceMap": true,
"allowJs": true,
"moduleResolution": "node",
"rootDir": "src",
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true
},
"exclude": ["node_modules", "build"],
"types": ["typePatches"]
}
Loading

0 comments on commit 09ce5a7

Please sign in to comment.