Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/kbn-scout-reporting/src/reporting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ export const scoutPlaywrightReporter = (
export const scoutFailedTestsReporter = (
options?: ScoutPlaywrightReporterOptions
): ReporterDescription => {
return ['@kbn/scout-reporting/src/reporting/playwright/failed_test', options];
return SCOUT_REPORTER_ENABLED
? ['@kbn/scout-reporting/src/reporting/playwright/failed_test', options]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this an array?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I presume it's to contain more than one reporter much like other test runners. Cool

: ['null'];
};
70 changes: 57 additions & 13 deletions packages/kbn-scout/src/playwright/config/create_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,35 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { SCOUT_REPORTER_ENABLED, SCOUT_SERVERS_ROOT } from '@kbn/scout-info';
import { SCOUT_SERVERS_ROOT } from '@kbn/scout-info';
import { scoutPlaywrightReporter, scoutFailedTestsReporter } from '@kbn/scout-reporting';
import { createPlaywrightConfig } from './create_config';
import { VALID_CONFIG_MARKER } from '../types';
import { generateTestRunId } from '@kbn/scout-reporting';

jest.mock('@kbn/scout-reporting', () => ({
...jest.requireActual('@kbn/scout-reporting'),
generateTestRunId: jest.fn(),
scoutPlaywrightReporter: jest.fn(),
scoutFailedTestsReporter: jest.fn(),
}));

describe('createPlaywrightConfig', () => {
const mockedRunId = 'mocked-run-id';
const mockGenerateTestRunId = generateTestRunId as jest.Mock;
const mockedScoutPlaywrightReporter = scoutPlaywrightReporter as jest.Mock;
const mockedScoutFailedTestsReporter = scoutFailedTestsReporter as jest.Mock;

beforeEach(() => {
jest.clearAllMocks();
delete process.env.TEST_RUN_ID;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had no idea you could do this...deleting an env var that was on the cli? Pretty cool

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found few examples of it online: not sure if it is the most optimal, but it works

});

it('should return a valid default Playwright configuration', () => {
const testRunId = 'test-run-id';
mockGenerateTestRunId.mockImplementationOnce(() => testRunId);
mockGenerateTestRunId.mockImplementationOnce(() => mockedRunId);
// Scout reporters are disabled by default
mockedScoutPlaywrightReporter.mockReturnValueOnce(['null']);
mockedScoutFailedTestsReporter.mockReturnValueOnce(['null']);

const testDir = './my_tests';
const config = createPlaywrightConfig({ testDir });
Expand All @@ -49,28 +58,63 @@ describe('createPlaywrightConfig', () => {
expect(config.reporter).toEqual([
['html', { open: 'never', outputFolder: './output/reports' }],
['json', { outputFile: './output/reports/test-results.json' }],
SCOUT_REPORTER_ENABLED
? [
'@kbn/scout-reporting/src/reporting/playwright/events',
{ name: 'scout-playwright', runId: testRunId },
]
: ['null'],
[
'@kbn/scout-reporting/src/reporting/playwright/failed_test',
{ name: 'scout-playwright-failed-tests', runId: testRunId },
],
['null'],
['null'],
]);
expect(config.timeout).toBe(60000);
expect(config.expect?.timeout).toBe(10000);
expect(config.outputDir).toBe('./output/test-artifacts');
expect(config.projects![0].name).toEqual('chromium');
});

it('should return a Playwright configuration with Scout reporters', () => {
mockGenerateTestRunId.mockImplementationOnce(() => mockedRunId);
mockedScoutPlaywrightReporter.mockReturnValueOnce([
'@kbn/scout-reporting/src/reporting/playwright/events',
{ name: 'scout-playwright', runId: mockedRunId },
]);
mockedScoutFailedTestsReporter.mockReturnValueOnce([
'@kbn/scout-reporting/src/reporting/playwright/failed_test',
{ name: 'scout-playwright-failed-tests', runId: mockedRunId },
]);

const testDir = './my_tests';
const config = createPlaywrightConfig({ testDir });

expect(mockGenerateTestRunId).toHaveBeenCalledTimes(1);
expect(config.reporter).toEqual([
['html', { open: 'never', outputFolder: './output/reports' }],
['json', { outputFile: './output/reports/test-results.json' }],
[
'@kbn/scout-reporting/src/reporting/playwright/events',
{ name: 'scout-playwright', runId: mockedRunId },
],
[
'@kbn/scout-reporting/src/reporting/playwright/failed_test',
{ name: 'scout-playwright-failed-tests', runId: mockedRunId },
],
]);
});

it(`should override 'workers' count in Playwright configuration`, () => {
const testDir = './my_tests';
const workers = 2;

const config = createPlaywrightConfig({ testDir, workers });
expect(config.workers).toBe(workers);
});

it('should generate and cache runId in process.env.TEST_RUN_ID', () => {
mockGenerateTestRunId.mockReturnValue(mockedRunId);

// First call to create config
createPlaywrightConfig({ testDir: 'tests' });
expect(process.env.TEST_RUN_ID).toBe(mockedRunId);

// Second call (should use the cached value)
createPlaywrightConfig({ testDir: 'tests' });

expect(generateTestRunId).toHaveBeenCalledTimes(1);
expect(process.env.TEST_RUN_ID).toBe(mockedRunId);
});
});
10 changes: 9 additions & 1 deletion packages/kbn-scout/src/playwright/config/create_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ import { SCOUT_SERVERS_ROOT } from '@kbn/scout-info';
import { ScoutPlaywrightOptions, ScoutTestOptions, VALID_CONFIG_MARKER } from '../types';

export function createPlaywrightConfig(options: ScoutPlaywrightOptions): PlaywrightTestConfig {
const runId = generateTestRunId();
/**
* Playwright loads the config file multiple times, so we need to generate a unique run id
* and store it in the environment to be used across all config function calls.
*/
let runId = process.env.TEST_RUN_ID;
if (!runId) {
runId = generateTestRunId();
process.env.TEST_RUN_ID = runId;
}

return defineConfig<ScoutTestOptions>({
testDir: options.testDir,
Expand Down