jest-test-lineage-reporter 2.1.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -20,29 +20,58 @@ A comprehensive test analytics platform that provides line-by-line test coverage
20
20
 
21
21
  ## šŸš€ CLI Usage (New!)
22
22
 
23
- Jest Test Lineage Reporter now includes a powerful CLI tool!
23
+ Jest Test Lineage Reporter now includes a powerful CLI tool with automatic configuration!
24
24
 
25
25
  ### Quick Start with CLI
26
26
 
27
27
  ```bash
28
- # Run tests with lineage tracking
29
- jest-lineage test
28
+ # 1. Install the package
29
+ npm install --save-dev jest-test-lineage-reporter jest babel-jest @babel/core @babel/preset-env
30
30
 
31
- # Run mutation testing on existing data
32
- jest-lineage mutate --threshold 85
31
+ # 2. Initialize configuration (creates jest.config.js and babel.config.js)
32
+ npx jest-lineage init
33
33
 
34
- # Generate HTML report
35
- jest-lineage report --open
34
+ # 3. Run tests with lineage tracking
35
+ npx jest-lineage test
36
36
 
37
- # Query which tests cover a specific line
38
- jest-lineage query src/calculator.ts 42
37
+ # 4. Generate HTML report
38
+ npx jest-lineage report --open
39
39
 
40
- # Full analysis workflow (test + mutate + report)
41
- jest-lineage analyze --open
40
+ # 5. Run mutation testing
41
+ npx jest-lineage mutate --threshold 85
42
+
43
+ # 6. Query which tests cover a specific line
44
+ npx jest-lineage query src/calculator.ts 42
45
+
46
+ # 7. Full analysis workflow (test + mutate + report)
47
+ npx jest-lineage analyze --open
42
48
  ```
43
49
 
44
50
  ### CLI Commands
45
51
 
52
+ #### `jest-lineage init` ⭐ NEW
53
+ Initialize project configuration automatically.
54
+
55
+ ```bash
56
+ # Create jest.config.js and babel.config.js with all required settings
57
+ npx jest-lineage init
58
+
59
+ # Force overwrite existing config files
60
+ npx jest-lineage init --force
61
+
62
+ # Configure for TypeScript project
63
+ npx jest-lineage init --typescript
64
+ ```
65
+
66
+ **What it does:**
67
+ - āœ… Checks for required dependencies
68
+ - āœ… Creates `jest.config.js` with lineage reporter configured
69
+ - āœ… Creates `babel.config.js` with instrumentation plugin
70
+ - āœ… Detects TypeScript and configures accordingly
71
+ - āœ… Shows clear next steps
72
+
73
+
74
+
46
75
  #### `jest-lineage test [jest-args...]`
47
76
  Run Jest tests with lineage tracking enabled.
