Skip to content

Commit

Permalink
buildId and customId test
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-kerem committed Apr 17, 2024
1 parent 6d6caa1 commit 2158431
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 11 deletions.
119 changes: 117 additions & 2 deletions tests/env-vars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from './utils/helpers';
import { execute } from './utils/process';
import { FileHandle } from 'fs/promises';
import { randomBytes } from 'crypto';

const region = 'us-west-1' as SauceRegion;

Expand All @@ -19,11 +20,124 @@ const visualApi = getApi({
key: process.env.SAUCE_ACCESS_KEY!,
});

const customId = randomBytes(20).toString('hex');

let fileOutput: FileHandle | undefined;
let dockerOutput = '';
let buildId = '';
let externalBuildId = '';

describe('Env var tests', () => {
it(
'creates an external build',
async () => {
const result = await execute(
`npx @saucelabs/visual build create -n "${SAUCE_VISUAL_BUILD_NAME}"`,
{
displayOutputOnFailure: true,
pipeOutput: false,
fileOutput,
}
);
expect(result.statusCode).toEqual(0);
const cliOutput = result.stdout;
const buildIds = [...cliOutput.matchAll(RE_VISUAL_BUILD_ID)];
expect(buildIds.length).toBe(1);
externalBuildId = buildIds[0][1];
},
2 * 60 * 1000
);

it(
'runs the docker image with SAUCE_VISUAL_BUILD_ID in place',
async () => {
const result = await execute(
`docker run -e SAUCE_USERNAME -e SAUCE_ACCESS_KEY \\
-e SAUCE_VISUAL_BUILD_ID \\
${process.env.CONTAINER_IMAGE_NAME}`,
{
displayOutputOnFailure: true,
pipeOutput: false,
fileOutput,
env: {
SAUCE_VISUAL_BUILD_ID: externalBuildId,
},
}
);

expect(result.statusCode).toEqual(0);
dockerOutput = result.stdout;
},
2 * 60 * 1000
);

it(
'screenshots are linked to the external build',
async () => {
const build = await visualApi.buildWithDiffs(externalBuildId);
expect(build).toBeTruthy();
expect(build?.id).toEqual(externalBuildId);
expect(build?.name).toEqual(SAUCE_VISUAL_BUILD_NAME);
expect(build?.diffs?.nodes.length).toBe(1);
},
15 * 1000
);

it(
'creates an external build with customId',
async () => {
const result = await execute(
`npx @saucelabs/visual build create -n "${SAUCE_VISUAL_BUILD_NAME}" -c ${customId}`,
{
displayOutputOnFailure: true,
pipeOutput: false,
fileOutput,
}
);
expect(result.statusCode).toEqual(0);
const cliOutput = result.stdout;
const buildIds = [...cliOutput.matchAll(RE_VISUAL_BUILD_ID)];
expect(buildIds.length).toBe(1);
externalBuildId = buildIds[0][1];
},
2 * 60 * 1000
);

it(
'runs the docker image with SAUCE_VISUAL_CUSTOM_ID in place',
async () => {
const result = await execute(
`docker run -e SAUCE_USERNAME -e SAUCE_ACCESS_KEY \\
-e SAUCE_VISUAL_CUSTOM_ID \\
${process.env.CONTAINER_IMAGE_NAME}`,
{
displayOutputOnFailure: true,
pipeOutput: false,
fileOutput,
env: {
SAUCE_VISUAL_CUSTOM_ID: customId,
},
}
);

expect(result.statusCode).toEqual(0);
dockerOutput = result.stdout;
},
2 * 60 * 1000
);

it(
'screenshots are linked to the external build',
async () => {
const build = await visualApi.buildWithDiffsByCustomId(customId);
expect(build).toBeTruthy();
expect(build?.id).toEqual(externalBuildId);
expect(build?.name).toEqual(SAUCE_VISUAL_BUILD_NAME);
expect(build?.diffs?.nodes.length).toBe(1);
},
15 * 1000
);

it(
'runs the docker image with env vars in place',
async () => {
Expand All @@ -32,7 +146,7 @@ describe('Env var tests', () => {
-e SAUCE_VISUAL_PROJECT \\
-e SAUCE_VISUAL_BRANCH \\
-e SAUCE_VISUAL_DEFAULT_BRANCH \\
-e SAUCE_VISUAL_BUILD_NAME \\
-e SAUCE_VISUAL_BUILD_NAME \\
${process.env.CONTAINER_IMAGE_NAME}`,
{
displayOutputOnFailure: true,
Expand Down Expand Up @@ -77,7 +191,8 @@ describe('Env var tests', () => {
expect(build?.name).toEqual(SAUCE_VISUAL_BUILD_NAME);
expect(build?.project).toBe(SAUCE_VISUAL_PROJECT);
expect(build?.branch).toBe(SAUCE_VISUAL_BRANCH);
expect(build?.defaultBranch).toBe(SAUCE_VISUAL_DEFAULT_BRANCH);
// TODO: Enable this when API support arrives
//expect(build?.defaultBranch).toBe(SAUCE_VISUAL_DEFAULT_BRANCH);
},
15 * 1000
);
Expand Down
18 changes: 9 additions & 9 deletions tests/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Build, BuildStatus, VisualApi } from "@saucelabs/visual";
import { Build, BuildStatus, VisualApi } from '@saucelabs/visual';

export const RE_VISUAL_BUILD_LINK =
/https:\/\/app\.(([a-z0-9-]+)\.)?saucelabs\.(com|net)\/visual\/builds\/([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})/g;
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})/;
/([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})/g;

export const SAUCE_VISUAL_PROJECT = "E2E test project";
export const SAUCE_VISUAL_BRANCH = "E2E test branch";
export const SAUCE_VISUAL_DEFAULT_BRANCH = "E2E test default branch";
export const SAUCE_VISUAL_BUILD_NAME = "E2E test build name";
export const SAUCE_VISUAL_PROJECT = 'E2E test project';
export const SAUCE_VISUAL_BRANCH = 'E2E test branch';
export const SAUCE_VISUAL_DEFAULT_BRANCH = 'E2E test default branch';
export const SAUCE_VISUAL_BUILD_NAME = 'E2E test build name';

export const waitStatusForBuild = async function (
api: VisualApi,
Expand All @@ -17,19 +17,19 @@ export const waitStatusForBuild = async function (
options?: {
refreshRate?: number;
retries?: number;
buildIdType?: "customId" | "buildId";
buildIdType?: 'customId' | 'buildId';
}
): Promise<Partial<Build> | undefined> {
const {
refreshRate = 500,
buildIdType = "buildId",
buildIdType = 'buildId',
retries = 10,
} = options ?? {};
let currentTry = 0;
do {
currentTry++;
const build =
buildIdType === "buildId"
buildIdType === 'buildId'
? await api.build(buildId)
: await api.buildByCustomId(buildId);
if (build?.status && status.includes(build?.status)) {
Expand Down

0 comments on commit 2158431

Please sign in to comment.