Skip to content

Commit 79d79ce

Browse files
author
Elrey Belmonti
committed
add unit test for utils/format-output.js
1 parent e5acbcd commit 79d79ce

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

test/utils/format-assets.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"use strict";
22

3-
const { expect } = require("chai");
4-
53
const { _getAssetSize, _getTotalSize, _printAssets } = require("../../utils/format-assets");
64

75
describe("format-assets.js", () => {

test/utils/format-output.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
3+
const { _isLikelyASyntaxError, _lineJoin, _formatMessage } = require("../../utils/format-output");
4+
5+
describe("format-output.js", () => {
6+
describe("#_isLikelyASyntaxError", () => {
7+
context("when message is a syntax error", () => {
8+
it("returns true", () => {
9+
const message = "Syntax error: missing ; before statement";
10+
expect(_isLikelyASyntaxError(message)).to.be.true;
11+
});
12+
});
13+
14+
context("when message is a type error", () => {
15+
it("returns false", () => {
16+
const message = "Type error: null has no properties";
17+
expect(_isLikelyASyntaxError(message)).to.be.false;
18+
});
19+
});
20+
});
21+
22+
describe("#_formatMessage", () => {
23+
const message1 = "Module build failed: SyntaxError: missing ; before statement";
24+
const message2 = "/Module not found: Error: Cannot resolve 'file' or 'directory'/";
25+
it("returns a readable user friendly message", () => {
26+
expect(_formatMessage(message1)).to.equal("Syntax error: missing ; before statement");
27+
expect(_formatMessage(message2)).to.equal("/Module not found:/");
28+
});
29+
});
30+
31+
describe("#_lineJoin", () => {
32+
it("returns the elements of an array on a newline as a string", () => {
33+
const array = ["word", "word2", "word3"];
34+
const output = "word\nword2\nword3";
35+
expect(_lineJoin(array)).to.equal(output);
36+
});
37+
});
38+
});

utils/format-output.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,19 @@
22

33
const friendlySyntaxErrorLabel = "Syntax error:";
44

5-
function isLikelyASyntaxError(message) {
5+
function _isLikelyASyntaxError(message) {
66
return message.indexOf(friendlySyntaxErrorLabel) !== -1;
77
}
88

9-
function formatMessage(message) {
9+
function _formatMessage(message) {
1010
return message
11-
.replace(
12-
"Module build failed: SyntaxError:",
13-
friendlySyntaxErrorLabel
14-
)
15-
.replace(
16-
/Module not found: Error: Cannot resolve 'file' or 'directory'/,
17-
"Module not found:"
18-
)
11+
.replace("Module build failed: SyntaxError:", friendlySyntaxErrorLabel)
12+
.replace(/Module not found: Error: Cannot resolve 'file' or 'directory'/, "Module not found:")
1913
.replace(/^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, "")
2014
.replace("./~/css-loader!./~/postcss-loader!", "");
2115
}
2216

23-
function lineJoin(arr) {
17+
function _lineJoin(arr) {
2418
return arr.join("\n");
2519
}
2620

@@ -31,20 +25,20 @@ function formatOutput(stats) {
3125
const hasWarnings = stats.hasWarnings();
3226

3327
const json = stats.toJson();
34-
let formattedErrors = json.errors.map(message => `Error in ${formatMessage(message)}`);
35-
const formattedWarnings = json.warnings.map(message => `Warning in ${formatMessage(message)}`);
28+
let formattedErrors = json.errors.map(message => `Error in ${_formatMessage(message)}`);
29+
const formattedWarnings = json.warnings.map(message => `Warning in ${_formatMessage(message)}`);
3630

3731
if (hasErrors) {
3832
output.push("{red-fg}Failed to compile.{/}");
3933
output.push("");
40-
if (formattedErrors.some(isLikelyASyntaxError)) {
41-
formattedErrors = formattedErrors.filter(isLikelyASyntaxError);
34+
if (formattedErrors.some(_isLikelyASyntaxError)) {
35+
formattedErrors = formattedErrors.filter(_isLikelyASyntaxError);
4236
}
4337
formattedErrors.forEach(message => {
4438
output.push(message);
4539
output.push("");
4640
});
47-
return lineJoin(output);
41+
return _lineJoin(output);
4842
}
4943

5044
if (hasWarnings) {
@@ -55,13 +49,16 @@ function formatOutput(stats) {
5549
output.push("");
5650
});
5751

58-
return lineJoin(output);
52+
return _lineJoin(output);
5953
}
6054

6155
output.push("{green-fg}Compiled successfully!{/}");
6256
output.push("");
6357

64-
return lineJoin(output);
58+
return _lineJoin(output);
6559
}
6660

67-
module.exports = formatOutput;
61+
module.exports = { formatOutput,
62+
_formatMessage,
63+
_isLikelyASyntaxError,
64+
_lineJoin };

0 commit comments

Comments
 (0)