From 7ba71f762768a104efd16a87c90b800b202de881 Mon Sep 17 00:00:00 2001 From: Kerem Date: Wed, 17 Apr 2024 12:48:44 +0200 Subject: [PATCH] try building test image --- tests/jest.config.cjs | 10 ++++++++++ tests/utils/helpers.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/utils/runner.ts | 12 ++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tests/jest.config.cjs create mode 100644 tests/utils/helpers.ts create mode 100644 tests/utils/runner.ts diff --git a/tests/jest.config.cjs b/tests/jest.config.cjs new file mode 100644 index 00000000..9d823b6c --- /dev/null +++ b/tests/jest.config.cjs @@ -0,0 +1,10 @@ +/** @type {import('ts-jest').JestConfigWithTsJest} */ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', + testMatch: ['**/tests/*.spec.ts'], + testPathIgnorePatterns: ['/project/', '/node_modules/'], + runner: './utils/runner.ts', + setupFilesAfterEnv: ['jest-extended/all'], + bail: 1, +}; diff --git a/tests/utils/helpers.ts b/tests/utils/helpers.ts new file mode 100644 index 00000000..48134644 --- /dev/null +++ b/tests/utils/helpers.ts @@ -0,0 +1,42 @@ +import { Build, BuildStatus, VisualApi } from "@saucelabs/visual"; + +export const RE_VISUAL_BUILD_ID = + /([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})/; + +export const waitStatusForBuild = async function ( + api: VisualApi, + buildId: string, + status: BuildStatus[], + options?: { + refreshRate?: number; + retries?: number; + buildIdType?: 'customId' | 'buildId'; + }, +): Promise | undefined> { + const { + refreshRate = 500, + buildIdType = 'buildId', + retries = 10, + } = options ?? {}; + let currentTry = 0; + do { + currentTry++; + const build = + buildIdType === 'buildId' + ? await api.build(buildId) + : await api.buildByCustomId(buildId); + if (build?.status && status.includes(build?.status)) { + return build; + } + await wait(refreshRate); + } while (currentTry < retries); + throw new Error( + `Expected status ${status} never received for build ${buildId}`, + ); +}; + +async function wait(delayMs: number): Promise { + return new Promise((resolve) => { + setTimeout(resolve, delayMs); + }); +} \ No newline at end of file diff --git a/tests/utils/runner.ts b/tests/utils/runner.ts new file mode 100644 index 00000000..117d90a3 --- /dev/null +++ b/tests/utils/runner.ts @@ -0,0 +1,12 @@ +import DefaultJestRunner, { Config, TestRunnerContext } from 'jest-runner'; + +class SerialJestRunner extends DefaultJestRunner { + declare isSerial?: boolean; + + constructor(_globalConfig: Config.GlobalConfig, _context: TestRunnerContext) { + super(_globalConfig, _context); + this.isSerial = true; + } +} + +module.exports = SerialJestRunner;