Cypress 能否识别默认导出函数?

Can Cypress Recognize Default Export Functions?

看来 Cypress 可能无法正确识别默认导出的函数。示例:

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);
  });

赛普拉斯转轮错误: (0 , _index.default) is not a function

在 cypress 中,您可以将此函数作为自定义命令添加到 support/commands.js 文件中:

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

& 使用规范确保在 support/index.js

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

能够让它以这种方式工作:

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


module.exports = {
  randomValueFromArray,
};

然后导入并调用它:

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

let warehouse;

  warehouse = randomValueFromArray([1, 2]);