Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
Wdio Reporter output Directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaime Mackey committed Aug 15, 2021
1 parent 5bb1be6 commit 7dfbda6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/terra-functional-testing/src/config/wdio.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ exports.config = {
/**
* Gets executed once before all workers get launched.
*/
onPrepare() {
onPrepare(config = {}) {
// Clean previous reporter results.
console.log(`\n\nonPrepare:\n${JSON.stringify(config)}\n\n`);
cleanResults();
},
/**
Expand All @@ -152,6 +153,8 @@ exports.config = {
const { launcherOptions } = config;
const { formFactor, locale, theme } = launcherOptions || {};

console.log(`\n\nonComplete:\n${JSON.stringify(config)}\n\n`);

// Merge reporter results.
mergeResults({
...formFactor && { formFactor },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const getOutputDir = require('./get-output-dir');
/**
* Cleans the output directory by removing any previous spec reports.
*/
const cleanResults = () => {
const outputDir = getOutputDir();
const cleanResults = (subOutputDir) => {
const outputDir = getOutputDir(subOutputDir);

if (!fs.existsSync(outputDir)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const path = require('path');

/**
* Returns the default output directory for reporter results.
* @param {string} - Subdirector under `test` to generate report.
* @returns {string} - An output directory.
*/
const getOutputDir = () => path.resolve(process.cwd(), 'tests', 'wdio', 'reports');
const getOutputDir = (outputDir = 'wdio') => path.resolve(process.cwd(), 'tests', outputDir, 'reports');

module.exports = getOutputDir;
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const writeResultsToFile = (outputDir, results, options) => {
* @param {Object} options - Additional option data to include in the output file.
*/
const mergeResults = (options = {}) => {
const outputDir = getOutputDir();
const outputDir = getOutputDir(options.outputDir);

if (!fs.existsSync(outputDir)) {
return;
Expand All @@ -111,7 +111,7 @@ const mergeResults = (options = {}) => {
writeResultsToFile(outputDir, mergedResults, options);

// Delete the individual spec result files.
cleanResults();
cleanResults(options.outputDir);
};

module.exports = mergeResults;
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class SpecReporter extends WDIOReporter {
super({ stdout: true, ...options });
this.screenshotPaths = [];
this.screenshotMap = {};
this.outputDir = options.outputDir;

// Listen to this event when a screenshot is being captured.
eventEmitter.on('terra-functional-testing:capture-screenshot', (latestPath) => {
Expand Down Expand Up @@ -165,14 +166,19 @@ class SpecReporter extends WDIOReporter {
*/
static writeResults(runner, results) {
const { cid } = runner;
const outputDir = getOutputDir();
const outputDir = getOutputDir(this.outputDir);

// Create the output directory if it does not already exist.
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}

const fileName = path.join(outputDir, `wdio-spec-results-${cid}.json`);
let fileName;
if (runner?.launcherOptions?.cloudRegion) {
fileName = path.join(outputDir, `wdio-spec-results-${runner.launcherOptions.cloudRegion}-${cid}.json`);
} else {
fileName = path.join(outputDir, `wdio-spec-results-${cid}.json`);
}

fs.writeFileSync(fileName, JSON.stringify(results, null, 2));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ describe('getOutputDir', () => {

expect(outputDir).toEqual(expect.stringContaining('/tests/wdio/reports'));
});

it('should return the output directory with expected sub directory', () => {
const outputDir = getOutputDir('other');

expect(outputDir).toEqual(expect.stringContaining('/tests/other/reports'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,18 @@ describe('Spec Reporter', () => {
expect(fs.mkdirSync).toHaveBeenCalledWith('/mock/', { recursive: true });
expect(fs.writeFileSync).toHaveBeenCalledWith('/mock/wdio-spec-results-0-0.json', '"mock-results"');
});
it('should write the test results to file with cloud region', () => {
const runner = { cid: '0-0', launcherOptions: { cloudRegion: 'dev' } };

jest.spyOn(fs, 'existsSync').mockImplementationOnce(() => false);
jest.spyOn(fs, 'mkdirSync').mockImplementationOnce(() => {});
jest.spyOn(fs, 'writeFileSync').mockImplementationOnce(() => []);

SpecReporter.writeResults(runner, 'mock-results');

expect(fs.existsSync).toHaveBeenCalledWith('/mock/');
expect(fs.mkdirSync).toHaveBeenCalledWith('/mock/', { recursive: true });
expect(fs.writeFileSync).toHaveBeenCalledWith('/mock/wdio-spec-results-dev-0-0.json', '"mock-results"');
});
});
});

0 comments on commit 7dfbda6

Please sign in to comment.