All files / src/commands/add-jest index.js

23.08% Statements 3/13
0% Branches 0/6
0% Functions 0/6
25% Lines 3/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 50 51 52 53 54 55 56 57 58                6x 6x                 6x                                                                              
import {join} from 'path';
import {
    PackageJsonEditor,
    Scaffolder,
    allDoExist,
    install
} from '../../api';
 
const ALWAYS = () => true;
const JEST_DEPENDENCIES = [
    'jest',
    'jest-watch-typeahead',
    'babel-jest'
];
/**
 * @type {task[]}
 * @see https://jestjs.io/
 */
export const addJest = [
    {
        text: 'Add test tasks and Jest configuration to package.json',
        task: async ({browser}) => {
            const scripts = {
                test: 'jest .*.test.js --coverage',
                'test:ing': 'npm test -- --watchAll'
            };
            const jest = {
                testMatch: ['**/__tests__/**/*.(e2e|test).[jt]s?(x)'],
                setupFilesAfterEnv: browser ? ['<rootDir>/__tests__/setup.js'] : undefined,
                watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname']
            };
            await (new PackageJsonEditor())
                .extend({jest, scripts})
                .commit();
        },
        condition: () => allDoExist('package.json')
    },
    {
        text: 'Copy Jest boilerplate',
        task: async ({browser}) => {
            const scaffolder = new Scaffolder(join(__dirname, 'templates'));
            browser && await scaffolder
                .target('__tests__')
                .copy('setup.js');
            await scaffolder
                .target('__tests__')
                .copy('example.test.js')
                .commit();
        },
        condition: ALWAYS
    },
    {
        text: 'Install Jest dependencies',
        task: ({skipInstall}) => install(JEST_DEPENDENCIES, {dev: true, skipInstall}),
        condition: ({skipInstall}) => !skipInstall && allDoExist('package.json')
    }
];
export default addJest;