Total Complexity | 9 |
Complexity/F | 1.5 |
Lines of Code | 46 |
Function Count | 6 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import path from 'path'; |
||
2 | import { assert } from 'chai'; |
||
3 | import { createLogger, transports } from 'winston'; |
||
4 | import ArrayTransport from 'winston-array-transport'; |
||
5 | import { entry } from './constants'; |
||
6 | |||
7 | export const pause = time => new Promise(res => setTimeout(res, time)); |
||
8 | |||
9 | export async function testFormatter(formatter, data, expected) { |
||
10 | const logs = []; |
||
11 | const logger = createLogger({ |
||
12 | level : 'info', |
||
13 | format : formatter, |
||
14 | transports : [ |
||
15 | new transports.Console(), |
||
16 | new ArrayTransport({ |
||
17 | array : logs |
||
18 | }) |
||
19 | ] |
||
20 | }); |
||
21 | |||
22 | data.forEach(item => { |
||
23 | logger.log('info', item); |
||
24 | }); |
||
25 | for (const line of expected) { |
||
26 | const logged = logs.shift(); |
||
27 | |||
28 | assert.equal(logged, line); |
||
29 | } |
||
30 | } |
||
31 | |||
32 | export function load(relPath, clearCache) { |
||
33 | const absPath = path.resolve(entry, relPath); |
||
34 | |||
35 | if (clearCache) delete require.cache[require.resolve(absPath)]; |
||
|
|||
36 | // eslint-disable-next-line security/detect-non-literal-require |
||
37 | const result = require(absPath); |
||
38 | |||
39 | if (clearCache) delete require.cache[require.resolve(absPath)]; |
||
40 | |||
41 | return result; |
||
42 | } |
||
43 | |||
44 | export function resolve(relPath) { |
||
45 | return require.resolve(path.join(entry, relPath)); |
||
46 | } |
||
47 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.