Skip to content
This repository has been archived by the owner on Sep 11, 2018. It is now read-only.

feat: add JSON reporter. fixes #16 #39

Merged
merged 5 commits into from
Jun 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/reporters/JsonReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const weblog = require('webpack-log');

const WebpackCommandError = require('../WebpackCommandError');

const Reporter = require('./Reporter');

module.exports = class JesonReporter extends Reporter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this perhaps be JsonReporter instead of JesonReporter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jeez, yeah it should. I shouldn't PR so early in the morning 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda liked JesonReporter 😄

constructor(...args) {
super(...args);

const log = weblog({ name: 'webpack', id: 'webpack-command' });
this.originalLevel = log.level;
log.level = 'silent';
this.log = log;
}

/* istanbul ignore next */
progress() {
throw new WebpackCommandError(
'Build rogress display is not supported when using the JSON reporter'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo here:
rogress instead of progress

);
}

render(error, stats) {
const json = stats.toJson();
const result = JSON.stringify(json);

process.stdout.write(result);

this.log.level = this.originalLevel;

return result;
}
};
25 changes: 25 additions & 0 deletions test/fixtures/reporters/json/json-multi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { resolve } = require('path');

const config = require('../../common/webpack.config');

module.exports = {
arguments: [],

config: [
Object.assign({}, config, {
entry: resolve(__dirname, '../../common/entry-a.js'),
reporter: 'json',
}),
Object.assign({}, config, {
entry: [
resolve(__dirname, '../../common/entry-b.js'),
resolve(__dirname, '../../common/entry-c.js'),
],
reporter: 'json',
}),
],

inspect: 'stdout',

group: 'general',
};
16 changes: 16 additions & 0 deletions test/fixtures/reporters/json/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { resolve } = require('path');

const config = require('../../common/webpack.config');

module.exports = {
arguments: [],

config: Object.assign({}, config, {
entry: resolve(__dirname, '../../common/entry-a.js'),
reporter: 'json',
}),

inspect: 'stdout',

group: 'general',
};
7 changes: 7 additions & 0 deletions test/fixtures/reporters/json/problems/entry-problems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// warning
console.log(require); // eslint-disable-line

// error
if (!window) { require('test'); } // eslint-disable-line

require('./image.jpg');
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions test/fixtures/reporters/json/problems/problems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { resolve } = require('path');

const config = require('../../../common/webpack.config');

module.exports = {
arguments: [],

config: Object.assign({}, config, {
entry: resolve(__dirname, './entry-problems.js'),
reporter: 'json',
}),

inspect: 'stdout',

group: 'general',
};
185 changes: 185 additions & 0 deletions test/tests/__snapshots__/reporters.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`JsonReporter > json: reporter should apply #0 1`] = `
Object {
"context": "<PROJECT_ROOT>",
"entry": "<PROJECT_ROOT>/test/fixtures/common/entry-a.js",
"mode": "development",
"plugins": Array [
NamedModulesPlugin {
"options": Object {},
},
],
"reporter": "json",
}
`;

exports[`JsonReporter > json-multi: reporter should apply #0 1`] = `
Array [
Object {
"context": "<PROJECT_ROOT>",
"entry": "<PROJECT_ROOT>/test/fixtures/common/entry-a.js",
"mode": "development",
"plugins": Array [
NamedModulesPlugin {
"options": Object {},
},
],
"reporter": "json",
},
Object {
"context": "<PROJECT_ROOT>",
"entry": Array [
"<PROJECT_ROOT>/test/fixtures/common/entry-b.js",
"<PROJECT_ROOT>/test/fixtures/common/entry-c.js",
],
"mode": "development",
"plugins": Array [
NamedModulesPlugin {
"options": Object {},
},
],
"reporter": "json",
},
]
`;

exports[`JsonReporter > problems/problems: reporter should apply #0 1`] = `
Object {
"context": "<PROJECT_ROOT>",
"entry": "<PROJECT_ROOT>/test/fixtures/reporters/json/problems/entry-problems.js",
"mode": "development",
"plugins": Array [
NamedModulesPlugin {
"options": Object {},
},
],
"reporter": "json",
}
`;

exports[`StylishReporter > problems/stylish-problems reporter should apply #0 1`] = `
Object {
"context": "<PROJECT_ROOT>",
Expand Down Expand Up @@ -29,6 +87,50 @@ webpack v4.6.0
<duration>


./test/fixtures/reporters/stylish/problems/entry-problems.js
5:15 error Module not found: Error: Can't resolve 'test' in
'<PROJECT_ROOT>/test/fixtures/reporters/stylish/problems'
2:12 warning Critical dependency: require function is used in a way in which
dependencies cannot be statically extracted

./test/fixtures/reporters/stylish/problems/image.jpg
1:0 error Module parse failed: Unexpected character '�' You may need an
appropriate loader to handle this file type. (Source code omitted for
this binary file) @
./test/fixtures/reporters/stylish/problems/entry-problems.js 7:0-22

✖ 3 problems (2 errors, 1 warning)"
`;

