Ad

Test Method Is Overspecified

- 1 answer

I have this before function in my test:

before((done) => {
        const cognito = new Cognito();
        return cognito.authUser(
          '[email protected]',
          'password',
        )
      .then((res) => {
        AuthToken += res.AuthenticationResult.IdToken;
        done();
      })
      .catch((err) => {
        done(err);
      });
  });

It throws this error:

Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

I thought this may have been the fix:

before((done) => {
        const cognito = new Cognito();
        return new Promise(function(resolve) {
          cognito.authUser(
            '[email protected]',
            'password',
          )
        })
      .then((res) => {
        AuthToken += res.AuthenticationResult.IdToken;
        done();
      })
      .catch((err) => {
        done(err);
      });
  });

but it gives me this error:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

How do I resolve this?

Ad

Answer

The error explains a little bit.

You can't use both callback and a return.

You have 2 options:

callback (the done parameter)

before((done) => {
    const cognito = new Cognito();
    cognito.authUser(
        '[email protected]',
        'password',
    )
    .then((res) => {
        AuthToken += res.AuthenticationResult.IdToken;
        done();
    })
    .catch((err) => done(err));
});

or

Return promise

before(() => {
    const cognito = new Cognito();
    return cognito.authUser(
        '[email protected]',
        'password',
    )
    .then((res) => {
        AuthToken += res.AuthenticationResult.IdToken;
    })
});
Ad
source: stackoverflow.com
Ad