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

92.86% Statements 26/28
75% Branches 3/4
88.89% Functions 16/18
92.59% Lines 25/27

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129                        6x 6x               6x       2x       2x       2x         2x               2x       2x         2x 2x       2x         2x                               2x     6x       1x 1x       1x         1x               1x       1x         1x 1x       1x         1x      
import {join} from 'path';
import {
    EslintConfigModuleEditor,
    PackageJsonEditor,
    Scaffolder,
    allDoExist,
    allDoNotExist,
    createJsonEditor,
    install,
    uninstall
} from '../../api';
 
const ALWAYS = () => true;
const CYPRESS_DEPENDENCIES = [
    'cypress',
    'cypress-image-snapshot' // https://github.com/palmerhq/cypress-image-snapshot
];
/**
 * @type {task[]}
 * @see https://www.cypress.io/
 */
export const addCypress = [
    {
        text: 'Create Cypress config file',
        task: async ({port}) => {
            const Editor = createJsonEditor('cypress.json', {
                baseUrl: `http://localhost:${port}`,
                video: false
            });
            await (new Editor())
                .create()
                .commit();
        },
        condition: () => allDoNotExist('cypress.json')
    },
    {
        text: 'Add Cypress test tasks to package.json',
        task: async () => {
            const scripts = {
                'cy:open': 'cypress open',
                'cy:run': 'cypress run',
                'cy:update': 'npm run cy:run -- --env updateSnapshots=true',
                'test:visual': 'npm-run-all --parallel start cy:open',
                'test:visual:update': 'del-cli ./cypress/snapshots',
                'test:visual:ci': 'npm-run-all --parallel start cy:run'
            };
            await (new PackageJsonEditor())
                .extend({scripts})
                .commit();
        },
        condition: () => allDoExist('package.json')
    },
    {
        text: 'Add Cypress global variable to .eslintrc.js',
        task: async () => {
            const globals = {cy: true};
            await (new EslintConfigModuleEditor())
                .extend({globals})
                .commit();
        },
        condition: () => allDoExist('.eslintrc.js')
    },
    {
        text: 'Copy Cypress files',
        task: async ({overwrite}) => {
            await (new Scaffolder(join(__dirname, 'templates')))
                .overwrite(overwrite)
                .target('cypress/plugins')
                .copy('plugins/index.js', 'index.js')
                .target('cypress/support')
                .copy('support/index.js', 'index.js')
                .copy('support/commands.js', 'commands.js')
                .target('cypress/integration')
                .copy('visual-regression.test.js')
                .commit();
        },
        condition: ALWAYS
    },
    {
        text: 'Install Cypress dependencies',
        task: ({skipInstall}) => install([...CYPRESS_DEPENDENCIES, 'npm-run-all', 'del-cli'], {dev: true, skipInstall}),
        condition: ({skipInstall}) => !skipInstall && allDoExist('package.json')
    }
];
export const removeCypress = [
    {
        text: 'Delete Cypress config file',
        task: async () => {
            const Editor = createJsonEditor('cypress.json');
            await (new Editor())
                .delete()
                .commit();
        },
        condition: () => allDoExist('cypress.json')
    },
    {
        text: 'Remove Cypress test tasks from package.json',
        task: async () => {
            const scripts = {
                'cy:open': undefined,
                'cy:run': undefined,
                'cy:update': undefined,
                'test:visual': undefined,
                'test:visual:update': undefined,
                'test:visual:ci': undefined
            };
            await (new PackageJsonEditor())
                .extend({scripts})
                .commit();
        },
        condition: () => allDoExist('package.json')
    },
    {
        text: 'Remove Cypress global variable from .eslintrc.js',
        task: async () => {
            const globals = {cy: false};
            await (new EslintConfigModuleEditor())
                .extend({globals})
                .commit();
        },
        condition: () => allDoExist('.eslintrc.js')
    },
    {
        text: 'Uninstall Cypress dependencies',
        task: () => uninstall(CYPRESS_DEPENDENCIES),
        condition: () => allDoExist('package.json') && (new PackageJsonEditor()).hasAll(...CYPRESS_DEPENDENCIES)
    }
];
export default addCypress;