-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
executable file
·60 lines (48 loc) · 1.64 KB
/
Copy pathtest.js
File metadata and controls
executable file
·60 lines (48 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env node
// Simple test suite for json-formatter-cli
const { execSync } = require('child_process');
const fs = require('fs');
console.log('Running tests...\n');
let passed = 0;
let failed = 0;
function test(name, fn) {
try {
fn();
console.log(`✓ ${name}`);
passed++;
} catch (error) {
console.log(`✗ ${name}`);
console.log(` ${error.message}`);
failed++;
}
}
// Create test file
const testJSON = { name: "test", value: 123, nested: { key: "value" } };
fs.writeFileSync('/tmp/test.json', JSON.stringify(testJSON));
test('Format valid JSON', () => {
const output = execSync('echo \'{"a":1}\' | node index.js', { encoding: 'utf8' });
if (!output.includes('"a"')) throw new Error('Output missing expected content');
});
test('Minify JSON', () => {
const output = execSync('echo \'{"a": 1, "b": 2}\' | node index.js --minify', { encoding: 'utf8' });
if (output.includes(' ')) throw new Error('Output should not contain spaces');
});
test('Validate valid JSON', () => {
execSync('echo \'{"valid":true}\' | node index.js --validate');
});
test('Reject invalid JSON', () => {
try {
execSync('echo \'{invalid}\' | node index.js', { encoding: 'utf8' });
throw new Error('Should have failed on invalid JSON');
} catch (error) {
if (error.status !== 1) throw error;
}
});
test('Format from file', () => {
const output = execSync('node index.js /tmp/test.json', { encoding: 'utf8' });
if (!output.includes('"name"')) throw new Error('Output missing expected content');
});
// Cleanup
fs.unlinkSync('/tmp/test.json');
console.log(`\n${passed} passed, ${failed} failed`);
process.exit(failed > 0 ? 1 : 0);