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

vue-node

v1.1.1

Published

A require hook for loading single-file vue components in node.

Downloads

125

Readme

vue-node

Dependency Status devDependency Status Build Status Npm Version License Badges

A require hook for loading single-file vue components in node. Useful for testing without having to spin up web browsers!

Impending Deprecation Notice

The Vue team is going to be creating official testing tools based on avoriaz which already has complete examples of how to test Vue components in node-based testing frameworks without spinning up web browsers. My suggestion is for users of this library to migrate to avoriaz and then to the official testing tools once those are available. Once the official testing tools are available, I will mark this package as deprecated on npm.

Usage of this library will also require changes to your vue-loader configuration starting with version ^12.0.0. See issue #9 for more details.

Usage Example

Here is an example of using vue-node with AVA. The process should be similar for whatever node testing framework you want to use.

First, make sure you have vue-node and browser-env installed as development dependencies. If you are running an environment with vue-loader and webpack@2 then you will already have all required peer dependencies:

npm i -D vue-node browser-env

Now create a setup file called test/helpers/setup.js. Putting it in the test/helpers directory will let AVA know that this file is not a test.

const browserEnv = require('browser-env');
const hook = require('vue-node');
const { join } = require('path');

// Setup a fake browser environment
browserEnv();
// Pass an absolute path to your webpack configuration to the hook function.
hook(join(__dirname, 'webpack.config.test.js'));

Now you can configure AVA to require this file in all test processes. In package.json:

{
  "ava": {
    "require": [
      "./test/helpers/setup.js"
    ]
  }
}

Now you can require / import .vue files and test like you would in a browser! If you need to test DOM updates, you can use Vue.nextTick along with async / await.

import Vue from 'vue';
import test from 'ava';
import TestComponent from './test.vue';

test('renders the correct message', async (t) => {
  const Constructor = Vue.extend(TestComponent);
  const vm = new Constructor().$mount();
  t.is(vm.$el.querySelector('h1').textContent, 'Hello, World!');
  // Update
  vm.setName('Foo');
  await Vue.nextTick();
  t.is(vm.$el.querySelector('h1').textContent, 'Hello, Foo!');
});

See the test directory in this project for more examples!

Common Questions

How does this work?

Node allows developers to hook require to load files that aren't JavaScript or JSON. Unfortunately, require hooks have to be synchronous. Using vue-loader on the other hand, is inherently asynchronous. vue-node works by synchronously running webpack in a separate process and collecting the output to pass to node's module compilation system. The compilation is done completely in memory without writing to the filesystem. It also modifies your webpack configuration to automatically build for node and commonjs with all dependencies of your component externalized. This means that the built component modules are as small as possible with dependency resolution left up to node.

What if I am using vueify?

I am personally more familiar with webpack than browserify, so for the time being this will only work in combination with webpack. I will gladly accept a pull request to implement browserify functionality.

What about testing in web browsers?

Unit testing in web browsers is a very heavy process with many tradeoffs. Configuration and tooling is tricky as is getting browsers to run in CI. I personally like saving browsers for end-to-end testing with things like Nightwatch.js.

License

MIT