Confabulation is a JavaScript test tool to assist with BDD / Cucumber for Amazon Alexa skills.
- Issue Tracker: https://gitlab.com/dan.malec/confabulation/issues
Confabulation is available as an npm module.
$ npm install confabulation
Confabulation depends on a local DynamoDB instance being present. It has been tested with both
This requires a modification to your skill's index.js.
const aws = require('aws-sdk');
// ...
exports.handler = function(event, context, callback) {
const alexa = Alexa.handler(event, context);
if (process.env.NODE_ENV === 'test') {
alexa.dynamoDBClient = new aws.DynamoDB({
endpoint: process.env.ENDPOINT_URL || 'http://localhost:8000/',
accessKeyId: 'dynamo',
region: 'test',
secretAccessKey: 'secret',
});
}
// ...
};
And a change to your package.json:
"scripts": {
"cucumber": "NODE_ENV=test cucumber-js"
}
And changes to CI (E.G. .gitlab-ci.yml):
services:
- name: dwmkerr/dynamodb
alias: dynamodb
variables:
ENDPOINT_URL: "http://dynamodb:8000/"
Create an endpoint in world.js:
const skill = require('../../index.js');
const { setWorldConstructor } = require('cucumber');
const { Endpoint } = require('confabulation');
class CustomWorld {
constructor({attach, parameters}) {
this.attach = attach;
this.parameters = parameters;
this.endpoint = new Endpoint()
.withSkill(skill)
.withApplicationId('<your-skill-id>')
.withTableName('<your-table-name>')
.withDbUrl(process.env.ENDPOINT_URL || 'http://localhost:8000/')
}
}
setWorldConstructor(CustomWorld);
You can then add invocations in steps:
When('the user opens the skill', function(callback) {
this.endpoint.say(new Intent('LaunchIntent'), callback);
}
Including passing in slot data:
When('the user says the number {int}', function(number, callback) {
this.endpoint.say(new Intent('CountIntent').withSlot('count', number), callback);
});
When('the user says {word}', function(value, callback) {
const value_id = value.toUpperCase();
this.endpoint.say(new Intent('SelectListItemIntent').withSlot('list_item', value, value_id), callback);
});
You can set session data:
Given('the user has {int} items', function(value) {
this.endpoint.setAttribute('item_count', value);
});
And check session data:
Then('the player should have {int} items', function(value) {
expect(this.endpoint.getAttribute('item_count'))).to.equal(value);
});
You can check the response:
Then('the user should hear {string}', function(phrase) {
expect(this.endpoint.response.response.outputSpeech.ssml).to.contain(phrase);
});
Portions of confabulation are copied/modified from Joan Gamell Farre's alexa-conversation module (as indicated in the source).