为什么在IntelliJ IDEA中使用带有附加参数的自定义Mocha界面时,Run test gutter图标会消失?

pepwfjgg  于 2023-06-05  发布在  IntelliJ IDEA
关注(0)|答案(1)|浏览(267)

我需要你的帮助,关于我们在使用IntelliJ IDEA开发自动化测试时面临的一个问题。
我们需要创建我们自己的Mocha自定义接口(https://github.com/mochajs/mocha/wiki/Third-party-UIs),我们基本上只是为'it'和'describe'添加一个新的参数到BDD接口,见下文

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

在测试文件中,如果我们为套件使用此附加参数,则Run test gutter图标消失(包括测试),并且我们无法找到任何解决方案。注意:奇怪的是,当我们从套件中删除这个附加参数,但保留它用于测试时,会显示Run test gutter图标。

你知道会出什么问题吗?我们在实施过程中是否遗漏了什么?或者我们需要在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

// 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"]
}
ljsrvy3e

ljsrvy3e1#

IDE在检测测试/套装时静态地分析源代码以寻找已知的代码模式。由于您已通过向标准函数添加更多参数来自定义接口,因此这些参数不会被识别为测试套/测试。不幸的是,没有解决方法。
我们有一个功能请求,要求提供对自定义测试/套件名称WEB-37848的支持,请随时投票支持。Mocha自定义接口的问题记录为WEB-61278

相关问题