Search code examples
node.jstestingjestjsbotframework

Calling processMessage() from bot.on('send')


I am writing tests for my bot using Jest, and I have the following code:

let bot;
let conn;
let index = 0;
let responses = [];
let expectedResponses = [];

beforeEach(() => {
    let connector = new builder.ConsoleConnector();
    bot = new builder.UniversalBot(connector);
    conn = connector;
});

test('test case 1 jeff', () => {
    bot.library(require('../dialogs/inputRecognizer').createLibrary());
    // the main dialog "/" should start "inputRecognizer:/"
    // as it can be seen [https://github.com/Microsoft/BotBuilder/blob/858aad96cdd1fabbf7f9a29ee797eb03111d7cba/Node/core/tests/Dialogs.js][1] the code below seems to be identical to the one shown in examples
    bot.dialog('/', [
        (session) => {
            session.beginDialog('inputRecognizer:/');
        }
    ]);

    basicMessages.forEach(o => {if (o.in) {expectedResponses.push(o.in)}});

    bot.on('send', function (message) {
        responses.push(message.text);
        index++;
        if (index < testMessages.length) {
            conn.processMessage(basicMessages[index].out);
        } else {
            expect(responses).toEqual(expectedResponses);
        }
    });

    conn.processMessage(basicMessages[0].out);
});

The code runs fine and inputs all my messages into the connector. However, the second message is treated as a new dialog, as are all the following messages. So what I get is the dialog starting over and over again.

I've looked into the BotFramework tests at GitHub and it looks like they're using a similar approach (trigger the first processMessage outside the 'send' event handler, and call the next ones from the handler).

How do I get processMessage to not restart my dialog every time? I assume it's a problem with my dialog, maybe?

My dialog code ("inputRecognizer:/"):

lib.dialog('/', [
    function (session) {
        // it always executes this part
        builder.Prompts.text(session,
            `Please type your inquiry!`);
    },
    (session, result) => {
        if (result.response) {
            // and it never gets to here

Edit: basicMessages is this:

module.exports = [
    {
        out: 'hi',
        in: 'Please type your inquiry!',
    },
    {
        out: 'aaa',
        in: 'Please type your inquiry!'
    },
    {
        out: 'aaa',
        in: 'Please type your inquiry!'
    },
    {
        out: 'aaaa',
        in: 'Please type your inquiry!'
    },
    {
        out: 'aa',
        in: 'Please type your inquiry!'
    }
];

Solution

  • It seems that your basicMessages are wrong: your expectedResponses array looks like:

    ["Please type your inquiry!", "Please type your inquiry!", "Please type your inquiry!", "Please type your inquiry!", "Please type your inquiry!"]
    

    which I don't think it's expected as you are adding messages.Text to the list of responses you later use to assert.

    Also, you don't need the createLibrary();

    You can just use bot.library(require('../dialogs/inputRecognizer'));

    I recreated your project (GitHub) and the code reaches the waterfall after the prompt just fine.