Skip to content

Commit

Permalink
Merge pull request #937 from chromaui/jarel/ap-4316-cli-exits-unexpec…
Browse files Browse the repository at this point in the history
…tedly-when-junitreport-true-is-in-my-config

Fix support for boolean value to `junitReport` option
  • Loading branch information
thafryer authored Mar 5, 2024
2 parents 0994508 + 748a3db commit 30bc7c4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
99 changes: 99 additions & 0 deletions node-src/tasks/report.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import reportBuilder from 'junit-report-builder';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { generateReport } from './report';
import path from 'path';

const log = { error: vi.fn(), info: vi.fn() };
const mockTests = [
{
status: 'ACCEPTED',
result: '',
spec: { name: '', component: { name: '' } },
parameters: { viewportIsDefault: true, viewport: 1080 },
mode: { name: 'Beast Mode' },
},
{
status: 'PENDING',
result: '',
spec: { name: '', component: { name: '' } },
parameters: { viewportIsDefault: true, viewport: 1080 },
mode: { name: 'Beast Mode' },
},
{
status: 'DENIED',
result: '',
spec: { name: '', component: { name: '' } },
parameters: { viewportIsDefault: false, viewport: 1080 },
mode: { name: 'Beast Mode' },
},
{
status: 'BROKEN',
result: '',
spec: { name: '', component: { name: '' } },
parameters: { viewportIsDefault: true, viewport: 1080 },
mode: { name: null },
},
{
status: 'FAILED',
result: '',
spec: { name: '', component: { name: '' } },
parameters: { viewportIsDefault: false, viewport: 1080 },
mode: { name: null },
},
];

vi.spyOn(reportBuilder, 'writeTo').mockImplementation(vi.fn());

describe('generateRport', () => {
const client = { runQuery: vi.fn(), setAuthorization: vi.fn() };
const build = {
app: { repository: { provider: 'github' } },
number: 1,
reportToken: 'report-token',
};
beforeEach(() => {
vi.resetAllMocks();
client.runQuery.mockReturnValue({
app: {
build: {
number: 1,
status: 'PASSED',
createdAt: 0,
completedAt: 0,
webUrl: 'https://google.com',
storybookUrl: 'https://storybook.js.org',
tests: mockTests,
},
},
});
});
it('sucessfully generates report when passed a string', async () => {
const ctx = {
client,
log,
options: {
junitReport: 'tests-file.xml',
},
build,
} as any;
await generateReport(ctx);
expect(reportBuilder.writeTo).toHaveBeenCalledWith(
path.join(__dirname, '../../tests-file.xml')
);
});

it('sucessfully generates report when passed a boolean equal to true', async () => {
const ctx = {
client,
log,
options: {
junitReport: true,
},
build,
} as any;
await generateReport(ctx);
expect(reportBuilder.writeTo).toHaveBeenCalledWith(
path.join(__dirname, '../../chromatic-build-1.xml')
);
});
});
6 changes: 5 additions & 1 deletion node-src/tasks/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ export const generateReport = async (ctx: Context) => {
const { junitReport } = ctx.options;
const { number: buildNumber, reportToken } = ctx.build;

ctx.reportPath = path.resolve(junitReport.replace(/{buildNumber}/g, String(buildNumber)));
const file =
typeof junitReport === 'boolean' && junitReport
? 'chromatic-build-{buildNumber}.xml'
: junitReport;
ctx.reportPath = path.resolve(file.replace(/{buildNumber}/g, String(buildNumber)));

const {
app: { build },
Expand Down

0 comments on commit 30bc7c4

Please sign in to comment.