Nuxt testing utils (a single home for all your favorite testing utils).
Some of these utils may help your tests shave off many minutes of run time!
npm i -D nuxt-test-utils
getModuleOptions
ModuleContext
compilePlugin
PluginContext
delay
nextTickP
watchP
This package will come with:
Required dependencies:
serialize
.Optional dependencies: (but highly recommended!)
Suppose your nuxt.config is set up like this:
module.exports = {
modules: ['./lib/module.js'],
myModOpts: { } // NOTE: example purposes only! Use a name best suited for *your* module!
}
import TestUtils from 'nuxt-test-utils'
// or, just pick the utils you need
import { getModuleOptions, ModuleContext, PluginContext } from 'nuxt-test-utils'
import config from '@/nuxt.config' // optional, but useful for utilities.
import path from 'path'
import Module from '@/lib/module'
const ctx = new ModuleContext({
options: getModuleOptions(config, 'myModOpts'),
module: Module,
compileOpts: {
src: path.resolve('./lib/plugin.js'),
tmpFile: path.resolve('./lib/plugin.compiled.js'),
overwrite: true
}
})
// Finally, load the module
ctx.registerModule()
// Check if the plugin got added:
t.truthy(ctx.pluginAdded) // ava
expect(ctx.pluginAdded).toBeTruthy() // jest
getModuleOptions(config, moduleName, optsContainer)
:
Params:
config: Object - provide nuxt.config
moduleName: String
optsContainer: Object - specify the container holding your options. defaults to moduleName. Options will be searched in this order: buildModules, then modules, then your optsContainer. As options are found, they'll be merged in.
Returns: module options
ModuleContext({ options, module, compileOpts })
:
this.addTemplate
were used by the module, this simply mocks the function. It sets this.templateAdded
with the template options provided.addPlugin
: If this.addPlugin
were used by the module, this actually compiles the plugin using the compileOpts provided to ModuleContext
. It also sets this.pluginAdded
with the plugin options provided.compilePlugin
: it also wires up compilePlugin to the ModuleContext, in case you need to use it yourselfregisterModule
: It registers the module with the provided options
to ModuleContext
. compilePlugin({ src, tmpFile, options, overwrite })
src
: String - plugin source filenametmpFile
: String - filename to save the compiled plugin tooptions
: Object - plugin options; i.e., where <%= JSON.stringify(options) %> exists, that will be replaced by the options specified here.overwrite
: Boolean - overwrite the compiled plugin if it already exists. Default: false
.PluginContext(Plugin)
Plugin
- Object - plugin function, usually the export default from your plugin.js file. It usually wraps around an injector.new
operatorinject
, a mock inject
will be called and set this.injected[label]
to the object that the plugin is injecting.delay(ms)
- promisified delay...nothing fancy, just a wrapper around setTimeout
that you can await
on. nextTickP(ctx)
- promisified wrapper around ctx.$nextTick
. Lets you do await nextTickP(ctx)
for cleaner code.watchP(ctx, prop, changesFn)
- promisified wrapper around ctx.$watch
so you can await watchP(ctx, 'someData', () => { ctx.someData = 123 })
. It will resolve once the data has changed. Getting your test environment set up correctly for Nuxt is more than half the battle. Even though test environment is technically beyond the scope of this repo, to avoid having issues being opened on this topic, here are some bullets that may help:
When you first create a Nuxt app using create-nuxt-app, you are asked for choice of test framework. Try running that sample code first before proceeding. If you skipped the test framework selection, you can have a look at their templates and start with those, most likely for ava or jest.
Alternatively, you may find it just as useful to clone Vinayak's repo: https://github.com/vinayakkulkarni/nuxt-ava-e2e-unit-testing