All files utils.js

100% Statements 15/15
10% Branches 1/10
100% Functions 8/8
100% Lines 15/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                  1x           1x     2x       3x       2x 2x       11x       1x 4x 4x 4x 4x 4x       1x     1x  
import { spawnSync } from 'child_process';
import {
    compact,
    findIndex,
    get,
    partial,
    takeRight,
} from 'lodash';
 
export const commands = {
    base: 'jest',
    coverage: ['--coverage', '--json'],
    showConfig: ['--showConfig', '--json'],
};
 
export const HEAD_STRING = 'Jest: Coverage for';
 
export function getThresholds(config = {}) {
    return get(JSON.parse(config), 'globalConfig.coverageThreshold.global');
}
 
export function splitResults(results = '') {
    return results.split(/\r?\n/);
}
 
export function getLastLines(results = '', count = 0) {
    const resultsList = splitResults(results);
    return takeRight(compact(resultsList), count);
}
 
export function matchLine(str = '', line = '') {
    return line.match(str);
}
 
export function findFailures(thresholds = [], lines = []) {
    return thresholds.map((threshold) => {
        const matchString = `${HEAD_STRING} ${threshold}`;
        const appliedMatchLine = partial(matchLine, matchString);
        const index = findIndex(lines, appliedMatchLine);
        return index > -1 ? { threshold, failure: lines[index] } : false;
    }).filter(failure => failure);
}
 
export function getStderr(base, command) {
    return spawnSync(base, command).stderr.toString();
}
 
export const appliedGetStderr = partial(getStderr, commands.base);