Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript #21

Merged
merged 2 commits into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.rpt2_cache

# Runtime data
pids
Expand Down
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/**',
],
};
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"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",
"module": "lib/es.js",
"homepage": "https://github.com/jtassin/pending-xhr-puppeteer",
"repository": {
"type": "git",
Expand All @@ -24,22 +26,23 @@
"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",
"rollup-plugin-typescript2": "^0.20.1",
"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
20 changes: 15 additions & 5 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import typescript from 'rollup-plugin-typescript2';
import pkg from './package.json';

module.exports = {
input: 'src/index.js',
output: {
file: 'lib/index.js',
format: 'cjs',
},
input: 'src/index.ts',
plugins: [typescript({ typescript: require('typescript') })],
output: [
{
file: pkg.main,
format: 'cjs',
},
{
file: pkg.module,
format: 'es',
},
],
};
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
30 changes: 21 additions & 9 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
class PendingXHR {
constructor(page) {
import { Request, Page } from 'puppeteer';

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

export class PendingXHR {

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 All @@ -50,5 +64,3 @@ class PendingXHR {
return this.pendingXhrs.size;
}
}

module.exports.PendingXHR = PendingXHR;
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"declaration": true,
"declarationDir": "./dist",
"outDir": "build",
"module": "ES2015",
"target": "es5",
"lib": ["ES2015"],
"sourceMap": true,
"moduleResolution": "node",
"rootDir": "src",
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true
},
"exclude": ["node_modules", "build"],
"types": ["typePatches"]
}
Loading