2

I'm using autoLogin plugin in CodeceptJS project with Puppeteer as testing lib.

This is my first project with CodeceptJS, the autoLogin workflow is working fine, but I'm not using the application pages (UI) to login or check the current token, I'm using directly REST API calls to make all the process a bit faster.

My autoLogin config is something like:


autoLogin: {
      enabled: true,
      saveToFile: true,
      inject: 'login',
      users: {
        admin: {
          login: async (I) => {
            I.amOnPage('/login');
            await I.loginWithCredentials(adminUser.username, adminUser.password);            
          },
          check: (I) => {
            const token = codeceptjs.store['admin_session'];
            I.validateToken(token);
          },
          fetch: async (I) => {
            const cookie = await I.grabCookie('token');
            return cookie.value;
          },
          restore: (I, sessionToken) => {
            I.amOnPage('/login');
            I.saveTokenData(sessionToken);
          }
        },
     }
   }

The steps saveTokenData(), validateToken() and loginWithCredentials() are custom steps defined with actor(), for instance:

module.exports = function() {
  return actor({

    async validateToken(token) {
    
      let response = await this.sendGetRequest(`/api/session/validate?token=${token}`);
      
      if (response.status === 200) {
        if (response.data === true) {
          return true;
        }
      }
      throw new Error('Invalid token !!');
    }
});

The line throw new Error('Invalid token !!'); It's generating a "unexpected" error in the workflow, showing this line in the log:

(node:5986) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

I've tried several approaches like using the recorder.add() (throwing the error inside the recorder), I also tried to use chained promises (without async keyword) and the assert lib with assert.fail() , but always the same problem.

How can I "mark" my custom step as a failure without odd log messages?, I just want to implement the check step in the autoLogin without the exiting API to validate the user interface (I.see(), I.seeElement(), I.dontSee()...)

I'm using an ugly workaround until I get a better solution, the custom step returns a boolean, without throwing any error, and in the check code I've got this:

check: async (I) => {
  console.log('admin.check');
  const token = codeceptjs.store['admin_session'];
  const isValid = await I.validateToken(token);
  if (!isValid) {
    I.see("NOTHING TO SEE HERE, so It'll fail");
  }
},

I opened an issue in CodeceptJS GitHub project #2600

Roberto
  • 7,296
  • 2
  • 39
  • 46

1 Answers1

0

How about use the assert.fail method? It will fail your entire Scenario and you can customize the message!

const assert = require("assert");

module.exports = function() {
  return actor({

    async validateToken(token) {
    
      let response = await this.sendGetRequest("/api/session/validate?token=${token}");
      
      if (response.status !== 200 || response.data !== true) {
         assert.fail("Invalid token!");
      }
       
    }
});

Check method:

check: async (I) => {
  console.log('admin.check');
  const token = codeceptjs.store['admin_session'];
  await I.validateToken(token);
}
Klein
  • 19
  • 6