Cucumber-js: エラー:関数は複数の非同期インターフェースを使用しています:コールバックとpromise

作成日 2017年07月03日  ·  7コメント  ·  ソース: cucumber/cucumber-js

こんにちは、みんな、
ステップ定義でasyncを使用できない場合があります。
アプリケーションのAPIインターフェイスからログインして認証トークンを取得するという単純なシナリオがあります
私の機能ファイルは次のとおりです。

Feature: Login
  Scenario: Login using api
    Given I login to my account with my username and password
    Then I should get an authorization token
      |username|password|
      |[email protected]|dev|

私のステップは:

defineSupportCode(({ Given, Then, setDefaultTimeout }) => {
  const timeOut = 30000;
  const baseSteps: BaseSteps = new BaseSteps();
  setDefaultTimeout(timeOut);

  // tslint:disable-next-line:only-arrow-functions
  Given(/^I login to my account with my username and password$/, async (table: TableDefinition) => {
   const userData = table.hashes();

    const loginResponse =  await baseSteps.loginUser(userData[0].username, userData[0].password);


    console.log('Login response is ', loginResponse);
    const statusCode = 302;

    expect(await loginResponse.status).to.equal(statusCode);
  });

   Then(/^I should get an authorization token$/, async () => {
    const authorizationHeader = await baseSteps.getAuthorizationHeader();
    console.log('Auth Header', authorizationHeader);
    expect(authorizationHeader).to.not.equal(null);

    const orders = await fetch('url',
      {
        method: 'GET', headers: {
          authorization: authorizationHeader
        }
      });

    // tslint:disable-next-line:no-console
    console.log(await orders.json());
  });
});

また、BaseSteps.tsのヘルパー関数は次のとおりです。

 async loginUser(userName: string, password: string): Promise<Response> {
    const getSignInFormResponse = await fetch(this.getInitialRequestUrl(),
      {
        method: 'GET'
      });

    const form = <some-form-data>;

    const loginResponse = await fetch(getSignInFormResponse.url,
      {
        method: 'POST',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        body: form,
        redirect: 'manual'
      });

      return loginResponse;
  }

async getAuthorizationHeader() {
    const tokenResponse = await fetch(this.getInitialRequestUrl(),
      {
        method: 'GET',
        redirect: 'manual'
      });

    const tokenInfo = qs.parse(tokenResponse.headers.get('location'));
    const authorizationHeader = `${tokenInfo.token_type} ${tokenInfo.access_token}`;

    return authorizationHeader;
  }

シナリオを実行すると、次のエラーで失敗します。

Error: function uses multiple asynchronous interfaces: callback and promise

私のギブンが始まるライン上

このエラーの修正を手伝ってください。

最も参考になるコメント

Givenステップ定義にはtable引数がありますが、機能ファイルにはテーブルがありません。 Cucumber-jsは、ステップ定義の引数の数に基づいて、 callbackインターフェースを使用しているかどうかを判別します。 渡された引数の数よりも1つ多い場合は、コールバックインターフェイスを使用していると見なされます。 また、promiseを返すため、promiseインターフェイスを使用していると判断されます。 一度に使用できるインターフェースは1つだけです。

ステップ定義にX個の引数があるため、コールバックインターフェイスが想定されているという事実について何かを含めるように、エラーメッセージを更新することについて考えていますか?

全てのコメント7件

Givenステップ定義にはtable引数がありますが、機能ファイルにはテーブルがありません。 Cucumber-jsは、ステップ定義の引数の数に基づいて、 callbackインターフェースを使用しているかどうかを判別します。 渡された引数の数よりも1つ多い場合は、コールバックインターフェイスを使用していると見なされます。 また、promiseを返すため、promiseインターフェイスを使用していると判断されます。 一度に使用できるインターフェースは1つだけです。

ステップ定義にX個の引数があるため、コールバックインターフェイスが想定されているという事実について何かを含めるように、エラーメッセージを更新することについて考えていますか?

@charlierudolphお返事ありがとうございますが、私の機能ファイルにテーブルがないと言った理由がよくわかりませんでしたか?

  Given I login to my account with my username and password
    Then I should get an authorization token
      |username|password|
      |[email protected]|dev|

機能ファイルでテーブルを渡す方法ではありませんか?
ここからインスピレーションを得ましたhttps://github.com/cucumber/cucumber-js/blob/master/features/data_tables.feature

ええ、エラーメッセージはもう少し明確かもしれません。
再度、感謝します。

つまり、機能ファイルでは、 Thenステップにテーブルがありますが、 Givenステップにはありません。

うーん、くそー..うん、そうだね..混乱してすみません、テーブルをexampleと間違えました。 今は大丈夫だと思います。 問題が解決しない場合は、もう一度質問するために戻ってきます。

@charlierudolphとして問題を特定することができました。

それがあなたのために働いてうれしいです。 エラーメッセージを更新するための別の問題を開いて、将来これを理解しやすくすることを願っています

このスレッドは、閉じられた後に最近のアクティビティがないため、自動的にロックされています。 関連するバグについては、新しい問題を開いてください。

このページは役に立ちましたか?
0 / 5 - 0 評価