Skip to content

Commit 45c03bf

Browse files
committed
Failing tests if there are any un-asserted warnings or deprecations
1 parent 77008f2 commit 45c03bf

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

lib/logger.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,18 @@ module.exports = {
6969
log(`${chalk.bgYellow.black('DEPRECATION')} ${chalk.yellow(message)}`);
7070
},
7171

72-
getMessages() {
73-
return messages;
72+
/**
73+
* @returns {Array}
74+
*/
75+
getDeprecations() {
76+
return messages.deprecation;
77+
},
78+
79+
/**
80+
* @returns {Array}
81+
*/
82+
getWarnings() {
83+
return messages.warning;
7484
},
7585

7686
quiet(setQuiet = true) {

test/_unsilencedLogsCheck.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <fabien@symfony.com>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const logger = require('../lib/logger');
13+
14+
beforeEach(function() {
15+
logger.quiet();
16+
});
17+
18+
afterEach(function() {
19+
if (logger.getDeprecations().length > 0) {
20+
this.test.error(new Error(`There were ${logger.getWarnings().length} unexpected deprecation log messages: \n${logger.getDeprecations().join('\n')}`));
21+
}
22+
23+
if (logger.getWarnings().length > 0) {
24+
this.test.error(new Error(`There were ${logger.getWarnings().length} unexpected warning log messages: \n${logger.getWarnings().join('\n')}`));
25+
}
26+
27+
logger.reset();
28+
});

test/helpers/logger-assert.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <fabien@symfony.com>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const logger = require('../../lib/logger');
13+
14+
function assertWarning(expectedMessage) {
15+
assertLogMessage(logger.getWarnings(), 'warning', expectedMessage);
16+
}
17+
18+
function assertDeprecation(expectedMessage) {
19+
assertLogMessage(logger.getDeprecations(), 'deprecation', expectedMessage);
20+
}
21+
22+
function assertLogMessage(messages, description, expectedMessage) {
23+
if (messages.length === 0) {
24+
throw new Error(`Found zero log ${description}s. And so, expected ${description} ${expectedMessage} was not logged.`);
25+
}
26+
27+
let isFound = false;
28+
messages.forEach(function(message, index) {
29+
if (!isFound && message.includes(expectedMessage)) {
30+
isFound = true;
31+
// remove from the array now that it is found
32+
messages.splice(index, 1);
33+
}
34+
});
35+
36+
if (!isFound) {
37+
throw new Error(`Did not find any log ${description}s matching ${expectedMessage}. Found: ${messages.join('\n')}`);
38+
}
39+
}
40+
41+
module.exports = {
42+
assertWarning,
43+
assertDeprecation
44+
};

0 commit comments

Comments
 (0)