Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Cypress测试规范文件中导出可重用Javascript函数_Javascript_Cypress - Fatal编程技术网

在Cypress测试规范文件中导出可重用Javascript函数

在Cypress测试规范文件中导出可重用Javascript函数,javascript,cypress,Javascript,Cypress,我试图在我的Cypress规范文件中包含一个可重用组件,该文件由我们的系统“服务”组织。有一些代码片段我想作为其他JS文件的组件提供。 然而,我发现'import'语句导致运行B.js的测试用例,我无法找到避免运行'testcaseb'的方法。 我知道Cypress中有自定义命令,但在我的例子中,我想使用纯JS来组织我的组件。多谢各位 A.js import {functionInB as helloB } from "./B" describe(`A`, () =>

我试图在我的Cypress规范文件中包含一个可重用组件,该文件由我们的系统“服务”组织。有一些代码片段我想作为其他JS文件的组件提供。 然而,我发现'import'语句导致运行B.js的测试用例,我无法找到避免运行'testcaseb'的方法。 我知道Cypress中有自定义命令,但在我的例子中,我想使用纯JS来组织我的组件。多谢各位

A.js

import {functionInB as helloB } from "./B"

describe(`A`, () => {
    it(`01 Testcase A`, () => {
      let result = helloB()
      console.log(result)
    })
});
describe(`B`, () => {
  it(`01 Testcase B`, () => {
        //let result = Hello2()
        console.log("inside hello B")
    })
});

export function functionInB(){
  return "Do something in functionInB"
}

B.js

import {functionInB as helloB } from "./B"

describe(`A`, () => {
    it(`01 Testcase A`, () => {
      let result = helloB()
      console.log(result)
    })
});
describe(`B`, () => {
  it(`01 Testcase B`, () => {
        //let result = Hello2()
        console.log("inside hello B")
    })
});

export function functionInB(){
  return "Do something in functionInB"
}


似乎没有办法停止导入时运行的脚本(或
require()
或dynamic
import()

在Python中,main()用于确定是否在“顶层”调用模块

如果名称=“\uuuuu main\uuuuuuuu”:
#仅当作为脚本运行时执行
main()
这有点骇人听闻,但是您可以使用
Cypress.spec.name
来做同样的事情,并且只在B的代码是当前规范时运行它

if(Cypress.spec.name===“B.js”){
描述(`B`,()=>{
它(`01 Testcase B`,()=>{
//让结果=Hello2()
log(“在hellob内部”)
})
});
}
导出函数functionInB(){
返回“在functionInB中执行某些操作”
}


“javascript方法”是将
functionInB()
移动到一个实用程序文件中,并在其中导入两个测试,但我想你已经知道了。

你能详细说明一下这一行吗?不过,我发现“import”语句会导致运行B.js的测试用例,我无法找到一种方法来避免“TestCaseB”的运行。我只是添加了一张图片来说明。当我在Cypress中运行A.js时,我不希望运行来自B.js的测试用例。我相信Cypress函数在导入时是某种“刚刚运行”的。看起来,“/B”中的“import{functionInB as helloB}”不仅仅是导入函数“helloB”,而是导入整个文件。