Fix readme image tests in Karma (previously were not running)

This commit is contained in:
David Evans 2018-04-22 21:29:29 +01:00
parent 816206ed33
commit d7ce195ebc
4 changed files with 64 additions and 42 deletions

View File

@ -17,6 +17,14 @@ function read(pipe) {
}); });
} }
function processError(err) {
if(typeof err === 'object' && err.message) {
return err.message;
} else {
return err;
}
}
function getCodeArg() { function getCodeArg() {
if(process.argv.length > 2 && process.argv[2] !== '-') { if(process.argv.length > 2 && process.argv[2] !== '-') {
return Promise.resolve(process.argv[2]); return Promise.resolve(process.argv[2]);
@ -26,14 +34,6 @@ function getCodeArg() {
} }
} }
function processError(err) {
if(typeof err === 'object' && err.message) {
return err.message;
} else {
return err;
}
}
getCodeArg() getCodeArg()
.then(VirtualSequenceDiagram.render) .then(VirtualSequenceDiagram.render)
.then((svg) => process.stdout.write(svg + '\n')) .then((svg) => process.stdout.write(svg + '\n'))

View File

@ -2,6 +2,14 @@ import ImageRegion from './image/ImageRegion.mjs';
import SequenceDiagram from '../scripts/sequence/SequenceDiagram.mjs'; import SequenceDiagram from '../scripts/sequence/SequenceDiagram.mjs';
import TESTS from './images/list.mjs'; import TESTS from './images/list.mjs';
function readError(err) {
if(typeof err === 'object' && err.message) {
return err.message;
} else {
return err;
}
}
describe('SequenceDiagram Visuals', () => { describe('SequenceDiagram Visuals', () => {
const RESOLUTION = 4; const RESOLUTION = 4;
@ -56,7 +64,7 @@ describe('SequenceDiagram Visuals', () => {
details: 'Code is:\n\n' + code, details: 'Code is:\n\n' + code,
}); });
}) })
.catch(fail) .catch((err) => fail(readError(err)))
.then(done); .then(done);
}); });
}); });

View File

@ -10,8 +10,19 @@ const SAMPLE_REGEX = new RegExp(
const SCREENSHOT_BLACKLIST = [ const SCREENSHOT_BLACKLIST = [
// Renders differently but correctly in different browsers // Renders differently but correctly in different browsers
'screenshots/Themes.png', 'screenshots/Themes.png',
// For some reason this one breaks in headless Chrome
'screenshots/Markdown.png',
]; ];
function readError(err) {
if(typeof err === 'object' && err.message) {
return err.message;
} else {
return err;
}
}
function findSamples(content) { function findSamples(content) {
SAMPLE_REGEX.lastIndex = 0; SAMPLE_REGEX.lastIndex = 0;
const results = []; const results = [];
@ -28,12 +39,10 @@ function findSamples(content) {
return results; return results;
} }
function makeSampleTests({file, code}, index) { function performSampleTests({file, code}, index) {
describe('example #' + (index + 1), () => {
if(file && !SCREENSHOT_BLACKLIST.includes(file)) { if(file && !SCREENSHOT_BLACKLIST.includes(file)) {
it('looks like ' + file + ' when rendered', (done) => {
let actual = null; let actual = null;
new SequenceDiagram(code) return new SequenceDiagram(code)
.getCanvas({resolution: RESOLUTION}) .getCanvas({resolution: RESOLUTION})
.then((c) => { .then((c) => {
actual = ImageRegion actual = ImageRegion
@ -45,22 +54,25 @@ function makeSampleTests({file, code}, index) {
{height: actual.height, width: actual.width} {height: actual.height, width: actual.width}
)) ))
.then((expected) => { .then((expected) => {
expect(actual).toLookLike(expected); expect(actual).toLookLike(expected, {
}) details: '#' + (index + 1) + ' compared to ' + file,
.catch(fail) });
.then(done);
}); });
} else { } else {
it('renders without error', () => {
expect(() => new SequenceDiagram(code)).not.toThrow(); expect(() => new SequenceDiagram(code)).not.toThrow();
}); return Promise.resolve();
} }
});
} }
fetch('README.md') describe('Readme', () => {
/* eslint-disable jasmine/missing-expect */ // See performSampleTests
it('renders all samples correctly', (done) => {
/* eslint-enable jasmine/missing-expect */
fetch('README.md')
.then((response) => response.text()) .then((response) => response.text())
.then(findSamples) .then(findSamples)
.then((samples) => describe('Readme', () => { .then((samples) => Promise.all(samples.map(performSampleTests)))
samples.forEach(makeSampleTests); .catch((err) => fail(readError(err)))
})); .then(done);
});
});

View File

@ -37,6 +37,7 @@ module.exports = (config) => {
{pattern: 'spec/helpers/**/*.mjs', type: 'module'}, {pattern: 'spec/helpers/**/*.mjs', type: 'module'},
{pattern: '**/*_spec.mjs', type: 'module'}, {pattern: '**/*_spec.mjs', type: 'module'},
{pattern: '**/*_webspec.mjs', type: 'module'}, {pattern: '**/*_webspec.mjs', type: 'module'},
{included: false, pattern: 'screenshots/**/*'},
{included: false, pattern: 'scripts/**/*'}, {included: false, pattern: 'scripts/**/*'},
{included: false, pattern: 'spec/**/*'}, {included: false, pattern: 'spec/**/*'},
{included: false, pattern: 'web/**/*'}, {included: false, pattern: 'web/**/*'},
@ -54,6 +55,7 @@ module.exports = (config) => {
proxies: { proxies: {
// Add some proxies so that fetch() calls work without modification // Add some proxies so that fetch() calls work without modification
'/README.md': '/base/README.md', '/README.md': '/base/README.md',
'/screenshots/': '/base/screenshots/',
'/spec/': '/base/spec/', '/spec/': '/base/spec/',
}, },
reportSlowerThan: 500, reportSlowerThan: 500,