Retrieve session in test

unit-testing

#1

Hi
i have in app.js

 async ON_REQUEST() {
     this.$session.$data.sapEvent = await HttpService.get(url);
 },

on test script

conversation = testSuite.conversation({
      runtime: "app"
    });
    const launchRequest = await 
    testSuite.requestBuilder.launch();
    const response = await conversation.send(launchRequest);
    console.log(conversation.$session)

why console.log return undefined ?

console.log of conversation return

Conversation {
      sessionData: {
        sapEvent: {
          status: 200,
          statusText: 'OK',
          headers: [Object],
          config: [Object],
          request: [ClientRequest],
          data: [Object]
        }
      },

how to retrieve session attribute in unit test ?


#2

Seems like you need to access it via conversation.sessionData


#3

Since we also use conversation.$user.$data, it makes more sense to use conversation.$session.$data for consistency reasons.

I added it and it will available in one of the next releases. conversation.sessionData still works, of course.


#4

Ah yes, this makes a lot of sense. Thanks, @AlexSwe!


#5

app.js

async ON_REQUEST() {
        this.$session.$data.sapEvent = "hello";
},

on unit list.test.js

 it('fetch all events in category', async () => {
    const on_request = await testSuite.requestBuilder.intent('ON_REQUEST');
    const response = await conversation.send(on_request);
    console.log(conversation.$session.$data)

  });

with sessionData it work better


#6

It’s not released yet :slight_smile:


#7

but something wrong today for me

ON_REQUEST(){
     this.$session.$data.sapEvent = "hello";
}}

in list.test.js

  const on_request = await 
    testSuite.requestBuilder.intent('ON_REQUEST');
    const response = await conversation.send(on_request);
    console.log(conversation.sessionData)

return nothing execerpt {}


#8

The session data is also stored in the response.

response.getSessionData()

#9

not work now.
something have strange, because return {} so empty

but same code with
this.$user.$data.sapEvent = “hello”;
it work fine

so i can’t use session


#10

app.js

ON_REQUEST() {
    this.$session.$data.sapEvent = "hello";
  },

on list.test.js

it('fetch all events in category', async () => {
    const on_request = await testSuite.requestBuilder.intent('ON_REQUEST');
    const response = await conversation.send(on_request);

    const title = response.getSessionData();
    expect(title).toEqual("hello")

});

result :frowning:

expect(received).toEqual(expected) // deep equality

Expected: "hello"
Received: {}

#11

I’m not sure if ON_REQUEST works as you’re testing it here as it’s a Jovo standard intent that is triggered every time: https://www.jovo.tech/docs/routing/intents#on_request

I’d probably test a more specific use case, intents that the user is actively triggering


#12

ok I see it work only if session is set in launch intent, but not in ON_REQUEST.
we can’t retrieve session, if get in this intent
So i don’t get interest of this ON_REQUEST