Why are Run test gutter icons disappearing in IntelliJ IDEA when using a custom Mocha interface with additional parameter?

huangapple go评论71阅读模式
英文:

Why are Run test gutter icons disappearing in IntelliJ IDEA when using a custom Mocha interface with additional parameter?

问题

I understand your request. Here's the translated content:

我需要您的帮助,关于使用IntelliJ IDEA开发我们的自动化测试时遇到的一个问题。

我们需要创建自己的Mocha自定义接口(https://github.com/mochajs/mocha/wiki/Third-party-UIs),基本上只是为BDD接口的'it'和'describe'添加一个新参数,如下所示:

  • describe("Suite title", async () => { ... }); -> describe("Suite title", async () => { ... }, "Additional parameter");
  • it("Test title", async () => { ... }); -> it("Test title", async () => { ... }, "Additional parameter");

在测试文件中,如果我们为套件使用这个额外的参数,运行测试的图标会消失(包括测试),我们无法找到任何解决方案来修复它。
注意:有趣的是,当我们从套件中删除这个额外的参数,但保留它用于测试时,运行测试的图标会显示。

Why are Run test gutter icons disappearing in IntelliJ IDEA when using a custom Mocha interface with additional parameter?

您有任何想法出了什么可能出错了吗?我们在实现中漏掉了什么吗?或者我们需要在IDEA中进行额外的配置吗?

我附上了截图,您可以在其中看到IDEA在这两种情况下的行为。

下面,您可以看到我们简化的Mocha自定义接口的实现,可以用于复现这个问题。

CustomInterface.ts:

import { Func, AsyncFunc, Suite, Test } from "mocha";
import * as Common from "mocha/lib/interfaces/common";
import * as Mocha from "mocha";

declare module "mocha" {
  namespace interfaces {
    function myCustomInterface(suite: Mocha.Suite): void;
  }

  export interface SuiteFunction {
    (title: string, fn: (this: Suite) => void, myNewParam?: string): Suite;
  }

  export interface TestFunction {
    (title: string, fn?: AsyncFunc, myNewParam?: string): Test;
  }
}

module.exports = Mocha.interfaces.myCustomInterface = function (suite) {
  const suites = [suite];

  suite.on("pre-require", function (context, file, mocha) {
    const common = Common(suites, context, mocha);

    context.before = common.before;
    context.after = common.after;
    context.beforeEach = common.beforeEach;
    context.afterEach = common.afterEach;

    context.run = mocha.options.delay && common.runWithSuite(suite);

    (context.describe as any) = function (
      title: string,
      fn: (this: Suite) => void,
      myNewParam: string
    ) {
      console.log("Suite new param: ", myNewParam);
      return common.suite.create({
        title,
        file,
        fn,
      });
    };

    (context.it as any) = function (
      title: string,
      fn: Func | AsyncFunc,
      myNewParam: string
    ) {
      console.log("Test new param: ", myNewParam);
      const suite = suites[0];
      if (suite.isPending()) {
        fn = null;
      }

      const test = new Test(title, fn);

      test.file = file;
      suite.addTest(test);
      return test;
    };
  });
};

myTest.spec.ts:

// 在IDEA中正确显示运行测试图标
describe("Suite without annotation", () => {
  // 显示运行测试图标
  it(
    "Test with annotation",
    async () => {
      // 显示运行测试图标
      console.log("");
    },
    "This is *test* new param"
  );
});

// 在IDEA中不显示运行测试图标
describe(
  "Suite with annotation",
  () => {
    // 不显示运行测试图标
    it(
      "Test with annotation",
      async () => {
        // 不显示运行测试图标
        console.log("");
      },
      "This is *test* new param"
    );
  },
    "This is *suite* new param"
);

package.json:

{
  "name": "tests",
  "description": "ui tests",
  "main": "index.js",
  "devDependencies": {
    "@types/node": "^14.6.4",
    "@types/mocha": "^8.0.3",
    "typescript": "4.5.4",
    "mocha": "^10.1.0",
    "mocha-multi-reporters": "^1.5.1",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "^4.1.2"
  },
  "dependencies": {},
  "author": "Oracle/NetSuite",
  "license": "ISC",
  "scripts": {
    "test": "npx mocha --require ts-node/register --require tsconfig-paths/register -u ./CustomInterface.ts ./tests/*.spec.ts"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2018",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "baseUrl": ".",
    "skipLibCheck": true
  },
  "include": ["./**/*.ts"]
}

If you have any specific questions or need further assistance, please let me know.

英文:

I would need your help regarding one issue which we are facing when using IntelliJ IDEA for the developing of our automation tests.

We needed to create our own Mocha custom interface (https://github.com/mochajs/mocha/wiki/Third-party-UIs) where we basically just adding a new parameter to BDD interface for 'it' and for 'describe', see below

  • describe( "Suite title", async () => { ... }); -> describe( "Suite title", async () => { ... }, "Additional parameter");
  • it( "Test title", async () => { ... }); -> it( "Test title", async () => { ... }, "Additional parameter");

In the test file, if we use this additional parameter for the suite, Run test gutter icons disappear (include for tests) and we are unable to find any solution how to fix.
Note: It is curious that when we remove this additional parameter from the suite but keeping it for tests, then Run test gutter icons are displayed.

Why are Run test gutter icons disappearing in IntelliJ IDEA when using a custom Mocha interface with additional parameter?

Do you have any idea what could be wrong? Are we missing something in the implementation? Or do we need some extra configuration in IDEA?

I attached the screenshot where you can see how IDEA behaves in these two situations.

Below, you can see our simplified implementation of our Mocha custom interface which can be used for reproducing of the issue.

CustomInterface.ts:

import { Func, AsyncFunc, Suite, Test } from "mocha";
import * as Common from "mocha/lib/interfaces/common";
import * as Mocha from "mocha";
declare module "mocha" {
namespace interfaces {
function myCustomInterface(suite: Mocha.Suite): void;
}
export interface SuiteFunction {
(title: string, fn: (this: Suite) => void, myNewParam?: string): Suite;
}
export interface TestFunction {
(title: string, fn?: AsyncFunc, myNewParam?: string): Test;
}
}
module.exports = Mocha.interfaces.myCustomInterface = function (suite) {
const suites = [suite];
suite.on("pre-require", function (context, file, mocha) {
const common = Common(suites, context, mocha);
context.before = common.before;
context.after = common.after;
context.beforeEach = common.beforeEach;
context.afterEach = common.afterEach;
context.run = mocha.options.delay && common.runWithSuite(suite);
(context.describe as any) = function (
title: string,
fn: (this: Suite) => void,
myNewParam: string
) {
console.log("Suite new param: ", myNewParam);
return common.suite.create({
title,
file,
fn,
});
};
(context.it as any) = function (
title: string,
fn: Func | AsyncFunc,
myNewParam: string
) {
console.log("Test new param: ", myNewParam);
const suite = suites[0];
if (suite.isPending()) {
fn = null;
}
const test = new Test(title, fn);
test.file = file;
suite.addTest(test);
return test;
};
});
};

myTest.spec.ts

// Run test gutter icons are correctly displayed in IDEA
describe("Suite without annotation", () => {
// Run test gutter icon IS displayed
it(
"Test with annotation",
async () => {
// Run test gutter icon IS displayed
console.log("");
},
"This is *test* new param"
);
});
// Run test gutter icons are NOT displayed in IDEA
describe(
"Suite with annotation",
() => {
// Run test gutter icon IS NOT displayed
it(
"Test with annotation",
async () => {
// Run test gutter icon IS NOT displayed
console.log("");
},
"This is *test* new param"
);
},
"This is *suite* new param"
);

package.json

{
"name": "tests",
"description": "ui tests",
"main": "index.js",
"devDependencies": {
"@types/node": "^14.6.4",
"@types/mocha": "^8.0.3",
"typescript": "4.5.4",
"mocha": "^10.1.0",
"mocha-multi-reporters": "^1.5.1",
"ts-node": "^10.0.0",
"tsconfig-paths": "^4.1.2"
},
"dependencies": {},
"author": "Oracle/NetSuite",
"license": "ISC",
"scripts": {
"test": "npx mocha --require ts-node/register --require tsconfig-paths/register -u ./CustomInterface.ts ./tests/*.spec.ts"
}
}

tsconfig.json

{
"compilerOptions": {
"module": "commonjs",
"target": "es2018",
"noImplicitAny": true,
"moduleResolution": "node",
"baseUrl": ".",
"skipLibCheck": true
},
"include": ["./**/*.ts"]
}

答案1

得分: 0

IDE 在静态分析源代码时寻找已知的代码模式,用于检测测试套件时。由于您自定义了界面,添加了更多参数到标准函数中,它们未被识别为测试套件/测试。不幸的是,目前没有解决方法。

我们有一个特性请求,提供自定义测试/套件名称的支持,WEB-37848,请随时投票支持。

关于 Mocha 自定义界面的问题已记录为 WEB-61278

英文:

the IDE is statically analyzing the source code looking for known code patterns when detecting the tests/suits. As you have customized the interface by adding more parameters to standard functions, they are not recognized as test suits/tests. There are no workarounds unfortunately.

We have a feature request for providing support for custom test/suite names, WEB-37848, please feel free to vote for it.
The problem with Mocha custom interfaces is logged as WEB-61278

huangapple
  • 本文由 发表于 2023年5月30日 00:17:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定