Testing Alexa Skills

Testing Alexa Skills — Because Sometimes They Suck

Testing Alexa

Telling Alexa to open your skill 500 times isn’t fun, but until recently that was how I went about testing my skills. I’d use the developer console to save my voice, but still copying and pasting the same launch command got old, fast. Then if I wanted to test a specific flow I had to type all of the commands for that 😢

I have come up with a set of tools that make testing very easy. They may not be “the best”, but it’s much better than what I had been doing.

Alexa testing…

Libraries Used

Before I get started with the technical details, I should mention that the testing tools that I use should work for any Alexa skills, as long as they’re written in nodejs. For the actual skill development process, I use the Jovo Framework. If you haven’t tried it, you really should. Jovo does have a testing framework that was recently released, but I haven’t invested too much time in figuring it out yet.

For my testing library I’m using Jest. I moved away from using Mocha/Chai since Jest includes everything that you need (except for the Alexa interactions) right out of the box.

The rest of this tutorial uses the Jovo hello world template. To use this template see their tutorial. To get started quick, use the following commands:

npm i -g jovo-cli
jovo new --init alexaSkill --build testing-alexa-skills

Setup

To get started you need to open a terminal to the root folder of your project and install virtual-alexa as a development dependency and jest either as a global dependency or a development dependency.

npm i -D virtual-alexa jest

After jest and virtual alexa are installed, initialize jest so that it knows that you’re working on a nodejs project by running jest –init.

Creating a Unit Test

Jest runs unit tests that you put in a special folder named __tests__, or anywhere in your project as long as they end with .test.js or .spec.js. I prefer keeping my unit tests in the __tests__ folder since it simplifies deployment to lambda a little bit.

To create the first unit test, create a folder named __tests__ and create a new file inside it named tests.js. Open the folder in your favorite editor and enter the following code:

https://medium.com/media/0ad16b57fe771c438a359a0506451196/href

Now execute jest from the root directory and you should see the following:

Virtual Alexa

Virtual Alexa is a library provided by Bespoken specifically for unit testing Alexa skills. To create an instance of Virtual Alexa, you need to provide two arguments: a handler and an interaction model.

The handler is a reference to the nodejs code that would normally be triggered in a lambda function. In our example the handler is index.handler, this means that Virtual Alexa will call the handler export in index.js for every request.

The interaction model is the JSON file that you normally edit in the Amazon Alexa Developer interface. It’s where all of the slots and intents for a skill are defined. When using Jovo, you need to make sure this parameter points to the model in the platforms folder. Also, make sure you run jovo build before testing if you’ve modified Jovo’s model.

Hopefully this tutorial has provided you enough information to get started testing your Alexa skills. If you have some suggestions or questions, please feel free to leave a comment.


Testing Alexa Skills was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.