Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cy.request does not work from Cypress plugin file?

Tags:

cypress

 return cy.request('https://webiste.com/config.json').then( (appConfig) => {
  // save to Cypress.config
 });

The above code threw this error:

The function exported by the plugins file threw an error.

We invoked the function exported by 'C:\projects\nfe-credit-flow\cypress\plugins\index.js', but it threw an error.

The following error was thrown:

ReferenceError: cy is not defined at Promise.then (C:\projects\nfe-credit-flow\cypress\plugins\index.js:74:4) at tryCatcher (C:\projects\nfe-credit-flow\node_modules\bluebird\js\release\util.js:16:23) at Promise._settlePromiseFromHandler (C:\projects\nfe-credit-flow\node_modules\bluebird\js\release\promise.js:512:31) at Promise._settlePromise (C:\projects\nfe-credit-flow\node_modules\bluebird\js\release\promise.js:569:18) at Promise._settlePromise0 (C:\projects\nfe-credit-flow\node_modules\bluebird\js\release\promise.js:614:10) at Promise._settlePromises (C:\projects\nfe-credit-flow\node_modules\bluebird\js\release\promise.js:693:18)

like image 899
crs Avatar asked Nov 23 '18 10:11

crs


1 Answers

Plugins run a node.js task while cypress runs in the browser. You should use an npm package such as request to do this. The config variable is accessible from the plugin e.g. module.exports = (on, config) => { ...

Since cypress uses request, it's a good idea to have the same dependency rather than a new one like axios. So just npm i request than:

const request = require('request');
request('https://webiste.com/config.json').then( (appConfig) => {
  config.whatever = appConfig.whatever
 });

Or you could use a merge function (e.g. from loadsh) to override config.

like image 152
Moshisho Avatar answered Sep 29 '22 04:09

Moshisho