aws-lex

Automated tests for Lex bot using Node.js


We are building a bot using Amazon Lex. As the bot is growing in functionality we need a way to make sure that the bot keeps working as we introduce new changes, but doing that manually is becoming quite costly. We can’t find in Amazon Lex SDK tools to create automated tests for Lex bots. Is there some framework or tools that helps on this and work with Node.js?


Solution

  • We were facing the same problem. After looking around, I decided it was easier just to build some simple tools to run automated tests for Lex. Keep in mind that this solution requires you to deploy the bot in Amazon Lex first, so it is not to create unit tests, but more like functional tests.

    Basically what we did was create a simple library like this that uses the Amazon SDK to send text to the bot:

    let uuid = require('uuid/v4'),
        aws = require('aws-sdk');
    
    let config = {
        awsAccount: process.env.AWS_ACCOUNT,
        awsAccessKey: process.env.AWS_ACCESS_KEY_ID,
        awsAccessKeySecret: process.env.AWS_SECRET_ACCESS_KEY,
        awsRegion: process.env.AWS_DEFAULT_REGION,
        botName: 'BotName',
        botAlias: 'Dev'
    };
    
    let lex;
    
    exports.config = config;
    
    exports.initConfig = function() {
        aws.config.update({
            credentials: new aws.Credentials(config.awsAccessKey, config.awsAccessKeySecret),
            region: config.awsRegion
        });
        lex = new aws.LexRuntime();
    };
    
    exports.speak = async function(userId, previousResponse, text) {
        let res = await lex.postText({
            botAlias: config.botAlias,
            botName: config.botName,
            inputText: text,
            userId: userId,
            requestAttributes: previousResponse ? previousResponse.requestAttributes : {},
            sessionAttributes: previousResponse ? previousResponse.sessionAttributes : {}
        }).promise();
        return res;
    };
    
    exports.generateUserId = function() {
        return uuid();
    };
    

    Then you can use a tool like Mocha to create a test like this:

    let assert = require('chai').assert,
        utils = require('./utils'); // this is the library above
    
    utils.initConfig();
    
    describe('your conversation name', function() {
        it('conversation path 1', async function() {
            let userId = utils.generateUserId();
            let res = await utils.speak(userId, null, 'init message');
            assert.equal(res.slots.slot1, 'val1'); // check slot value
            assert.include(res.message, 'words included'); // check response from bot
            res = await utils.speak(userId, res, 'send more messages');
            assert.equal(res.slots.slot2, 'val2'); // check another slot value
            assert.include(res.message, 'another words included'); // check response from bot
        });
    });
    

    Again, this is not a unit test but more a functional test.

    NOTE: I saw this answer was deleted before because it was identical to another answer. I was answering two questions and mixed the answers, but I already changed the other answer, so this is completely different. So I'm posting it again.