A Vue CLI plugin that adds Jest unit testing capabilities to Vue.js projects, providing seamless integration with the Vue CLI service through the 'test:unit' command.
—
The command registration capability provides the core Vue CLI plugin functionality that integrates Jest testing with the Vue CLI service infrastructure.
The primary entry point that registers the test:unit command with Vue CLI service.
/**
* Main Vue CLI plugin function that registers test:unit command
* @param api - Vue CLI Plugin API for registering commands and services
*/
function plugin(api: PluginAPI): void;Usage Example:
// Main plugin entry point (index.js)
module.exports = api => {
api.registerCommand('test:unit', {
description: 'run unit tests with jest',
usage: 'vue-cli-service test:unit [options] <regexForTestFiles>',
options: {
'--watch': 'run tests in watch mode'
},
details: 'All jest command line options are supported.'
}, (args, rawArgv) => {
// Set environment variables for Babel
process.env.VUE_CLI_BABEL_TARGET_NODE = true
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true
require('jest').run(rawArgv)
})
}Registers a new command with the Vue CLI service.
/**
* Register a command with Vue CLI service
* @param name - Command name (e.g., 'test:unit')
* @param options - Command configuration and metadata
* @param handler - Function to execute when command is run
*/
registerCommand(
name: string,
options: CommandOptions,
handler: CommandHandler
): void;Specifies the default mode for the test:unit command.
/**
* Default modes configuration for plugin commands
*/
const defaultModes: Record<string, string>;Usage Example:
module.exports.defaultModes = {
'test:unit': 'test'
}interface PluginAPI {
/** Register a new command with Vue CLI service */
registerCommand(
name: string,
options: CommandOptions,
handler: CommandHandler
): void;
}
interface CommandOptions {
/** Human-readable description of the command */
description: string;
/** Usage syntax example */
usage: string;
/** Available command-line options */
options: Record<string, string>;
/** Detailed help information */
details: string;
}
/**
* Command handler function
* @param args - Parsed command arguments
* @param rawArgv - Raw command line arguments array
*/
type CommandHandler = (args: any, rawArgv: string[]) => void;The plugin automatically sets these environment variables when running tests:
interface TestEnvironmentVariables {
/** Configure Babel to target Node.js environment */
VUE_CLI_BABEL_TARGET_NODE: "true";
/** Enable Babel module transpilation for tests */
VUE_CLI_BABEL_TRANSPILE_MODULES: "true";
}The test:unit command supports all Jest CLI options and passes them through directly:
Common Usage Patterns:
# Basic test execution
vue-cli-service test:unit
# Watch mode for development
vue-cli-service test:unit --watch
# Generate coverage reports
vue-cli-service test:unit --coverage
# Run specific test patterns
vue-cli-service test:unit --testNamePattern="MyComponent"
# Update snapshots
vue-cli-service test:unit --updateSnapshot
# Run tests matching specific files
vue-cli-service test:unit tests/unit/components/Install with Tessl CLI
npx tessl i tessl/npm-vue--cli-plugin-unit-jest