0
# Command Registration
1
2
The command registration capability provides the core Vue CLI plugin functionality that integrates Jest testing with the Vue CLI service infrastructure.
3
4
## Capabilities
5
6
### Main Plugin Function
7
8
The primary entry point that registers the `test:unit` command with Vue CLI service.
9
10
```javascript { .api }
11
/**
12
* Main Vue CLI plugin function that registers test:unit command
13
* @param api - Vue CLI Plugin API for registering commands and services
14
*/
15
function plugin(api: PluginAPI): void;
16
```
17
18
**Usage Example:**
19
20
```javascript
21
// Main plugin entry point (index.js)
22
module.exports = api => {
23
api.registerCommand('test:unit', {
24
description: 'run unit tests with jest',
25
usage: 'vue-cli-service test:unit [options] <regexForTestFiles>',
26
options: {
27
'--watch': 'run tests in watch mode'
28
},
29
details: 'All jest command line options are supported.'
30
}, (args, rawArgv) => {
31
// Set environment variables for Babel
32
process.env.VUE_CLI_BABEL_TARGET_NODE = true
33
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true
34
require('jest').run(rawArgv)
35
})
36
}
37
```
38
39
### Command Registration
40
41
Registers a new command with the Vue CLI service.
42
43
```javascript { .api }
44
/**
45
* Register a command with Vue CLI service
46
* @param name - Command name (e.g., 'test:unit')
47
* @param options - Command configuration and metadata
48
* @param handler - Function to execute when command is run
49
*/
50
registerCommand(
51
name: string,
52
options: CommandOptions,
53
handler: CommandHandler
54
): void;
55
```
56
57
### Default Modes Configuration
58
59
Specifies the default mode for the test:unit command.
60
61
```javascript { .api }
62
/**
63
* Default modes configuration for plugin commands
64
*/
65
const defaultModes: Record<string, string>;
66
```
67
68
**Usage Example:**
69
70
```javascript
71
module.exports.defaultModes = {
72
'test:unit': 'test'
73
}
74
```
75
76
## Types
77
78
```javascript { .api }
79
interface PluginAPI {
80
/** Register a new command with Vue CLI service */
81
registerCommand(
82
name: string,
83
options: CommandOptions,
84
handler: CommandHandler
85
): void;
86
}
87
88
interface CommandOptions {
89
/** Human-readable description of the command */
90
description: string;
91
/** Usage syntax example */
92
usage: string;
93
/** Available command-line options */
94
options: Record<string, string>;
95
/** Detailed help information */
96
details: string;
97
}
98
99
/**
100
* Command handler function
101
* @param args - Parsed command arguments
102
* @param rawArgv - Raw command line arguments array
103
*/
104
type CommandHandler = (args: any, rawArgv: string[]) => void;
105
```
106
107
## Environment Variables
108
109
The plugin automatically sets these environment variables when running tests:
110
111
```javascript { .api }
112
interface TestEnvironmentVariables {
113
/** Configure Babel to target Node.js environment */
114
VUE_CLI_BABEL_TARGET_NODE: "true";
115
/** Enable Babel module transpilation for tests */
116
VUE_CLI_BABEL_TRANSPILE_MODULES: "true";
117
}
118
```
119
120
## Command Execution
121
122
The `test:unit` command supports all Jest CLI options and passes them through directly:
123
124
**Common Usage Patterns:**
125
126
```bash
127
# Basic test execution
128
vue-cli-service test:unit
129
130
# Watch mode for development
131
vue-cli-service test:unit --watch
132
133
# Generate coverage reports
134
vue-cli-service test:unit --coverage
135
136
# Run specific test patterns
137
vue-cli-service test:unit --testNamePattern="MyComponent"
138
139
# Update snapshots
140
vue-cli-service test:unit --updateSnapshot
141
142
# Run tests matching specific files
143
vue-cli-service test:unit tests/unit/components/
144
```