npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nuxt-test-utils

v0.0.1

Published

Nuxt test utilities (a single home for all your favorite testing utils)

Downloads

13

Readme

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