forked from NimaSoroush/differencify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generate report feature. Refs NimaSoroush#10
Supports HTML and JSON report types.
- Loading branch information
1 parent
579edfc
commit 7001c47
Showing
16 changed files
with
320 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,6 @@ dist/ | |
docs/ | ||
.vscode/ | ||
differencify_report/ | ||
screenshots/ | ||
screenshots/ | ||
# OSX hidden files | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import logger from './logger'; | ||
import getHtmlReport from './reportTypes/htmlReport'; | ||
import getJsonReport from './reportTypes/jsonReport'; | ||
|
||
const saveReport = (filepath, contents) => { | ||
fs.writeFileSync(filepath, contents); | ||
}; | ||
|
||
const getReport = (key, results) => { | ||
switch (key) { | ||
case 'json': | ||
return getJsonReport(results); | ||
case 'html': | ||
default: | ||
return getHtmlReport(results); | ||
} | ||
}; | ||
|
||
class Reporter { | ||
|
||
constructor() { | ||
this.results = []; | ||
} | ||
|
||
addResult(outcome, fileName, message, diff) { | ||
this.results.push({ | ||
outcome, | ||
fileName: path.basename(fileName), | ||
message, | ||
diff: diff ? path.basename(diff) : null, | ||
}); | ||
} | ||
|
||
getResults() { | ||
return this.results; | ||
} | ||
|
||
generate(types, testReportPath) { | ||
Object.keys(types).forEach((type) => { | ||
const filepath = path.join(testReportPath, types[type]); | ||
try { | ||
const template = getReport(type, this.getResults()); | ||
saveReport(filepath, template); | ||
logger.log(`Generated ${type} report at ${filepath}`); | ||
} catch (err) { | ||
logger.error(`Unable to generate ${type} report at ${filepath}: ${err}`); | ||
} | ||
}); | ||
return true; | ||
} | ||
} | ||
|
||
export { getHtmlReport, getJsonReport }; | ||
|
||
export default Reporter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import fs from 'fs'; | ||
import Reporter, { getHtmlReport, getJsonReport } from './Reporter'; | ||
import logger from './logger'; | ||
|
||
jest.mock('fs', () => ({ | ||
writeFileSync: jest.fn(), | ||
})); | ||
|
||
jest.mock('./logger', () => ({ | ||
log: jest.fn(), | ||
})); | ||
|
||
const results = [ | ||
{ | ||
outcome: true, | ||
fileName: 'image1.png', | ||
message: 'no mismatch found', | ||
diff: null, | ||
}, | ||
{ | ||
outcome: true, | ||
fileName: 'image2.png', | ||
message: 'no mismatch found', | ||
diff: null, | ||
}, | ||
{ | ||
outcome: false, | ||
fileName: 'image2.png', | ||
message: 'mismatch found!', | ||
diff: 'image2_diff.png', | ||
}, | ||
]; | ||
|
||
describe('Generate report index', () => { | ||
let reporter; | ||
|
||
beforeEach(() => { | ||
reporter = new Reporter(); | ||
results.forEach(result => | ||
reporter.addResult( | ||
result.outcome, | ||
result.fileName, | ||
result.message, | ||
result.diff, | ||
), | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
fs.writeFileSync.mockClear(); | ||
logger.log.mockClear(); | ||
}); | ||
|
||
it('generates a HTML report', () => { | ||
reporter.generate( | ||
{ | ||
html: 'index.html', | ||
}, | ||
'./example/path', | ||
); | ||
expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
'example/path/index.html', | ||
getHtmlReport(results), | ||
); | ||
}); | ||
|
||
it('generates a JSON report', () => { | ||
reporter.generate( | ||
{ | ||
json: 'report.json', | ||
}, | ||
'./example/path', | ||
); | ||
expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
'example/path/report.json', | ||
getJsonReport(results), | ||
); | ||
}); | ||
|
||
it('logs the report paths', () => { | ||
reporter.generate( | ||
{ | ||
html: 'index.html', | ||
json: 'report.json', | ||
}, | ||
'./example/path', | ||
); | ||
expect(logger.log).toHaveBeenCalledWith( | ||
'Generated json report at example/path/report.json', | ||
); | ||
expect(logger.log).toHaveBeenCalledWith( | ||
'Generated html report at example/path/index.html', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.