UNPKG

differencify-zerdos-jest-reporter

Version:
89 lines (72 loc) 3.48 kB
'use strict'; var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _pkgDir = require('pkg-dir'); var _pkgDir2 = _interopRequireDefault(_pkgDir); var _logger = require('./utils/logger'); var _logger2 = _interopRequireDefault(_logger); var _paths = require('./utils/paths'); var _reportTypes = require('./reportTypes'); var _reportTypes2 = _interopRequireDefault(_reportTypes); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } module.exports = class DifferencifyReporter { constructor(globalConfig = {}, options = {}) { this.globalConfig = globalConfig; // eslint-disable-next-line prefer-object-spread/prefer-object-spread this.options = { reportPath: 'differencify_reports', reportTypes: { html: 'index.html' }, imageType: 'png', debug: true, isJest: true, failedOnly: false }; Object.assign(this.options, options); this.resolvedReportPath = _path2.default.resolve(_pkgDir2.default.sync(), this.options.reportPath); if (this.options.debug === true) { _logger2.default.enable(); } } getTestResults(testResults) { return testResults.testResults.map(result => { const { fullName: testName, status } = result; const snapshotsDir = (0, _paths.getSnapshotsDir)({ testPath: testResults.testFilePath, isJest: this.options.isJest }); const snapshotPath = (0, _paths.getSnapshotPath)(snapshotsDir, { testName, imageType: this.options.imageType }); const currentImageDir = (0, _paths.getCurrentImageDir)(snapshotsDir); const currentImagePath = (0, _paths.getCurrentImagePath)(currentImageDir, { testName, imageType: this.options.imageType }); const diffDir = (0, _paths.getDiffDir)(snapshotsDir); const diffPath = (0, _paths.getDiffPath)(diffDir, { testName, imageType: this.options.imageType }); return { status, testName, snapshotPath: _fs2.default.existsSync(snapshotPath) && _path2.default.relative(this.resolvedReportPath, snapshotPath), currentImagePath: _fs2.default.existsSync(currentImagePath) && _path2.default.relative(this.resolvedReportPath, currentImagePath), diffPath: _fs2.default.existsSync(diffPath) && _path2.default.relative(this.resolvedReportPath, diffPath) }; }).filter(result => result.snapshotPath).filter(result => !this.options.failedOnly || result.status === 'failed'); } generate(results) { Object.keys(this.options.reportTypes).forEach(type => { const reportFilepath = _path2.default.join(this.resolvedReportPath, this.options.reportTypes[type]); try { const report = (0, _reportTypes2.default)(type, results); _fs2.default.writeFileSync(reportFilepath, report); _logger2.default.log(`Generated ${type} report at ${reportFilepath}`); } catch (err) { _logger2.default.error(`Unable to generate ${type} report at ${reportFilepath}: ${err}`); } }); } onRunComplete(contexts, aggregatedResults) { if (!_fs2.default.existsSync(this.resolvedReportPath)) { _fs2.default.mkdirSync(this.resolvedReportPath); } const results = aggregatedResults.testResults.reduce((flattedResults, testResults) => flattedResults.concat(this.getTestResults(testResults)), []); this.generate(results); } }; //# sourceMappingURL=index.js.map