Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geowarin committed Aug 8, 2016
1 parent b00a763 commit 8930083
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 21 deletions.
5 changes: 4 additions & 1 deletion lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ module.exports = function start (context, args) {
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: context.webpackConfig.output.publicPath,
quiet: true
quiet: true,
watchOptions: {
ignored: /node_modules/
}
}));

app.use(require('webpack-hot-middleware')(compiler, {
Expand Down
10 changes: 8 additions & 2 deletions lib/utils/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Debugger {
this.captureConsole(args, console.log);
}
}

info (...args) {
if (this.enabled) {
this.captureConsole(args, console.info, 'green');
Expand All @@ -41,6 +41,12 @@ class Debugger {
this.capturing = true;
}

clearConsole () {
if (!this.capturing) {
process.stdout.write('\x1bc');
}
}

captureConsole (args, method, color) {
if (this.capturing) {
this.capturedMessages.push(stripAnsi(args.join(' ').trim()));
Expand All @@ -55,7 +61,7 @@ class Debugger {
}
return array.map(e => {
if (typeof e === 'string') {
return chalk[color](e);
return chalk[color](e);
}
return e;
})
Expand Down
33 changes: 15 additions & 18 deletions lib/webpack/plugins/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const chalk = require('chalk');
const os = require('os');
const notifier = require('node-notifier');
const formatMessage = require('./formatMessage');
const debug = require('../../utils/debug');

const LOGO = path.join(__dirname, 'tarec_logo_ico.png');

Expand Down Expand Up @@ -30,15 +31,15 @@ class NotifierPlugin {
apply (compiler) {

compiler.plugin('done', stats => {
clearConsole();
debug.clearConsole();

const hasErrors = stats.hasErrors();
const hasWarnings = stats.hasWarnings();
if (!hasErrors && !hasWarnings) {
const time = stats.endTime - stats.startTime;
console.log(chalk.green('Compiled successfully in ' + time + 'ms'));
debug.log(chalk.green('Compiled successfully in ' + time + 'ms'));
if (this.compilationSuccessMessage) {
console.log(this.compilationSuccessMessage);
debug.log(this.compilationSuccessMessage);
}
return;
}
Expand All @@ -65,34 +66,30 @@ class NotifierPlugin {
});

compiler.plugin('invalid', () => {
clearConsole();
console.log(chalk.cyan('Compiling...'));
debug.clearConsole();
debug.log(chalk.cyan('Compiling...'));
});
}
}

module.exports = NotifierPlugin;

function clearConsole () {
process.stdout.write('\x1bc');
}

function displayError (index, severity, error) {
if (error.file) {
console.log(chalk.red((index + 1) + ') ' + severity) + ' in ' + error.file);
debug.log(chalk.red((index + 1) + ') ' + severity) + ' in ' + error.file);
} else {
console.log(chalk.red((index + 1) + ') ' + severity));
debug.log(chalk.red((index + 1) + ') ' + severity));
}
console.log();
console.log(error.message);
debug.log();
debug.log(error.message);
if (error.origin) {
console.log(error.origin);
debug.log(error.origin);
}
console.log();
debug.log();
}

function displayCompilationMessage (message, color) {
console.log();
console.log(chalk[color](message));
console.log();
debug.log();
debug.log(chalk[color](message));
debug.log();
}
192 changes: 192 additions & 0 deletions test/plugins/notifier.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
const expect = require('expect');
const test = require('ava');
const EventEmitter = require('events');
const Stats = require('webpack/lib/Stats');
const Module = require('webpack/lib/Module');
EventEmitter.prototype.plugin = EventEmitter.prototype.on;

const debug = require("../../lib/utils/debug");
const NotifierPlugin = require("../../lib/webpack/plugins/notifier");

test('notifier : capture invalid message', t => {

const notifierPlugin = new NotifierPlugin({});
var mockCompiler = new EventEmitter();
notifierPlugin.apply(mockCompiler);

debug.capture();
mockCompiler.emit('invalid');
t.is(debug.capturedMessages.length, 1);
t.is(debug.capturedMessages[0], 'Compiling...');
debug.endCapture();
});

test('notifier : capture compilation without errors', t => {

const notifierPlugin = new NotifierPlugin({});
var mockCompiler = new EventEmitter();
notifierPlugin.apply(mockCompiler);

const compilation = {
errors: [],
warnings: []
};
const stats = new Stats(compilation);
stats.startTime = 0;
stats.endTime = 100;

debug.capture();
mockCompiler.emit('done', stats);
t.is(debug.capturedMessages.length, 1);
t.is(debug.capturedMessages[0], 'Compiled successfully in 100ms');
debug.endCapture();
});

test('notifier : capture babel syntax error and clean it', t => {

const notifierPlugin = new NotifierPlugin({});
var mockCompiler = new EventEmitter();
notifierPlugin.apply(mockCompiler);

const compilation = {
errors: [{
name: 'ModuleBuildError',
message: 'Module build failed: SyntaxError: /Users/geowarin/dev/projects/tarec-js/src/components/App.js: Unexpected token (7:4)\n\u001b[0m 5 | render\u001b[34m\u001b[1m(\u001b[22m\u001b[39m\u001b[34m\u001b[1m)\u001b[22m\u001b[39m \u001b[32m{\u001b[39m\n 6 | \u001b[36mreturn\u001b[39m \u001b[1m<\u001b[22mh1\u001b[1m>\u001b[22mHello\u001b[1m<\u001b[22m\u001b[1m/\u001b[22mh1\n> 7 | \u001b[32m}\u001b[39m\n | ^\n 8 | \u001b[32m}\u001b[39m\n 9 | \u001b[0m\n at Parser.pp.raise (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/location.js:22:13)\n at Parser.pp.unexpected (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/util.js:89:8)\n at Parser.pp.expect (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/util.js:83:33)\n at Parser.pp.jsxParseClosingElementAt (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/plugins/jsx/index.js:406:8)\n at Parser.pp.jsxParseElementAt (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/plugins/jsx/index.js:426:35)\n at Parser.pp.jsxParseElement (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/plugins/jsx/index.js:465:15)\n at Parser.parseExprAtom (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/plugins/jsx/index.js:16:21)\n at Parser.pp.parseExprSubscripts (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/expression.js:277:19)\n at Parser.pp.parseMaybeUnary (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/expression.js:257:19)\n at Parser.pp.parseExprOps (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/expression.js:188:19)\n at Parser.pp.parseMaybeConditional (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/expression.js:165:19)\n at Parser.pp.parseMaybeAssign (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/expression.js:128:19)\n at Parser.parseMaybeAssign (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/plugins/flow.js:421:24)\n at Parser.pp.parseExpression (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/expression.js:92:19)\n at Parser.pp.parseReturnStatement (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/statement.js:333:26)\n at Parser.pp.parseStatement (/Users/geowarin/dev/projects/react-cli/node_modules/babylon/lib/parser/statement.js:107:19)',
file: 'src/myFile.js'
}],
warnings: []
};
const stats = new Stats(compilation);

debug.capture();
mockCompiler.emit('done', stats);
t.deepEqual(debug.capturedMessages, [
'',
'Failed to compile with 1 errors',
'',
'1) Error in src/myFile.js',
'',
`Module build failed: SyntaxError: /Users/geowarin/dev/projects/tarec-js/src/components/App.js: Unexpected token (7:4)
5 | render() {
6 | return <h1>Hello</h1
> 7 | }
| ^
8 | }
9 | `,
''
]);
debug.endCapture();
});

test('notifier : capture a Module not found error', t => {

const notifierPlugin = new NotifierPlugin({});
var mockCompiler = new EventEmitter();
notifierPlugin.apply(mockCompiler);

const compilation = {
errors: [{
name: 'ModuleNotFoundError',
message: 'Module not found: Error: Can\'t resolve \'reacazet\' in \'/Users/geowarin/dev/projects/tarec-js/src/components\'',
module: new MockModule({file: '/moduleFile.js'})
}],
warnings: []
};
const stats = new Stats(compilation);

debug.capture();
mockCompiler.emit('done', stats);
t.deepEqual(debug.capturedMessages, [
'',
'Failed to compile with 1 errors',
'',
'1) Error in ./moduleFile.js',
'',
"Module not found: Error: Can't resolve 'reacazet' in '/Users/geowarin/dev/projects/tarec-js/src/components'",
''
]);
debug.endCapture();
});

test('notifier : should display the origin of an error', t => {

const notifierPlugin = new NotifierPlugin({});
var mockCompiler = new EventEmitter();
notifierPlugin.apply(mockCompiler);

const compilation = {
errors: [{
name: 'ModuleNotFoundError',
message: 'Some error occurred',
origin: new MockModule({file: '/moduleFile.js', issuer: '/otherModule.js'}),
dependencies: [{
loc: {
start: {line: 36, column: 0},
end: {line: 36, column: 44}
}
}]
}],
warnings: []
};
const stats = new Stats(compilation);

debug.capture();
mockCompiler.emit('done', stats);
t.deepEqual(debug.capturedMessages, [
'',
'Failed to compile with 1 errors',
'',
'1) Error',
'',
'Some error occurred',
`@ ./moduleFile.js 36:0-44
@ ./otherModule.js`,
''
]);
debug.endCapture();
});

test('notifier : capture a warning', t => {

const notifierPlugin = new NotifierPlugin({});
var mockCompiler = new EventEmitter();
notifierPlugin.apply(mockCompiler);

const compilation = {
errors: [],
warnings: [{
name: 'Warning',
message: 'Some warning',
file: 'src/myFile.js'
}]
};
const stats = new Stats(compilation);

debug.capture();
mockCompiler.emit('done', stats);
t.deepEqual(debug.capturedMessages, [
'',
'Compiled with 1 warnings',
'',
'1) Warning in src/myFile.js',
'',
'Some warning',
''
]);
debug.endCapture();
});

class MockModule extends Module {

constructor ({issuer, file}) {
super();
this.file = file;
if (issuer) {
this.issuer = new MockModule({file: issuer});
}
}

readableIdentifier (requestShortener) {
return requestShortener.shorten(process.cwd() + this.file);
};
}

0 comments on commit 8930083

Please sign in to comment.