You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.tsconstisUserSessionValid=()=>{// logic to return true if user session valid; else false}exportconstplugin={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 casesserver.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',asynchandler(request: Request,h: ResponseToolkit): Promise<boolean>{returnawaitrequest.isUserSessionValid();},});},};
in test file, trying to do
// user-session.spec.ts
...
import{plugin}from'../user-session';describe('User session',()=>{letserver: Hapi.Server;beforeEach(async()=>{server=Hapi.server();awaitserver.initialize();server.register({
plugin,});});afterEach(async()=>{jest.clearAllMocks();awaitserver.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));awaitserver.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..
The text was updated successfully, but these errors were encountered:
Support plan
Context
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
orfalse
based on test case needs!Sample code:
in test file, trying to do
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..
The text was updated successfully, but these errors were encountered: