Testing Alexa Skills
Tags: testing, node, alexa, voicefirst
Published: 2019-09-18 23:18:51

---
title: Testing Alexa Skills
published: true
tags: 
  - testing
  - node
  - alexa
  - VoiceFirst
canonical_url: https://www.voiceify.io/posts/testing-alexa-skills/
---

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…](https://www.voiceify.io/static/30f6091ebdf7743469e1c1d84f184adb/d4502/alexa-testing-comic.png "Alexa testing…")](/static/30f6091ebdf7743469e1c1d84f184adb/d4502/alexa-testing-comic.png)<figcaption>Alexa testing…</figcaption>

## 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](https://www.jovo.tech/). 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](https://jestjs.io/). 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](https://www.jovo.tech/blog/project-1-hello-world/). 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`.

[![Initializing Jest](https://www.voiceify.io/static/48dbd7e83b2ef88b30b6cf4783f5ad91/149ee/init-jest.jpg "Initializing Jest")](/static/48dbd7e83b2ef88b30b6cf4783f5ad91/149ee/init-jest.jpg)<figcaption>Initializing Jest</figcaption>

## 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:

```
const va = require("virtual-alexa");
const alexa = va.VirtualAlexa.Builder()
  .handler("index.handler")
  .interactionModelFile("platforms/alexaSkill/models/en-US.json")
  .create();

test("Say hello world on launch", async () => {
  var result = await alexa.launch();

  expect(result.response.outputSpeech.ssml).toContain("Hello World!");
});
```

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

[![Console Output](https://www.voiceify.io/static/92146166ccaf8d88c99346ad227dd95d/da001/console.jpg "Console Output")](/static/92146166ccaf8d88c99346ad227dd95d/da001/console.jpg)<figcaption>Console Output</figcaption>

## Virtual Alexa

Virtual Alexa is a library provided by [Bespoken](https://bespoken.io/) 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.

<!--stackedit_data:
eyJoaXN0b3J5IjpbMTM3NjM4MTQ4NywtMTEyNjE4ODA5NF19
-->