Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

javascript - Can Cypress Recognize Default Export Functions?

It seems that Cypress may not correctly recognize functions that are default exports. Example:

export default function (array) {
  const randomValue = array[Math.floor(Math.random() * array.length)];
  return randomValue;
}

import randomValueFromArray from '../../../../../support/index';

randomValueFromArray([1, 2]).then((value) => {
    cy.log('VALUE', value);
  });

Cypress runner error: (0 , _index.default) is not a function

question from:https://stackoverflow.com/questions/65838133/can-cypress-recognize-default-export-functions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In cypress you can add this function as custom commands in support/commands.js file:

Cypress.Commands.add("randomValueFromArray", (array) => { return array[Math.floor(Math.random() * array.length)]; })

& Use it the spec make sure to add import './commands' in support/index.js

   cy.randomValueFromArray([1,2,5,6,3,8,9,2,5,7]).then((value) => {
        cy.log('VALUE', value);
      })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...