impacted 0.0.5 → 0.0.6

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
@@ -9,18 +9,21 @@ A userland implementation of [predictive test selection for Node.js test runner]
9
9
  ## Usage
10
10
 
11
11
  ```bash
12
- # Run only impacted tests
12
+ # Run only impacted tests (--since gets changed files from git diff)
13
+ node --test $(npx impacted --since main)
14
+
15
+ # Pipe changed files from stdin
13
16
  node --test $(git diff --name-only main | npx impacted)
14
17
 
15
18
  # Works with any test runner
16
- vitest $(git diff --name-only main | npx impacted)
17
- jest $(git diff --name-only main | npx impacted)
19
+ vitest $(npx impacted --since main)
20
+ jest $(npx impacted --since main)
18
21
 
19
22
  # Custom test pattern
20
- git diff --name-only main | npx impacted -p "src/**/*.spec.js"
23
+ npx impacted --since main -p "src/**/*.spec.js"
21
24
 
22
25
  # Multiple patterns
23
- git diff --name-only main | npx impacted -p "test/**/*.test.js" -p "test/**/*.spec.js"
26
+ npx impacted --since main -p "test/**/*.test.js" -p "test/**/*.spec.js"
24
27
  ```
25
28
 
26
29
  ## GitHub Action
@@ -54,9 +57,25 @@ const tests = await findImpacted({
54
57
  });
55
58
  ```
56
59
 
60
+ ### `node:test` `run()` integration
61
+
62
+ ```javascript
63
+ import { run } from 'node:test';
64
+ import { findImpacted } from 'impacted';
65
+
66
+ const files = await findImpacted({
67
+ changedFiles: ['src/utils.js'],
68
+ testFiles: 'test/**/*.test.js',
69
+ });
70
+
71
+ run({ files });
72
+ ```
73
+
74
+ See [examples/05-node-test-run](./examples/05-node-test-run) for a full working example.
75
+
57
76
  ## Limitations
58
77
 
59
- - JavaScript only (`.js`, `.mjs`, `.cjs`, `.jsx`) — no TypeScript yet
78
+ - JavaScript only (`.js`, `.mjs`, `.cjs`, `.jsx`)
60
79
  - Static analysis only — dynamic `require(variable)` not supported
61
80
  - Local files only — `node_modules` changes won't trigger tests
62
81
 
package/bin/impacted.js CHANGED
@@ -1,35 +1,23 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ import { execSync } from 'node:child_process';
3
4
  import { findImpacted } from '../src/index.js';
4
5
 
5
6
  const args = process.argv.slice(2);
6
7
 
7
8
  // Parse -p/--pattern flags (supports multiple: -p "*.test.js" -p "*.spec.js")
8
9
  const patterns = [];
10
+ let since = null;
9
11
  for (let i = 0; i < args.length; i++) {
10
12
  if ((args[i] === '-p' || args[i] === '--pattern') && args[i + 1]) {
11
13
  patterns.push(args[++i]);
14
+ } else if (args[i] === '--since' && args[i + 1]) {
15
+ since = args[++i];
12
16
  }
13
17
  }
14
18
  const pattern = patterns.length > 0 ? patterns : '**/*.{test,spec}.{js,mjs,cjs,jsx}';
15
19
 
16
- // Read changed files from stdin
17
- let input = '';
18
- process.stdin.setEncoding('utf8');
19
-
20
- process.stdin.on('readable', () => {
21
- let chunk;
22
- while ((chunk = process.stdin.read()) !== null) {
23
- input += chunk;
24
- }
25
- });
26
-
27
- process.stdin.on('end', async () => {
28
- const changedFiles = input
29
- .split('\n')
30
- .map((line) => line.trim())
31
- .filter((line) => line.length > 0);
32
-
20
+ async function run(changedFiles) {
33
21
  if (changedFiles.length === 0) {
34
22
  process.exit(0);
35
23
  }
@@ -42,4 +30,41 @@ process.stdin.on('end', async () => {
42
30
  for (const file of impacted) {
43
31
  console.log(file);
44
32
  }
45
- });
33
+ }
34
+
35
+ // --since <ref>: get changed files from git diff
36
+ if (since) {
37
+ let output;
38
+ try {
39
+ output = execSync(`git diff --name-only ${since}`, { encoding: 'utf8' });
40
+ } catch {
41
+ process.exit(1);
42
+ }
43
+
44
+ const changedFiles = output
45
+ .split('\n')
46
+ .map((line) => line.trim())
47
+ .filter((line) => line.length > 0);
48
+
49
+ await run(changedFiles);
50
+ } else {
51
+ // Read changed files from stdin
52
+ let input = '';
53
+ process.stdin.setEncoding('utf8');
54
+
55
+ process.stdin.on('readable', () => {
56
+ let chunk;
57
+ while ((chunk = process.stdin.read()) !== null) {
58
+ input += chunk;
59
+ }
60
+ });
61
+
62
+ process.stdin.on('end', async () => {
63
+ const changedFiles = input
64
+ .split('\n')
65
+ .map((line) => line.trim())
66
+ .filter((line) => line.length > 0);
67
+
68
+ await run(changedFiles);
69
+ });
70
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impacted",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Find test files impacted by code changes using static dependency analysis",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",