nuxt-test-utils

0.0.1 • Public • Published

nuxt-test-utils

npm version npm License

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!

Installation

npm i -D nuxt-test-utils

What's in the box:

This package will come with:

Usage

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

Utilities

Module Utilities

  1. 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

  1. 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.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 yourself
    • registerModule: It registers the module with the provided options to ModuleContext.

Plugin Utilities

  1. compilePlugin({ src, tmpFile, options, overwrite })
  • Params:
    • src: String - plugin source filename
    • tmpFile: String - filename to save the compiled plugin to
    • options: 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
  1. 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 new operator
    • If the plugin.js calls inject, a mock inject will be called and set this.injected[label] to the object that the plugin is injecting.

Wait utilities

  1. delay(ms) - promisified delay...nothing fancy, just a wrapper around setTimeout that you can await on.
  2. nextTickP(ctx) - promisified wrapper around ctx.$nextTick. Lets you do await nextTickP(ctx) for cleaner code.
  3. 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.

Suggested Prep and Reading

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

Readme

Keywords

Package Sidebar

Install

npm i nuxt-test-utils

Weekly Downloads

4

Version

0.0.1

License

MIT

Unpacked Size

11.3 kB

Total Files

8

Last publish

Collaborators

  • richardeschloss