impacted 0.0.4 → 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 +28 -6
- package/bin/impacted.js +50 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,15 +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 $(
|
|
17
|
-
jest $(
|
|
19
|
+
vitest $(npx impacted --since main)
|
|
20
|
+
jest $(npx impacted --since main)
|
|
18
21
|
|
|
19
22
|
# Custom test pattern
|
|
20
|
-
|
|
23
|
+
npx impacted --since main -p "src/**/*.spec.js"
|
|
24
|
+
|
|
25
|
+
# Multiple patterns
|
|
26
|
+
npx impacted --since main -p "test/**/*.test.js" -p "test/**/*.spec.js"
|
|
21
27
|
```
|
|
22
28
|
|
|
23
29
|
## GitHub Action
|
|
@@ -30,7 +36,7 @@ git diff --name-only main | npx impacted -p "src/**/*.spec.js"
|
|
|
30
36
|
- uses: sozua/impacted@v1
|
|
31
37
|
id: impacted
|
|
32
38
|
with:
|
|
33
|
-
pattern: '**/*.test.js'
|
|
39
|
+
pattern: '**/*.{test,spec}.{js,mjs,cjs,jsx}' # default
|
|
34
40
|
|
|
35
41
|
- name: Run impacted tests
|
|
36
42
|
if: steps.impacted.outputs.has-impacted == 'true'
|
|
@@ -51,9 +57,25 @@ const tests = await findImpacted({
|
|
|
51
57
|
});
|
|
52
58
|
```
|
|
53
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
|
+
|
|
54
76
|
## Limitations
|
|
55
77
|
|
|
56
|
-
- JavaScript only (`.js`, `.mjs`, `.cjs`, `.jsx`)
|
|
78
|
+
- JavaScript only (`.js`, `.mjs`, `.cjs`, `.jsx`)
|
|
57
79
|
- Static analysis only — dynamic `require(variable)` not supported
|
|
58
80
|
- Local files only — `node_modules` changes won't trigger tests
|
|
59
81
|
|
package/bin/impacted.js
CHANGED
|
@@ -1,33 +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
|
-
// Parse -p/--pattern
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
pattern
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
let input = '';
|
|
16
|
-
process.stdin.setEncoding('utf8');
|
|
17
|
-
|
|
18
|
-
process.stdin.on('readable', () => {
|
|
19
|
-
let chunk;
|
|
20
|
-
while ((chunk = process.stdin.read()) !== null) {
|
|
21
|
-
input += chunk;
|
|
8
|
+
// Parse -p/--pattern flags (supports multiple: -p "*.test.js" -p "*.spec.js")
|
|
9
|
+
const patterns = [];
|
|
10
|
+
let since = null;
|
|
11
|
+
for (let i = 0; i < args.length; i++) {
|
|
12
|
+
if ((args[i] === '-p' || args[i] === '--pattern') && args[i + 1]) {
|
|
13
|
+
patterns.push(args[++i]);
|
|
14
|
+
} else if (args[i] === '--since' && args[i + 1]) {
|
|
15
|
+
since = args[++i];
|
|
22
16
|
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
process.stdin.on('end', async () => {
|
|
26
|
-
const changedFiles = input
|
|
27
|
-
.split('\n')
|
|
28
|
-
.map((line) => line.trim())
|
|
29
|
-
.filter((line) => line.length > 0);
|
|
17
|
+
}
|
|
18
|
+
const pattern = patterns.length > 0 ? patterns : '**/*.{test,spec}.{js,mjs,cjs,jsx}';
|
|
30
19
|
|
|
20
|
+
async function run(changedFiles) {
|
|
31
21
|
if (changedFiles.length === 0) {
|
|
32
22
|
process.exit(0);
|
|
33
23
|
}
|
|
@@ -40,4 +30,41 @@ process.stdin.on('end', async () => {
|
|
|
40
30
|
for (const file of impacted) {
|
|
41
31
|
console.log(file);
|
|
42
32
|
}
|
|
43
|
-
}
|
|
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
|
+
}
|