Testing Generators

You can test your generators with your testing framework of choice, like AVA we are going to use here.

Before writing actual tests, you need to install sao and your testing frameworkin the generator as dev dependencies:

cd my-generator
yarn add sao ava --dev

Now populate test/test.js with following contents:

const test = require('ava')
const sao = require('sao')

const generator = path.join(__dirname)

test('defaults', async t => {
	// In unit tests we skip prompts and use mocked answers instead
	// If not provided we will use the default value of the prompt
	const mockPromptAnswers = { useRouter: true }
	const stream = await sao.mock({ generator }, mockPromptAnswers)
	// Check if `router.js` is in the generated files

Finally type yarn ava to run the tests.

sao.mock(input, answers?) references



  • Type: string

The path to the generator.


  • Type: Object

Mocked prompt answers.

stream references


  • Type: string[]

A list of generated files.


  • Type: (path: string) => Promise<string>

Read the contents of specific file in the output directory.