CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

`process.env.profile` is the string "undefined" when no --profile is specified

Open dantman opened this issue 2 years ago • 0 comments

What are you trying to achieve?

I wanted to control what browser is run using --profile and fallback to chrome if --profile was not provided.

What do you get instead?

I got errors like "Error: Cannot find the browser. "undefined:headless" is neither a known browser alias, nor a path to an executable file.".

I tried debugging by putting console.log(process.env.profile, typeof process.env.profile); in my codecept.conf.js and got "undefined string".

Details

  • CodeceptJS version: 3.3.3
  • NodeJS Version: v16.15.1
  • Operating System:
  • puppeteer || webdriverio || testcafe version (if related): [email protected]
  • Configuration file:
require('ts-node/register');
const {
  setHeadlessWhen,
  setBrowser,
  setTestHost,
} = require('@codeceptjs/configure');

console.log(process.env.profile, typeof process.env.profile);
setHeadlessWhen(process.env.CI);
setBrowser(process.env.profile || 'chrome');
setTestHost(process.env.TEST_HOST);

exports.config = {
  tests: './tests/e2e/codecept/features/*.ts',
  output: './tests/e2e/codecept/output',
  helpers: {
    TestCafe: {
      url: process.env.BASE_URL || 'http://localhost:3000',
    },
  },
  include: {
    // I: './tests/e2e/codecept/custom-steps.ts',
  },
  bootstrap: null,
  mocha: {},
  name: 'seymour-portal',
};

Investigation

I did a bit of debugging and it looks like the issue is the way that the run command (and other commands) set the profile.

https://github.com/codeceptjs/CodeceptJS/blob/58bf5d907a20e30075ddeaaac5c8df94f986cd9f/lib/command/run.js#L8-L11

It appears that when you assign to process.env.profile Node stringifies the value. So if options.profile is undefined then it gets stringified to "undefined" instead of resulting in the env not being set.

dantman avatar Jun 18 '22 00:06 dantman