【问题标题】:JavaScript Code Optimization: how to add common code with every method in .js fileJavaScript 代码优化:如何为 .js 文件中的每个方法添加通用代码
【发布时间】:2020-08-28 05:05:20
【问题描述】:

对不起!我想不出一个更好的标题,对 javascript 世界来说是新的。

我有一个abc.js 文件,并且想在每个it 方法中添加TestFilter。目前,我正在使用下面显示的方法。而不是在每个前面写TestFilterit 方法有没有优化的方法来实现这一点?请推荐

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;

【问题讨论】:

  • 请发布您希望代码显示方式的示例。
  • 您的 TestFilter 调用提供了不同的过滤器值 - 否则您将如何表达每个测试的过滤器规则?
  • 另外,这是什么测试框架或库?
  • @Dai 这是 cypress 框架。在运行时根据通过 cmd 提供的标签进行过滤。为了更好地理解,我添加了 TestFilter.js 文件。
  • @Dai 我只希望代码以更统一的方式呈现,因此我不必在每个 it 方法中添加 TestFilter。我想过使用装饰器,但不知道该怎么做。

标签: javascript node.js cypress


【解决方案1】:

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

这是我的(轻微的)优化。

测试

 /* 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;

【讨论】:

  • 一个问题是缺少it.only()it.skip(),您的语法可以处理。
  • 我认为您的回答也可能如此。只需在标签数组/描述中添加仅/跳过标签,如果标签数组/描述只有/跳过,则检查 itByTg 方法。然后使用 if else 调用 it.only 或 it.skip
  • 我的偏好是将标签作为标签,将描述作为描述,事实上.only().skip()应该遵循标准it使用的约定。
【解决方案2】:

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

【讨论】:

  • Cypress 支持 mocha hook。我已经发布了上面的示例代码,你能告诉我钩子在这种情况下如何提供帮助吗?
猜你喜欢
  • 2015-09-19
  • 2014-02-13
  • 2017-07-13
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多