48
77
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jest-test-lineage-reporter",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "main": "src/TestCoverageReporter.js",
5
5
  "bin": {
6
6
  "jest-lineage": "./bin/jest-lineage.js"
@@ -0,0 +1,214 @@
1
+ /**
2
+ * Init Command
3
+ * Initialize jest-test-lineage-reporter in a project
4
+ */
5
+
6
+ const fs = require('fs');
7
+ const path = require('path');
8
+ const { success, error, info, warning } = require('../utils/output-formatter');
9
+
10
+ async function initCommand(options) {
11
+ try {
12
+ const cwd = process.cwd();
13
+ const jestConfigPath = path.join(cwd, 'jest.config.js');
14
+ const babelConfigPath = path.join(cwd, 'babel.config.js');
15
+ const packageJsonPath = path.join(cwd, 'package.json');
16
+
17
+ console.log('\nšŸš€ Initializing jest-test-lineage-reporter...\n');
18
+
19
+ // Check if package.json exists
20
+ if (!fs.existsSync(packageJsonPath)) {
21
+ error('No package.json found. Please run "npm init" first.');
22
+ process.exit(1);
23
+ }
24
+
25
+ // Read package.json to check dependencies
26
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
27
+ const allDeps = {
28
+ ...(packageJson.dependencies || {}),
29
+ ...(packageJson.devDependencies || {}),
30
+ };
31
+
32
+ // Check for required dependencies
33
+ const requiredDeps = ['jest', 'babel-jest', '@babel/core', '@babel/preset-env'];
34
+ const missingDeps = requiredDeps.filter(dep => !allDeps[dep]);
35
+
36
+ if (missingDeps.length > 0) {
37
+ warning('Missing required dependencies:');
38
+ missingDeps.forEach(dep => console.log(` - ${dep}`));
39
+ console.log('\nšŸ’” Install them with:');
40
+ console.log(` npm install --save-dev ${missingDeps.join(' ')}\n`);
41
+
42
+ if (!options.force) {
43
+ error('Please install missing dependencies first, or use --force to continue anyway.');
44
+ process.exit(1);
45
+ }
46
+ }
47
+
48
+ // Create or update Jest config
49
+ let jestConfigCreated = false;
50
+ if (fs.existsSync(jestConfigPath)) {
51
+ if (!options.force) {
52
+ warning(`jest.config.js already exists. Use --force to overwrite.`);
53
+ info('Please manually add the following to your jest.config.js:');
54
+ console.log(`
55
+ setupFilesAfterEnv: ['jest-test-lineage-reporter/src/testSetup.js'],
56
+
57
+ reporters: [
58
+ 'default',
59
+ ['jest-test-lineage-reporter', {
60
+ outputFile: '.jest-lineage-data.json',
61
+ enableMutationTesting: false,
62
+ }]
63
+ ],
64
+
65
+ collectCoverage: true,
66
+ collectCoverageFrom: ['src/**/*.{js,ts}', '!src/**/*.test.{js,ts}'],
67
+
68
+ transform: {
69
+ '^.+\\\\.(js|jsx|ts|tsx)$': 'babel-jest',
70
+ },
71
+ `);
72
+ } else {
73
+ createJestConfig(jestConfigPath, options);
74
+ jestConfigCreated = true;
75
+ }
76
+ } else {
77
+ createJestConfig(jestConfigPath, options);
78
+ jestConfigCreated = true;
79
+ }
80
+
81
+ // Create or update Babel config
82
+ let babelConfigCreated = false;
83
+ if (fs.existsSync(babelConfigPath)) {
84
+ if (!options.force) {
85
+ warning(`babel.config.js already exists. Use --force to overwrite.`);
86
+ info('Please manually add the lineage tracker plugin to your babel.config.js:');
87
+ console.log(`
88
+ plugins: [
89
+ 'jest-test-lineage-reporter/src/babel-plugin-lineage-tracker.js',
90
+ ],
91
+ `);
92
+ } else {
93
+ createBabelConfig(babelConfigPath, options);
94
+ babelConfigCreated = true;
95
+ }
96
+ } else {
97
+ createBabelConfig(babelConfigPath, options);
98
+ babelConfigCreated = true;
99
+ }
100
+
101
+ // Summary
102
+ console.log('\n' + '═'.repeat(50));
103
+ if (jestConfigCreated && babelConfigCreated) {
104
+ success('Configuration complete! ✨\n');
105
+ console.log('āœ… Created jest.config.js');
106
+ console.log('āœ… Created babel.config.js\n');
107
+ } else if (!jestConfigCreated && !babelConfigCreated) {
108
+ info('Configuration files already exist.');
109
+ info('Use --force to overwrite, or manually update the files.\n');
110
+ } else {
111
+ info('Partial configuration completed.');
112
+ if (jestConfigCreated) console.log('āœ… Created jest.config.js');
113
+ if (babelConfigCreated) console.log('āœ… Created babel.config.js');
114
+ console.log('');
115
+ }
116
+
117
+ // Show next steps
118
+ console.log('šŸ“‹ Next steps:\n');
119
+ if (missingDeps.length > 0) {
120
+ console.log('1. Install missing dependencies:');
121
+ console.log(` npm install --save-dev ${missingDeps.join(' ')}\n`);
122
+ }
123
+ console.log(`${missingDeps.length > 0 ? '2' : '1'}. Run your tests with lineage tracking:`);
124
+ console.log(' npx jest-lineage test\n');
125
+ console.log(`${missingDeps.length > 0 ? '3' : '2'}. Query coverage data:`);
126
+ console.log(' npx jest-lineage query src/yourfile.js\n');
127
+ console.log(`${missingDeps.length > 0 ? '4' : '3'}. Generate HTML report:`);
128
+ console.log(' npx jest-lineage report --open\n');
129
+ console.log('═'.repeat(50) + '\n');
130
+
131
+ } catch (err) {
132
+ error(`Failed to initialize: ${err.message}`);
133
+ if (options.verbose) {
134
+ console.error(err.stack);
135
+ }
136
+ process.exit(1);
137
+ }
138
+ }
139
+
140
+ function createJestConfig(filePath, options) {
141
+ const isTypeScript = options.typescript || hasTypeScriptFiles();
142
+
143
+ const config = `module.exports = {
144
+ testEnvironment: 'node',
145
+
146
+ // Required: Setup file for lineage tracking
147
+ setupFilesAfterEnv: ['jest-test-lineage-reporter/src/testSetup.js'],
148
+
149
+ // Add the lineage reporter
150
+ reporters: [
151
+ 'default',
152
+ [
153
+ 'jest-test-lineage-reporter',
154
+ {
155
+ outputFile: '.jest-lineage-data.json',
156
+ enableMutationTesting: false,
157
+ }
158
+ ]
159
+ ],
160
+
161
+ // Enable coverage
162
+ collectCoverage: true,
163
+ collectCoverageFrom: [
164
+ 'src/**/*.{js${isTypeScript ? ',ts' : ''}}',
165
+ '!src/**/*.test.{js${isTypeScript ? ',ts' : ''}}',
166
+ '!src/**/*.d.ts',
167
+ ],
168
+
169
+ // Use babel-jest for transformation
170
+ transform: {
171
+ '^.+\\\\.(js|jsx${isTypeScript ? '|ts|tsx' : ''})$': 'babel-jest',
172
+ },
173
+
174
+ // File extensions
175
+ moduleFileExtensions: ['js', 'jsx'${isTypeScript ? ", 'ts', 'tsx'" : ''}, 'json'],
176
+ };
177
+ `;
178
+
179
+ fs.writeFileSync(filePath, config, 'utf8');
180
+ }
181
+
182
+ function createBabelConfig(filePath, options) {
183
+ const isTypeScript = options.typescript || hasTypeScriptFiles();
184
+
185
+ const config = `module.exports = {
186
+ presets: [
187
+ ['@babel/preset-env', { targets: { node: 'current' } }],${isTypeScript ? `
188
+ '@babel/preset-typescript',` : ''}
189
+ ],
190
+ plugins: [
191
+ // Required: Lineage tracker plugin for instrumentation
192
+ 'jest-test-lineage-reporter/src/babel-plugin-lineage-tracker.js',
193
+ ],
194
+ };
195
+ `;
196
+
197
+ fs.writeFileSync(filePath, config, 'utf8');
198
+ }
199
+
200
+ function hasTypeScriptFiles() {
201
+ const cwd = process.cwd();
202
+ const srcDir = path.join(cwd, 'src');
203
+
204
+ if (!fs.existsSync(srcDir)) return false;
205
+
206
+ try {
207
+ const files = fs.readdirSync(srcDir);
208
+ return files.some(file => file.endsWith('.ts') || file.endsWith('.tsx'));
209
+ } catch {
210
+ return false;
211
+ }
212
+ }
213
+
214
+ module.exports = initCommand;
package/src/cli/index.js CHANGED
@@ -4,6 +4,7 @@
4
4
  */
5
5
 
6
6
  const { Command } = require('commander');
7
+ const initCommand = require('./commands/init');
7
8
  const testCommand = require('./commands/test');
8
9
  const mutateCommand = require('./commands/mutate');
9
10
  const reportCommand = require('./commands/report');
@@ -19,6 +20,15 @@ async function run(argv) {
19
20
  .description('Comprehensive test analytics with lineage tracking and mutation testing')
20
21
  .version(pkg.version, '-v, --version', 'Display version number');
21
22
 
23
+ // Init command - Initialize project configuration
24
+ program
25
+ .command('init')
26
+ .description('Initialize jest-test-lineage-reporter configuration')
27
+ .option('--force', 'Overwrite existing configuration files')
28
+ .option('--typescript', 'Configure for TypeScript project')
29
+ .option('--verbose', 'Show detailed error messages')
30
+ .action(initCommand);
31
+
22
32
  // Test command - Run Jest with lineage tracking
23
33
  program
24
34
  .command('test [jest-args...]')