首页 > 解决方案 > 在 cucumber-js 中的钩子之前无法返回

问题描述

目前,我正在尝试根据提供的标签跳过 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'.

注意:我正在使用打字稿

似乎没有允许您返回字符串的重载方法。我挖掘了他们的源代码,似乎没有重载方法

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);

我在这里错过了什么吗?我对打字稿还很陌生,所以很有可能。但似乎为 before 函数定义的每个方法都返回 void。

标签: javascripttypescriptcucumberjs

解决方案


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

使用// @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();
});

```

推荐阅读