From ba28482ccc80f04d2a43ccb71b24d45f9dbb7287 Mon Sep 17 00:00:00 2001 From: diosmosis Date: Tue, 1 Oct 2024 20:44:59 -0700 Subject: [PATCH] try waiting until requests to the tunnel work --- jest.config.appscript.ts | 2 +- tests/appscript/api.spec.ts | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/jest.config.appscript.ts b/jest.config.appscript.ts index a1700cd..f30401c 100644 --- a/jest.config.appscript.ts +++ b/jest.config.appscript.ts @@ -9,7 +9,7 @@ export default { rootDir: 'tests/appscript', preset: 'ts-jest/presets/js-with-ts-esm', testEnvironment: 'node', - testTimeout: 180000, + testTimeout: 300000, globalSetup: '../globalSetup.js', globalTeardown: '../globalTeardown.js', maxWorkers: 1, // since we run our tests within apps script, we can't run them in parallel diff --git a/tests/appscript/api.spec.ts b/tests/appscript/api.spec.ts index ccb6976..188ada5 100644 --- a/tests/appscript/api.spec.ts +++ b/tests/appscript/api.spec.ts @@ -8,6 +8,7 @@ import { beforeEach, beforeAll, expect } from '@jest/globals'; import Clasp from '../utilities/clasp'; import { makeApiFailureMockServer } from './api/mockServer'; +import axios from 'axios'; describe('api', () => { let server: ReturnType; @@ -47,6 +48,26 @@ describe('api', () => { }); } + async function waitForMockServer() { + const testUrl = `${tunnel.url}/index.php`; + const body = { + method: 'SitesManager.getSitesIdWithAtLeastViewAccess', + }; + + while (true) { + await new Promise((resolve) => setTimeout(resolve, 2000)); + + try { + const response = await axios.post(testUrl, body); + if (Array.isArray(response.data) && response.data[0] === 1) { + return; + } + } catch (e) { + // retry + } + } + } + describe('extractBasicAuthFromUrl()', () => { const URLS_WITHOUT_CREDENTIALS = [ 'http://mymatomo.com/', @@ -129,7 +150,7 @@ describe('api', () => { }, }); - await new Promise((resolve) => setTimeout(resolve, 10000)); + await waitForMockServer(); // use the mock server's path that forces a random error const result = await Clasp.run('setCredentials', { @@ -175,7 +196,7 @@ describe('api', () => { }); // waiting seems to be required here, or the mock server isn't used in time - await new Promise((resolve) => setTimeout(resolve, 10000)); + await waitForMockServer(); // use the mock server's path that forces a non-random error await Clasp.run('setCredentials', { @@ -218,7 +239,7 @@ describe('api', () => { }, }); - await new Promise((resolve) => setTimeout(resolve, 10000)); + await waitForMockServer(); // use the mock server's path that forces a non-random error await Clasp.run('setCredentials', {