cypress-mochawesome-reporter
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -8,21 +8,22 @@ #!/usr/bin/env node | ||
.options({ | ||
jsonDir: { | ||
cypressConfig: { | ||
alias: 'c', | ||
type: 'string', | ||
default: 'cypress/results/json', | ||
describe: 'Cypress results json folder, should be the same as reportDir in cypress.json', | ||
default: 'cypress.json', | ||
describe: 'cypress.json path', | ||
coerce: path.resolve, | ||
}, | ||
screenshotsDir: { | ||
type: 'string', | ||
default: 'cypress/screenshots', | ||
describe: 'Cypress screenshots directory', | ||
coerce: path.resolve, | ||
}, | ||
output: { | ||
alias: 'o', | ||
type: 'string', | ||
default: 'cypress/reports/html', | ||
describe: 'Path to save report', | ||
coerce: path.resolve, | ||
}, | ||
jsonDir: { | ||
type: 'string', | ||
describe: 'Cypress results json folder, should be the same as reportDir in cypress.json', | ||
}, | ||
screenshotsDir: { | ||
type: 'string', | ||
describe: 'Cypress screenshots folder, should be the same as screenshotsFolder in cypress.json', | ||
}, | ||
}) | ||
@@ -29,0 +30,0 @@ .help('help') |
@@ -6,3 +6,41 @@ const path = require('path'); | ||
function extractReporterOptions(options) { | ||
if (!options) { | ||
return undefined; | ||
} | ||
// Only this reporter enabled | ||
if (!options.reporterEnabled) { | ||
return options; | ||
} | ||
// Multi reporters enabled | ||
return options.cypressMochawesomeReporterReporterOptions; | ||
} | ||
async function getCypressConfig(configPath) { | ||
let config = {}; | ||
try { | ||
config = await fse.readJson(configPath); | ||
} catch { | ||
console.warn(`Couldn't find cypress.json file in "${configPath}", continue with default config`); | ||
} | ||
const baseDir = path.dirname(configPath); | ||
return { | ||
resultsjsonDir: path.join( | ||
baseDir, | ||
(config.reporterOptions && config.reporterOptions.reportDir) || 'cypress/results/json' | ||
), | ||
screenshotsDir: path.join(baseDir, config.screenshotsFolder || 'cypress/screenshots'), | ||
outputFolder: path.join(baseDir, 'cypress/reports/html'), | ||
reporterOptions: extractReporterOptions(config.reporterOptions), | ||
}; | ||
} | ||
async function mergeAndCreate({ jsonDir, output, mochawesomeOptions }) { | ||
console.log(`Read and merge jsons from "${jsonDir}"`); | ||
const report = await merge({ | ||
@@ -12,21 +50,61 @@ files: [jsonDir.concat('/*.json')], | ||
const html = await reportGenerator.create(report, { reportDir: output, ...mochawesomeOptions }); | ||
console.log(`Create HTML report in "${output}"`); | ||
const html = await reportGenerator.create(report, { | ||
reportDir: output, | ||
reportFilename: 'index.html', | ||
...mochawesomeOptions, | ||
}); | ||
return html[0]; | ||
} | ||
async function copyScreenShotsDir({ screenshotsDir, output }) { | ||
async function copyscreenshotsDir({ screenshotsDir, output }) { | ||
const isExists = fse.existsSync(screenshotsDir); | ||
if (isExists) { | ||
await fse.copy(screenshotsDir, path.join(output, 'screenshots'), { recursive: true }); | ||
const outputscreenshotsDir = path.join(output, 'screenshots'); | ||
console.log(`Copy screenshots folder from "${screenshotsDir}" to "${outputscreenshotsDir}"`); | ||
await fse.copy(screenshotsDir, outputscreenshotsDir, { recursive: true }); | ||
} else { | ||
console.log(`Screenshots folder "${screenshotsDir}" not found, nothing to copy`); | ||
} | ||
} | ||
async function generateReport({ jsonDir, screenshotsDir, output, ...mochawesomeOptions }) { | ||
async function normalizeOptions({ | ||
cypressConfig: cypressConfigPath, | ||
jsonDir, | ||
screenshotsDir, | ||
output, | ||
...mochawesomeOptions | ||
}) { | ||
console.log(`Read cypress config file from "${cypressConfigPath}"`); | ||
const cypressConfig = await getCypressConfig(cypressConfigPath); | ||
return { | ||
jsonDir: jsonDir ? path.resolve(jsonDir) : cypressConfig.resultsjsonDir, | ||
screenshotsDir: screenshotsDir ? path.resolve(screenshotsDir) : cypressConfig.screenshotsDir, | ||
output: output ? path.resolve(output) : cypressConfig.outputFolder, | ||
mochawesomeOptions: { ...cypressConfig.reporterOptions, ...mochawesomeOptions }, | ||
}; | ||
} | ||
async function generateReport(options) { | ||
const { jsonDir, screenshotsDir, output, mochawesomeOptions } = await normalizeOptions(options); | ||
const [htmlPath] = await Promise.all([ | ||
mergeAndCreate({ jsonDir, output, mochawesomeOptions }), | ||
copyScreenShotsDir({ screenshotsDir, output }), | ||
mergeAndCreate({ | ||
jsonDir, | ||
output, | ||
mochawesomeOptions, | ||
}), | ||
copyscreenshotsDir({ | ||
screenshotsDir, | ||
output, | ||
}), | ||
]); | ||
console.log('HTML report successfully created!'); | ||
console.log(htmlPath); | ||
@@ -33,0 +111,0 @@ } |
{ | ||
"name": "cypress-mochawesome-reporter", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Zero config Mochawesome reporter for Cypress with screenshots", | ||
@@ -21,3 +21,3 @@ "engines": { | ||
"install-examples": "lerna exec --scope @example/* \"npm i\"", | ||
"test:prepare": "lerna run --stream --no-bail --concurrency 1 --scope @example/* serve-test-report", | ||
"test:prepare": "lerna run --stream --no-bail --concurrency 2 --scope @example/* test-and-report", | ||
"test": "cypress run", | ||
@@ -24,0 +24,0 @@ "cypress:open": "cypress open" |
@@ -56,7 +56,8 @@ # cypress-mochawesome-reporter | ||
| Flag | Type | Default | Description | | ||
| :--------------- | :----- | :------------------------- | :--------------------------------------------------------------------------- | | ||
| --jsonDir | string | [cwd]/cypress/results/json | Cypress results json folder, should be the same as reportDir in cypress.json | | ||
| --screenshotsDir | string | [cwd]/cypress/screenshots | Cypress screenshots directory | | ||
| -o, --output | string | [cwd]/cypress/reports/html | Path to save report | | ||
| Flag | Type | Default | Description | | ||
| :------------------ | :----- | :------------------------- | :---------------------------------------------------------------------------------- | | ||
| -c, --cypressConfig | string | [cwd]/cypress.json | cypress.json file path | | ||
| -o, --output | string | [cwd]/cypress/reports/html | Path to save report | | ||
| --jsonDir | string | [cwd]/cypress/results/json | Cypress results json folder, should be the same as reportDir in cypress.json | | ||
| --screenshotsDir | string | [cwd]/cypress/screenshots | Cypress screenshots folder, should be the same as screenshotsFolder in cypress.json | | ||
@@ -69,3 +70,4 @@ You can also pass [mochawesome-report-generator CLI flags](https://github.com/adamgruber/mochawesome-report-generator#cli-flags) | ||
2. [Using `cypress-multi-reporters`](examples/multiple-reporters) | ||
3. [With `mochawesome-report-generator` CLI flags](examples/simple) | ||
3. [With `mochawesome-report-generator` CLI flags](examples/mochawesome-cli-flags) | ||
4. [Change default screenshots folder in `cypress.json`](examples/screenshots-folder) | ||
@@ -72,0 +74,0 @@ ``` |
9801
157
80