From 8ddc74121e6699ddfcbf927f000769715f49cac5 Mon Sep 17 00:00:00 2001 From: Hendrik Eeckhaut Date: Fri, 23 Feb 2024 14:52:07 +0100 Subject: [PATCH] Set timeout on check --- package.json | 1 + test/testRunner.ts | 44 ++++++++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 9a77ba5..0bf8d16 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "@types/mocha": "^10.0.6", "@types/serve-handler": "^6.1.4", "@typescript-eslint/eslint-plugin": "^6.15.0", + "@typescript-eslint/parser": "7.0.2", "browserify": "^17.0.0", "concurrently": "^5.1.0", "constants-browserify": "^1.0.0", diff --git a/test/testRunner.ts b/test/testRunner.ts index 880f9f0..36540bd 100644 --- a/test/testRunner.ts +++ b/test/testRunner.ts @@ -1,26 +1,29 @@ -import puppeteer, { PuppeteerLaunchOptions } from 'puppeteer'; +import puppeteer, { Browser, Page, PuppeteerLaunchOptions } from 'puppeteer'; import { describe, it, before, after } from 'mocha'; const assert = require('assert'); -const { exec } = require('node:child_process'); +import { exec, ChildProcess } from 'node:child_process'; + +const timeout = 60000; // puppeteer options let opts: PuppeteerLaunchOptions = { - headless: !!process.env.HEADLESS ? "new" : false, + headless: !!process.env.HEADLESS ? 'new' : false, slowMo: 100, - timeout: 60000, + timeout: timeout, }; if (process.env.CHROME_PATH) { opts = { ...opts, - executablePath: process.env.CHROME_PATH - } -}; - + executablePath: process.env.CHROME_PATH, + }; +} -console.log("puppeteer options", opts) +console.log('puppeteer options', opts); -let browser: any, page: any, server: any; +let browser: Browser; +let page: Page; +let server: ChildProcess; // expose variables before(async function () { @@ -62,10 +65,23 @@ describe('tlsn-js test suite', function () { }); async function check(testId: string): Promise { - const content = await page.$eval('#' + testId, (n: any) => n.innerText); - if (content) return content; - await new Promise((r) => setTimeout(r, 1000)); - return check(testId); + const startTime = Date.now(); + const attemptFetchContent = async (): Promise => { + const content = await page.$eval( + `#${testId}`, + (el: Element) => el.textContent || '', + ); + if (content) return content; + const elapsedTime = Date.now() - startTime; + if (elapsedTime >= timeout) { + throw new Error( + `Timeout: Failed to retrieve content for '#${testId}' within ${timeout} ms.`, + ); + } + await new Promise((resolve) => setTimeout(resolve, 1000)); + return attemptFetchContent(); + }; + return attemptFetchContent(); } function safeParseJson(data: string): any | null {