Skip to content

Commit

Permalink
reporters integratino test
Browse files Browse the repository at this point in the history
  • Loading branch information
dwightjack committed May 25, 2021
1 parent d7a2632 commit 460c008
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
155 changes: 155 additions & 0 deletions test/integration/cli-reporter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/* eslint max-len: 'off' */
'use strict';

const assert = require('proclaim');
const fs = require('fs');
const path = require('path');

describe('pa11y-ci (with default reporter)', () => {
before(() => {
return global.cliCall(['--config',
'defaults']);
});

it('outputs to the stdout and stderr', () => {
assert.include(global.lastResult.stdout, 'Running Pa11y on 2 URLs');
assert.include(global.lastResult.stderr, 'http://localhost:8090/passing-1 - Failed to run');
assert.include(global.lastResult.stderr, 'http://localhost:8090/passing-2 - Failed to run');
assert.include(global.lastResult.stderr, 'timed out');
});

});

describe('pa11y-ci (with json built-in reporter)', () => {
before(() => {
return global.cliCall(['--config',
'defaults', '--reporter', 'json']);
});


it('logs json to stdout', () => {
let report;
assert.doesNotThrow(() => {
report = JSON.parse(global.lastResult.output);
});
assert.isObject(report);
assert.include(Object.keys(report.results), 'http://localhost:8090/passing-1');
assert.include(Object.keys(report.results), 'http://localhost:8090/passing-2');
});

});

describe('pa11y-ci (with json built-in reporter and configuration)', () => {
const reportFile = path.resolve(__dirname, './mock/config/__output__/results.json');
before(() => {
return global.cliCall(['--config',
'reporters-json']);
});

after(() => {
if (fs.existsSync(reportFile)) {
fs.rmSync(reportFile);
}
});


it('stores report in a file', () => {
assert.equal(global.lastResult.output, '');
let report;

assert.ok(fs.existsSync(reportFile));

assert.doesNotThrow(() => {
report = JSON.parse(fs.readFileSync(reportFile, 'utf-8'));
});
assert.isObject(report);
assert.include(Object.keys(report.results), 'http://localhost:8090/passing-1');
assert.include(Object.keys(report.results), 'http://localhost:8090/passing-2');
});

});


describe('pa11y-ci (with custom json reporter configuration)', () => {
const reportFile = path.resolve(__dirname, './mock/config/__output__/my-results.json');
before(() => {
return global.cliCall(['--config',
'reporters-custom']);
});

after(() => {
if (fs.existsSync(reportFile)) {
fs.rmSync(reportFile);
}
});


it('stores report in a "my-results.json" file', () => {
assert.equal(global.lastResult.output, '');
let report;

assert.ok(fs.existsSync(reportFile));

assert.doesNotThrow(() => {
report = JSON.parse(fs.readFileSync(reportFile, 'utf-8'));
});
assert.isObject(report);
assert.include(Object.keys(report.results), 'http://localhost:8090/passing-1');
assert.include(Object.keys(report.results), 'http://localhost:8090/passing-2');
});

});


describe('pa11y-ci (with multiple reporters)', () => {
const customReportFile = path.resolve(__dirname, './mock/config/__output__/my-results.json');
const defaultReportFile = path.resolve(__dirname, './mock/config/__output__/default.json');
before(() => {
return global.cliCall(['--config',
'reporters-multiple']);
});

after(() => {
if (fs.existsSync(customReportFile)) {
fs.rmSync(customReportFile);
}
if (fs.existsSync(defaultReportFile)) {
fs.rmSync(defaultReportFile);
}
});


it('stores report in a "my-results.json" file', () => {
let report;

assert.ok(fs.existsSync(customReportFile));

assert.doesNotThrow(() => {
report = JSON.parse(fs.readFileSync(customReportFile, 'utf-8'));
});
assert.isObject(report);
assert.include(Object.keys(report.results), 'http://localhost:8090/passing-1');
assert.include(Object.keys(report.results), 'http://localhost:8090/passing-2');
});

it('stores report in the default reporter file', () => {
let report;

assert.ok(fs.existsSync(defaultReportFile));

assert.doesNotThrow(() => {
report = JSON.parse(fs.readFileSync(defaultReportFile, 'utf-8'));
});
assert.isObject(report);
assert.include(Object.keys(report.results), 'http://localhost:8090/passing-1');
assert.include(Object.keys(report.results), 'http://localhost:8090/passing-2');
});

it('outputs results to console', () => {
assert.include(global.lastResult.stdout, 'Running Pa11y on 2 URLs');
assert.include(global.lastResult.stderr, 'http://localhost:8090/passing-1 - Failed to run');
assert.include(global.lastResult.stderr, 'http://localhost:8090/passing-2 - Failed to run');
assert.include(global.lastResult.stderr, 'timed out');
});

});
12 changes: 12 additions & 0 deletions test/integration/mock/config/reporters-custom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"defaults": {
"timeout": 100,
"reporters": [
["./reporters/custom.js", { "fileName": "./__output__/my-results.json"}]
]
},
"urls": [
"http://localhost:8090/passing-1",
"http://localhost:8090/passing-2"
]
}
12 changes: 12 additions & 0 deletions test/integration/mock/config/reporters-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"defaults": {
"timeout": 100,
"reporters": [
["json", { "fileName": "./__output__/results.json"}]
]
},
"urls": [
"http://localhost:8090/passing-1",
"http://localhost:8090/passing-2"
]
}
14 changes: 14 additions & 0 deletions test/integration/mock/config/reporters-multiple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"defaults": {
"timeout": 100,
"reporters": [
"cli",
"./reporters/custom.js",
["./reporters/custom.js", { "fileName": "./__output__/my-results.json"}]
]
},
"urls": [
"http://localhost:8090/passing-1",
"http://localhost:8090/passing-2"
]
}
14 changes: 14 additions & 0 deletions test/integration/mock/config/reporters/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const fs = require('fs');
const {resolve, dirname} = require('path');

module.exports = ({fileName = './__output__/default.json'}) => ({
afterAll(report) {
if (fileName) {
const filePath = resolve(process.cwd(), fileName);
fs.mkdirSync(dirname(filePath), {recursive: true});
fs.writeFileSync(filePath, JSON.stringify(report), 'utf8');
}
}
});

0 comments on commit 460c008

Please sign in to comment.