Serverless test using $db failing

Have been repeatedly getting this error when I run my test case that involves stubbing the $db methods.

This is the error message.

TypeError: Cannot read properties of undefined (reading 'statusCode')
    at Request._callback (/Users/administrator/.nvm/versions/node/v18.16.0/lib/node_modules/fdk/lib/event_handler/features/db.js:44:45)

Here is the stub in the test case:

const stubbedGetDb = this.stub('$db', 'get').resolves({
  logs: [
    {
      message: 'hi',
      success: true,
      timestamp: '2023-09-18',
    },
  ],
});

The code for which this fails is:

try {
  const storedData = await $db.get(ruleAlias); 
}

Please help in resolving this as it is a blocker in achieving 80% code coverage for my app.

Hi @arunrajkumar235

I successfully tested the sample $db set and get calls by stubbing them, and they functioned as expected.

Sample server.js

exports = {

  onAppInstallHandler: async function () {
    try {
      let data = await $db.set("ticket:101", {
        "jiraIssueId": 15234
      });
      console.log('data', data);
    } catch (error) {
      console.error('error', error);
    }
    renderData(null)
  },
  onTicketUpdateHandler: async function () {

    try {
      let data = await $db.get("ticket:101");
      console.log('data', data);
    } catch (error) {
      console.log('error', error);
    }
  },


};

Sample test/server.js


const expect = require('./../server/node_modules/chai').expect;
const appInstallArg = require('./args/onAppInstall.json');

describe('App Events Spec', function() {
  it('checks onAppInstall Success Flow', function() {

    const stubbedDB = this.stub('$db', 'set').resolves({
      "jiraIssueId": 1
    })
    return this.invoke('onAppInstall', appInstallArg);
  });

  it('checks onTicketCreate Success Flow', function() {

    const stubbedDB = this.stub('$db', 'get').resolves({
      "jiraIssueId": 1
    })
    return this.invoke('onTicketUpdate', appInstallArg);
  });

});

Could you kindly use the provided sample code as a reference to review the implementation in your code and verify the test cases?

Could you kindly provide the code snippet for which you are seeking to write test cases? Sharing the relevant code will help us analyze it and identify any potential issues more accurately.

Please consider exploring Freddy Copilot for Developers to enhance your development experience with the support of generative AI capabilities.

Regards
Nadeem

I did exactly what you’ve shared in your reply.

The error seems to originate in the fdk node module. I had a breakpoint to see what the error was and it turns out that it tries to make a network call to localhost:10001 and it fails.

So, on another terminal window I ran fdk run and then ran fdk test in parallel.
This setup makes it work.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.