Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.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
JavaScript代码优化:如何为.js文件中的每个方法添加公共代码_Javascript_Node.js_Cypress - Fatal编程技术网

JavaScript代码优化:如何为.js文件中的每个方法添加公共代码

JavaScript代码优化:如何为.js文件中的每个方法添加公共代码,javascript,node.js,cypress,Javascript,Node.js,Cypress,对不起!我想不出比这更好的标题了,这是javascript领域的新事物。 我有一个abc.js文件,想用每个it方法添加TestFilter。目前,我正在使用下面显示的方法。与其在每个,it方法前面写TestFilter,还有什么优化的方法来实现这一点吗?请建议 import TestFilter from '../support/TestFilter'; describe('Test A', () => { TestFilter(['smoke'],()=>it.on

对不起!我想不出比这更好的标题了,这是javascript领域的新事物。

我有一个
abc.js
文件,想用每个
it
方法添加
TestFilter
。目前,我正在使用下面显示的方法。与其在每个,
it
方法前面写
TestFilter
,还有什么优化的方法来实现这一点吗?请建议

import TestFilter from '../support/TestFilter';

 describe('Test A', () => {

    TestFilter(['smoke'],()=>it.only('should run test A successfully', () => {
      expect(1 + 1).to.be.equal(2);
    }));

    TestFilter(['regression'],()=> it('should run test A successfully', () => {
      expect(1 + 1).to.be.equal(2);
    }));
  });
TestFilter.js

const TestFilter = (definedTags, runTest) => {
  if (Cypress.env('testtag')) {
    const tags = Cypress.env('testtag').split(',');
    const isFound = definedTags.some((definedTag) => tags.includes(definedTag));
  
    if (isFound) {
      runTest();
    }
  }else{
    runTest();
  }
  
};

export default TestFilter;

如果您希望在每次测试之前运行某段代码,可以在mocha中尝试“beforeach”钩子。虽然我不确定cypress是否支持mocha钩子。

如果您希望在每次测试之前运行某段代码,可以在mocha中尝试“beforeach”钩子。尽管我不确定cypress是否支持mocha挂钩。

有选择地运行测试的有趣想法,这很难做到

这是我的(轻微)优化

测试

 /* For checking out the extension - normally set externally */
 Cypress.env('testTags', 'regression')
 // Cypress.env('testTags', 'smoke')
 // Cypress.env('testTags', 'smoke,regression')
 // Cypress.env('testTags', null)

 itByTag(['smoke'], 'should run with SMOKE tag', () => {
   expect(1 + 1).to.be.equal(2);
 });

 itByTag(['regression'], 'should run with REGRESSION tag #1', () => {
   expect(1 + 1).to.be.equal(2);
 });

 itByTag.only(['regression'], 'should run with REGRESSION tag #2', () => {
   expect(1 + 1).to.be.equal(2);
 });

 itByTag(['smoke', 'regression'], 'should run with SMOKE and REGRESSION tags', () => {
   expect(1 + 1).to.be.equal(2);
 });
itByTag.js支持文件

const itByTag = (tags, description, callback) => {
  _itByTag(tags, description, callback)
};

// Adding 'only' extension
itByTag.only = (tags, description, callback) => {
  _itByTag(tags, description, callback, 'only')
};

// Adding 'skip' extension
itByTag.skip = (tags, description, callback) => {
  _itByTag(tags, description, callback, 'skip')
};

// internal implementation
const _itByTag = (tags, description, callback, onlyOrSkip) => {
  const tagsToRun = Cypress.env('testTags');
  const shouldRun = !tagsToRun || 
    tags.some(tag => tagsToRun.split(',').includes(tag));
  if (shouldRun) {
    const itFn = onlyOrSkip ? it[onlyOrSkip] : it;
    itFn(description, callback)
  }
};

export default itByTag;

有选择地运行测试的有趣想法,这很难做到

这是我的(轻微)优化

测试

 /* For checking out the extension - normally set externally */
 Cypress.env('testTags', 'regression')
 // Cypress.env('testTags', 'smoke')
 // Cypress.env('testTags', 'smoke,regression')
 // Cypress.env('testTags', null)

 itByTag(['smoke'], 'should run with SMOKE tag', () => {
   expect(1 + 1).to.be.equal(2);
 });

 itByTag(['regression'], 'should run with REGRESSION tag #1', () => {
   expect(1 + 1).to.be.equal(2);
 });

 itByTag.only(['regression'], 'should run with REGRESSION tag #2', () => {
   expect(1 + 1).to.be.equal(2);
 });

 itByTag(['smoke', 'regression'], 'should run with SMOKE and REGRESSION tags', () => {
   expect(1 + 1).to.be.equal(2);
 });
itByTag.js支持文件

const itByTag = (tags, description, callback) => {
  _itByTag(tags, description, callback)
};

// Adding 'only' extension
itByTag.only = (tags, description, callback) => {
  _itByTag(tags, description, callback, 'only')
};

// Adding 'skip' extension
itByTag.skip = (tags, description, callback) => {
  _itByTag(tags, description, callback, 'skip')
};

// internal implementation
const _itByTag = (tags, description, callback, onlyOrSkip) => {
  const tagsToRun = Cypress.env('testTags');
  const shouldRun = !tagsToRun || 
    tags.some(tag => tagsToRun.split(',').includes(tag));
  if (shouldRun) {
    const itFn = onlyOrSkip ? it[onlyOrSkip] : it;
    itFn(description, callback)
  }
};

export default itByTag;


请举例说明您希望代码的显示方式。您的
TestFilter
调用提供了不同的筛选值-否则您将如何表示每个测试的筛选规则?此外,这是什么测试框架或库?@Dai这是cypress framework。在运行时通过cmd根据提供的标记进行过滤。为了更好地理解,我添加了TestFilter.js文件。@Dai我只希望代码以更统一的方式进行,这样我就不必在每个it方法中都添加TestFilter。我曾想过使用decorator,但不知道如何使用。请发布一个示例,说明您希望代码如何显示。您的
TestFilter
调用提供了不同的筛选值-否则,您将如何表示每个测试的筛选规则?还有,这是什么测试框架或库?@Dai这是cypress framework。在运行时通过cmd根据提供的标记进行过滤。为了更好地理解,我添加了TestFilter.js文件。@Dai我只希望代码以更统一的方式进行,这样我就不必在每个it方法中都添加TestFilter。我曾想过使用decorator,但不知道怎么做。Cypress支持摩卡挂钩。我已经发布了上面的示例代码,你能告诉我钩子在这个场景中有什么帮助吗?Cypress支持mocha钩子。我已经发布了上面的示例代码,您能告诉我钩子在这个场景中有什么帮助吗?一个问题是缺少
it.only()
it.skip()
,这是您的语法可以处理的。我想在您的回答中也有可能。只需在tag array/description中添加only/skip标记,如果tag array/description只有/skip,则在itByTg方法中进行检查。然后使用if else调用它.only或it.skipm我的首选是将标记保留为标记,将描述保留为描述,事实上
。only()
。skip()
应该遵循标准
it
使用的惯例。其中一个问题是缺少
it.only()
it.skip()
,这是你的语法可以处理的。我想你的答案中也有这个可能。只需在tag array/description中添加only/skip标记,如果tag array/description只有/skip,则在itByTg方法中进行检查。然后使用if else调用它.only或it.skipm我的首选方法是将标记保留为标记,将描述保留为描述,事实上
。only()
。skip()
应遵循标准
it
使用的约定。