网站搜索

javascript - 无法在 cucumber-js 的 before 钩子中返回


目前我正在尝试根据提供的标签在 cucumber-js 中跳过测试。查看他们的文档,它似乎相当直接,以下内容应该有效。

Before({ tags: '@skip' }, async function() {
  return 'skip'
})

但当我这样做时,我收到以下错误:

TSError: ⨯ Unable to compile TypeScript:
e2e/hooks/hooks.ts(36,1): error TS2769: No overload matches this call.
  Overload 1 of 3, '(tags: string, code: TestCaseHookFunction): void', gave the following error.
    Argument of type '{ tags: string; }' is not assignable to parameter of type 'string'.
  Overload 2 of 3, '(options: IDefineTestCaseHookOptions, code: TestCaseHookFunction): void', gave the following error.
    Argument of type '() => Promise<string>' is not assignable to parameter of type 'TestCaseHookFunction'.
      Type '() => Promise<string>' is not assignable to type 'TestCaseHookFunctionWithoutParameter'.
        Type 'Promise<string>' is not assignable to type 'void | Promise<void>'.
          Type 'Promise<string>' is not assignable to type 'Promise<void>'.
            Type 'string' is not assignable to type 'void'.

注意:我正在使用 TypeScript

似乎没有允许你返回字符串的重载方法。我仔细查看了他们的源代码,确实似乎没有重载方法。

export declare const Before: ((code: import("./support_code_library_builder/types").TestCaseHookFunction) => void) & ((tags: string, code: import("./support_code_library_builder/types").TestCaseHookFunction) => void) & ((options: import("./support_code_library_builder/types").IDefineTestCaseHookOptions, code: import("./support_code_library_builder/types").TestCaseHookFunction) => void);

我是不是漏掉了什么?我对 TypeScript 还比较陌生,所以这很可能。但似乎为 before 函数定义的每个方法都会返回 void。

答案#1

这是我的解决方案,你说得对,这真的很奇怪。

使用 // @ts-ignore 装饰器。

import {
    IDefineTestCaseHookOptions, TestCaseHookFunction
} from '@cucumber/cucumber/lib/support_code_library_builder/types';

// @ts-ignore
Before(function (testCase: IDefineTestCaseHookOptions, callback: TestCaseHookFunction): void {
    console.log(testCase);

    // @ts-ignore
    callback();
});

```