bahmutov / cypress-select-tests

User space solution for picking Cypress tests to run
97 stars 14 forks source link

SyntaxError: Unexpected token is thrown while combining the custom browserify with grep picker #34

Open siluo opened 4 years ago

siluo commented 4 years ago

Hey,

I updated the latest cypress-select-tests package, and now my plugins/index.js file is(I assume this is the only place i need to update):

/* eslint-disable no-console */
const browserify = require('@cypress/browserify-preprocessor');
// utility function to process source in browserify
const itify = require('cypress-select-tests/src/itify');
// actual picking tests based on environment variables in the config file
const { grepPickTests } = require('cypress-select-tests/src/grep-pick-tests');
const path = require('path');
const fs = require('fs');
const { get, merge } = require('lodash');

// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

module.exports = (on, config) => {
    let customBrowserify;

    // we need custom browserify transformation
    on('before:browser:launch', (browser = {}) => {
        const options = browserify.defaultOptions;
        const envPreset = options.browserifyOptions.transform[1][1].presets[0];
        options.browserifyOptions.transform[1][1].presets[0] = [
            envPreset,
            {
                ignoreBrowserslistConfig: true,
                targets: { [browser.name]: browser.majorVersion }
            }
        ];

        // notice how we add OUR select tests transform to the list of browserify options
        options.browserifyOptions.transform.push(itify(config, grepPickTests));
        customBrowserify = browserify(options);
    });

    on('file:preprocessor', file => customBrowserify(file));

    const env = get(config, ['env', 'myEnv'], 'local');
    let newConfig = config;
    const envConfigPath = path.resolve(__dirname, '../config/', `${env}.json`);
    if (fs.existsSync(envConfigPath)) {
        // eslint-disable-next-line global-require,import/no-dynamic-require
        const envConfig = require(envConfigPath);
        console.log(`Using ${env} env config:`, JSON.stringify(envConfig, null, 2));
        newConfig = merge(config, envConfig);
    } else {
        console.error('ERROR: Could not find config file:', envConfigPath);
    }

    console.log('CONFIG:', JSON.stringify(newConfig, null, 2));
    return newConfig;
};

However after running the command:

DEBUG=cypress-select-tests npm run test:cypress:vp:tag

(test:cypress:vp:tag is defined in package.json file:

"test:cypress:vp:tag": "cypress run --env grep=xyz,myEnv=${ENV:-qa},useTool=false")

I got the below error:

The following error was thrown by a plugin. We've stopped running your tests because a plugin crashed.

SyntaxError: Unexpected token (2:10)
    at Parser.pp$4.raise (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/acorn/dist/acorn.js:2757:13)
    at Parser.pp.unexpected (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/acorn/dist/acorn.js:647:8)
    at Parser.pp.semicolon (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/acorn/dist/acorn.js:624:64)
    at Parser.pp$1.parseExpressionStatement (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/acorn/dist/acorn.js:1093:8)
    at Parser.pp$1.parseStatement (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/acorn/dist/acorn.js:818:24)
    at Parser.pp$1.parseBlock (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/acorn/dist/acorn.js:1112:23)
    at Parser.pp$1.parseStatement (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/acorn/dist/acorn.js:791:34)
    at Parser.pp$1.parseTopLevel (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/acorn/dist/acorn.js:706:23)
    at Parser.parse (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/acorn/dist/acorn.js:551:15)
    at parse (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/acorn/dist/acorn.js:5288:37)
    at module.exports (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/node_modules/falafel/index.js:22:15)
    at Object.findTests (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/src/spec-parser.js:95:3)
    at process (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/src/itify.js:18:33)
    at Stream.onend (/Users/ss/IdeaProjects/tower/service/node_modules/cypress-select-tests/src/itify.js:52:18)
    at _end (/Users/ss/IdeaProjects/tower/service/node_modules/through/index.js:65:9)
    at Stream.stream.end (/Users/ss/IdeaProjects/tower/service/node_modules/through/index.js:74:5)
    at DestroyableTransform.onend (/Users/ss/IdeaProjects/tower/service/node_modules/readable-stream/lib/_stream_readable.js:577:10)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
    at DestroyableTransform.emit (events.js:208:7)
    at endReadableNT (/Users/ss/IdeaProjects/tower/service/node_modules/readable-stream/lib/_stream_readable.js:1010:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Could you please help me point out why it throws error?

Thanks.

bahmutov commented 4 years ago

there is probably some es6 syntax there in the spec file that breaks the current parser

bahmutov commented 4 years ago

try commenting out code in your spec file until you find what syntax breaks it. Also to fix this I would need to know that syntax to test against it

siluo commented 4 years ago

try commenting out code in your spec file until you find what syntax breaks it. Also to fix this I would need to know that syntax to test against it

@bahmutov i tried to comment out all codes line by line except those lines with "const" like:

const userFile = require('../../fixtures/test_user_datafile');

including it the test failed at the unexpected token as above, but without including it the test passed by skipping all codes.

and I think you are right that it is some es6 syntax causing the issue.

siluo commented 4 years ago

Btw, i tried to replace the only one line code in my spec file const userFile = require('../../fixtures/test_user_datafile') to var userFile = require('../../fixtures/test_user_datafile') I still got the same error. Not sure if it helps our debug.