exports[`StylishReporter > problems/stylish-problems: reporter should apply #0 1`] = `
Object {
"context": "<PROJECT_ROOT>",
"entry": "<PROJECT_ROOT>/test/fixtures/reporters/stylish/problems/entry-problems.js",
"mode": "development",
"plugins": Array [
NamedModulesPlugin {
"options": Object {},
},
],
}
`;

exports[`StylishReporter > problems/stylish-problems: reporter should build #0 1`] = `
"
webpack v4.6.0

<hash>
size name module status
160 B problems sync recursive ./test/fixtures/reporters/stylish/problems sync built
148 B entry-problems.js ./test/fixtures/reporters/stylish/problems/entry-problems.js built ⚠ ✖
177 B image.jpg ./test/fixtures/reporters/stylish/problems/image.jpg built failed ✖

size name asset status
<size> main main.js emitted

<duration>


./test/fixtures/reporters/stylish/problems/entry-problems.js
5:15 error Module not found: Error: Can't resolve 'test' in
'<PROJECT_ROOT>/test/fixtures/reporters/stylish/problems'
Expand Down Expand Up @@ -72,6 +174,34 @@ webpack v4.6.0
"
`;

exports[`StylishReporter > stylish: reporter should apply #0 1`] = `
Object {
"context": "<PROJECT_ROOT>",
"entry": "<PROJECT_ROOT>/test/fixtures/common/entry-a.js",
"mode": "development",
"plugins": Array [
NamedModulesPlugin {
"options": Object {},
},
],
}
`;

exports[`StylishReporter > stylish: reporter should build #0 1`] = `
"
webpack v4.6.0

<hash>
size name module status
41 B entry-a.js ./test/fixtures/common/entry-a.js built

size name asset status
<size> main main.js emitted

<duration>
"
`;

exports[`StylishReporter > stylish-multi reporter should apply #0 1`] = `
Array [
Object {
Expand Down Expand Up @@ -127,6 +257,61 @@ webpack v4.6.0
total <duration>"
`;

exports[`StylishReporter > stylish-multi: reporter should apply #0 1`] = `
Array [
Object {
"context": "<PROJECT_ROOT>",
"entry": "<PROJECT_ROOT>/test/fixtures/common/entry-a.js",
"mode": "development",
"plugins": Array [
NamedModulesPlugin {
"options": Object {},
},
],
},
Object {
"context": "<PROJECT_ROOT>",
"entry": Array [
"<PROJECT_ROOT>/test/fixtures/common/entry-b.js",
"<PROJECT_ROOT>/test/fixtures/common/entry-c.js",
],
"mode": "development",
"plugins": Array [
NamedModulesPlugin {
"options": Object {},
},
],
},
]
`;

exports[`StylishReporter > stylish-multi: reporter should build #0 1`] = `
"
webpack v4.6.0

<hash>
size name module status
41 B entry-a.js ./test/fixtures/common/entry-a.js built

size name asset status
<size> main main.js emitted

<duration>

<hash>
size name module status
41 B entry-b.js ./test/fixtures/common/entry-b.js built
41 B entry-c.js ./test/fixtures/common/entry-c.js built
40 B 0 multi ./test/fixtures/common/entry-b.js ./test/fixtures/common/entry-c.js built

size name asset status
<size> main main.js emitted

<duration>

total <duration>"
`;

exports[`reporter parse util > should parse hidden #0 1`] = `"(1 asset, 2 modules hidden)"`;

exports[`reporter parse util > should parse status: cacheable #0 1`] = `
Expand Down
40 changes: 37 additions & 3 deletions test/tests/reporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ test('StylishReporter', module, () => {

let config;

it(`${name} reporter should validate`, () => {
it(`${name}: reporter should validate`, () => {
expect(validate(opts)).toEqual(true);
});

it(`${name} reporter should apply`, () => {
it(`${name}: reporter should apply`, () => {
config = apply(opts);

expect(config).toMatchSnapshot();
});

it(`${name} reporter should build`, () =>
it(`${name}: reporter should build`, () =>
build(config).then((result) => {
expect(
strip(result)
Expand All @@ -84,3 +84,37 @@ test('StylishReporter', module, () => {
}));
}
});

test('JsonReporter', module, () => {
for (const name of ['json', 'json-multi', 'problems/problems']) {
const fixture = `json/${name}`;
const fixtureType = 'reporters';
const opts = { fixture, fixtureType };

let config;

it(`${name}: reporter should validate`, () => {
expect(validate(opts)).toEqual(true);
});

it(`${name}: reporter should apply`, () => {
config = apply(opts);

expect(config).toMatchSnapshot();
});

it(`${name}: reporter should build`, () =>
build(config).then((result) => {
expect(result.length).toBeGreaterThan(0);

const json = JSON.parse(result);

expect(Object.keys(json).length).toBeGreaterThan(0);

if (name === 'problems/problems') {
expect(json.errors.length).toBeGreaterThan(0);
expect(json.warnings.length).toBeGreaterThan(0);
}
}));
}
});