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
- Module Utilities
getModuleOptionsModuleContext
- Plugin Utilities
compilePluginPluginContext
- Wait Utilities
delaynextTickPwatchP
This package will come with:
-
Required dependencies:
- lodash.template - to compile ERB templates
- serialize-javascript - also for compiling ERB templates; alternative to JSON.stringify. Only used if your plugin uses
serialize.
-
Optional dependencies: (but highly recommended!)
- @nuxtjs/module-test-utils - Additional testing utilities for modules
- @vue/test-utils - Test utils for Vue components
- jsdom-global - Required by @vue/test-utils
- browser-env - Usually required when it is desired to have a browser-like environment in Node.js.
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() // jestgetModuleOptions(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 }):
- Params:
- options: Object - module options
- module: Object - module instance
- compileOpts: Object - compile options to be used for adding the plugin (see plugin utils)
- Actions:
- addTemplate: If
this.addTemplatewere used by the module, this simply mocks the function. It setsthis.templateAddedwith the template options provided. addPlugin: Ifthis.addPluginwere used by the module, this actually compiles the plugin using the compileOpts provided toModuleContext. It also setsthis.pluginAddedwith 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 providedoptionstoModuleContext.
- addTemplate: If
compilePlugin({ src, tmpFile, options, overwrite })
- Params:
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.
- Returns: none
PluginContext(Plugin)
- Params:
Plugin- Object - plugin function, usually the export default from your plugin.js file. It usually wraps around an injector.
- Actions:
- Plugin context is instantiated with the
newoperator - If the plugin.js calls
inject, a mockinjectwill be called and setthis.injected[label]to the object that the plugin is injecting.
- Plugin context is instantiated with the
delay(ms)- promisified delay...nothing fancy, just a wrapper aroundsetTimeoutthat you canawaiton.nextTickP(ctx)- promisified wrapper aroundctx.$nextTick. Lets you doawait nextTickP(ctx)for cleaner code.watchP(ctx, prop, changesFn)- promisified wrapper aroundctx.$watchso you canawait 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