Mikhail Bolotov
Mikhail Bolotov

Reputation: 1104

Evaluate a Cypress configuration file

In my build scripts I need to evaluate a Cypress configuration file. I'm using the following script:

let appdata = process.env.LOCALAPPDATA;
let version = `11.0.1`;
let src = `${appdata}/Cypress/Cache/${version}/Cypress/resources/app/node_modules/@packages/data-context/src`;
const DataContext = require(`${src}/DataContext.js`).DataContext;
const ProjectConfigManager  = require(`${src}/data/ProjectConfigManager.js`).ProjectConfigManager;

(async() => {
  const ctx = new DataContext({
      schema: null,
      schemaCloud: null,
      modeOptions: "run",
      appApi: {},
      localSettingsApi: {},
      authApi: {

      } ,
      configApi: {
      },
      projectApi: {

      } ,
      electronApi: {
      } ,
      browserApi: {

      },
    })

  let configManager = new ProjectConfigManager({
       ctx,
       configFile: 'C:\\work\\sample\\sample.config.ts',
       projectRoot: 'C:\\work\\sample',
       handlers: [],
       hasCypressEnvFile: false,
       eventRegistrar: null/*new EventRegistrar()*/,
       onError: (error) => {},
       onInitialConfigLoaded: () => {},
       onFinalConfigLoaded: () => Promise.resolve(),
       refreshLifecycle: () => Promise.resolve(),
     })

  configManager.configFilePath = "sample.config.ts"
  configManager.setTestingType('e2e')

  let cfg = await configManager.getConfigFileContents()

  console.log(JSON.stringify(cfg));

})();

It works well for Cypress 10 version. However, Cypress 11 has introduced some changes that break this script. Though I adjusted the paths, I'm still unable to make it work again. It currently fails with this error:

Error: Cannot find module 'C:\Users\mbolotov\AppData\Local\Cypress\Cache\11.0.1\Cypress\resources\app\node_modules\graphql\index'. Please verify that the package.json has a valid "main" entry

How can I fix this problem (without making changes to the Cypress installation)?

OR

Is there any other way to evaluate a Cypress configuration file (say from the command line) and obtain its values?

Upvotes: 0

Views: 388

Answers (2)

Paolo
Paolo

Reputation: 5451

The exact usage is unclear to me, but making some assumptions - a nodejs script in the /scripts folder of the project can compile and resolve the config using the Cypress module API.

It would need a test to run, a "null-test" can be generated from inside the script.

Note, the null-test must conform to the spec pattern of the project (below it's the std .cy.js)

const cypress = require('cypress')
const fs = require('fs')

fs.writeFileSync('../cypress/e2e/null-test.cy.js', 'it("", ()=>{})')

cypress.run({
  project: '..',
  spec: '../cypress/e2e/null-test.cy.js',
  quiet: true
}).then(results => {
  if (results.status === 'failed') {
    console.log(results)
  } else {
    console.log(results.config.resolved)      // output resolved config
  }
})

I haven't attempted to duplicate everything you have in your code, as it's using Cypress internals and not publicly documented.

Upvotes: 5

Fody
Fody

Reputation: 31944

This may be because of Changelog 11.0.0

We have also massively improved our startup performance by shipping a snapshot of our binary instead of the source files.

Looking inside the cache folder for v10.11.0 (${process.env.CYPRESS_CACHE_FOLDER}/10.11.0/Cypress/resources/app), the /node_modules is fully populated and /node_modules/graphql/index.js exists.

But in v11.0.1 /node_modules/graphql is not fully populated.

Upvotes: 4

Related Questions