Skip to content

Commit

Permalink
Merge pull request #801 from chromaui/fix-wait-for-completed-build
Browse files Browse the repository at this point in the history
Fix `Unexpected build status: PREPARED` error
  • Loading branch information
ghengeveld committed Aug 24, 2023
2 parents 16a7e45 + 22ed4f8 commit 0ef41ab
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion node-src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ jest.mock('node-fetch', () =>
if (query.match('SnapshotBuildQuery')) {
return {
data: {
app: { build: { status: 'PENDING', changeCount: 1 } },
app: { build: { status: 'PENDING', changeCount: 1, completedAt: 1 } },
},
};
}
Expand Down
16 changes: 8 additions & 8 deletions node-src/tasks/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('takeSnapshots', () => {

client.runQuery.mockReturnValueOnce({ app: { build: { status: 'IN_PROGRESS' } } });
client.runQuery.mockReturnValueOnce({
app: { build: { changeCount: 0, status: 'PASSED' } },
app: { build: { changeCount: 0, status: 'PASSED', completedAt: 1 } },
});

await takeSnapshots(ctx, {} as any);
Expand All @@ -34,7 +34,7 @@ describe('takeSnapshots', () => {
{ headers: { Authorization: `Bearer report-token` } }
);
expect(client.runQuery).toHaveBeenCalledTimes(2);
expect(ctx.build).toEqual({ ...build, changeCount: 0, status: 'PASSED' });
expect(ctx.build).toEqual({ ...build, changeCount: 0, status: 'PASSED', completedAt: 1 });
expect(ctx.exitCode).toBe(0);
});

Expand All @@ -53,11 +53,11 @@ describe('takeSnapshots', () => {

client.runQuery.mockReturnValueOnce({ app: { build: { status: 'IN_PROGRESS' } } });
client.runQuery.mockReturnValueOnce({
app: { build: { changeCount: 2, status: 'PENDING' } },
app: { build: { changeCount: 2, status: 'PENDING', completedAt: 1 } },
});

await takeSnapshots(ctx, {} as any);
expect(ctx.build).toEqual({ ...build, changeCount: 2, status: 'PENDING' });
expect(ctx.build).toEqual({ ...build, changeCount: 2, status: 'PENDING', completedAt: 1 });
expect(ctx.exitCode).toBe(1);
});

Expand All @@ -76,11 +76,11 @@ describe('takeSnapshots', () => {

client.runQuery.mockReturnValueOnce({ app: { build: { status: 'IN_PROGRESS' } } });
client.runQuery.mockReturnValueOnce({
app: { build: { changeCount: 2, status: 'BROKEN' } },
app: { build: { changeCount: 2, status: 'BROKEN', completedAt: 1 } },
});

await takeSnapshots(ctx, {} as any);
expect(ctx.build).toEqual({ ...build, changeCount: 2, status: 'BROKEN' });
expect(ctx.build).toEqual({ ...build, changeCount: 2, status: 'BROKEN', completedAt: 1 });
expect(ctx.exitCode).toBe(2);
});

Expand All @@ -99,11 +99,11 @@ describe('takeSnapshots', () => {

client.runQuery.mockReturnValueOnce({ app: { build: { status: 'IN_PROGRESS' } } });
client.runQuery.mockReturnValueOnce({
app: { build: { changeCount: 2, status: 'FAILED' } },
app: { build: { changeCount: 2, status: 'FAILED', completedAt: 1 } },
});

await takeSnapshots(ctx, {} as any);
expect(ctx.build).toEqual({ ...build, changeCount: 2, status: 'FAILED' });
expect(ctx.build).toEqual({ ...build, changeCount: 2, status: 'FAILED', completedAt: 1 });
expect(ctx.exitCode).toBe(3);
});
});
10 changes: 6 additions & 4 deletions node-src/tasks/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const SnapshotBuildQuery = `
testCount
changeCount
errorCount: testCount(statuses: [BROKEN])
completedAt
}
}
}
Expand All @@ -44,6 +45,7 @@ interface BuildQueryResult {
testCount: number;
changeCount: number;
errorCount: number;
completedAt?: number;
};
};
}
Expand Down Expand Up @@ -72,12 +74,12 @@ export const takeSnapshots = async (ctx: Context, task: Task) => {
ctx.options.interactive ? ctx.env.CHROMATIC_POLL_INTERVAL : ctx.env.CHROMATIC_OUTPUT_INTERVAL
);

const waitForBuild = async (): Promise<Context['build']> => {
const waitForBuildToComplete = async (): Promise<Context['build']> => {
const options = { headers: { Authorization: `Bearer ${reportToken}` } };
const data = await client.runQuery<BuildQueryResult>(SnapshotBuildQuery, { number }, options);
ctx.build = { ...ctx.build, ...data.app.build };

if (ctx.build.status !== 'IN_PROGRESS') {
if (ctx.build.completedAt) {
return ctx.build;
}

Expand All @@ -89,10 +91,10 @@ export const takeSnapshots = async (ctx: Context, task: Task) => {
}

await delay(ctx.env.CHROMATIC_POLL_INTERVAL);
return waitForBuild();
return waitForBuildToComplete();
};

const build = await waitForBuild();
const build = await waitForBuildToComplete();

switch (build.status) {
case 'PASSED':
Expand Down
1 change: 1 addition & 0 deletions node-src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export interface Context {
turboSnapEnabled?: boolean;
wasLimited?: boolean;
startedAt?: number;
completedAt?: number;
app: {
manageUrl: string;
setupUrl: string;
Expand Down

0 comments on commit 0ef41ab

Please sign in to comment.