Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest testing hapijs -> how to mock a decorated method call or override it during testing #4412

Open
vivekVells opened this issue Jan 5, 2023 · 0 comments
Labels
support Questions, discussions, and general support

Comments

@vivekVells
Copy link

vivekVells commented Jan 5, 2023

Support plan

  • is this issue currently blocking your project? (yes/no): yes.
  • is this issue affecting a production system? (yes/no): no.

Context

  • node version: node v14.19.1 (npm v6.14.17)
  • module version: "@hapi/hapi": "20.1.5",
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): reactJS
  • any other relevant information:

How can we help?

Objective: trying to figure out a way to mock return value of a decorated (server.decorate) function.

Example use-case:

simple plugin; uses server.decorate to define a function isUserSessionValid to return true or false value.

NEED HELP WITH: how to make this decorated isUserSessionValid (which would be like request.isUserSessionValid) in the jest test file to return true or false based on test case needs!

Sample code:

// user-session.ts

const isUserSessionValid = () => {
 // logic to return true if user session valid; else false
}

export const plugin = {
  name: 'session',
  register(server: Server) {
    // TODO: figure out a way to jest test mock this decorated method: isUserSessionValid return value to be true or false based on test cases
    server.decorate('request', 'isUserSessionValid', isUserSessionValid);

    // REST API endpoint to validate whether the received request contains valid user session or not; returns true if valid; else false. 
    server.route({
      method: 'GET',
      path: '/api/user-session-validate',
      async handler(request: Request, h: ResponseToolkit): Promise<boolean> {
        return await request.isUserSessionValid();
      },
    });
  },
};

in test file, trying to do

// user-session.spec.ts
...

import { plugin } from '../user-session';

describe('User session', () => {
  let server: Hapi.Server;

  beforeEach(async () => {
    server = Hapi.server();
    await server.initialize();

    server.register({
      plugin,
    });
  });

  afterEach(async () => {
    jest.clearAllMocks();
    await server.stop();
  });

  describe('GET /api/user-session-validate', () => {
    it('when session is valid, returns true', async () => {
      
      // doing this would result in expected error: "Request decoration already defined: isUserSessionValid"
      // HOW TO MOCK THIS TO RETURN "true" or "false" based on my needs?
      server.decorate('request', 'isUserSessionValid', () => Promise.resolve(true));

      await server
        .inject({
          method: 'GET',
          url: '/api/user-session-validate',
        })
        .then((res) => {
           // do some logic to validate.... 
        });
    });
  });
....

Can someone please help me on this? Been trying to tackle this for long time with no solid success. am also new to this framework & trying to learn..

@vivekVells vivekVells added the support Questions, discussions, and general support label Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

1 participant