Skip to content

Commit 337daf7

Browse files
author
Bruno Rocha
committed
Update to yaml tests
1 parent 9857f5f commit 337daf7

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

tests/toYaml.test.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toYaml } from '../src/converters/toYaml';
1+
import toYaml from '../src/converters/toYaml';
22

33
describe('toYaml', () => {
44
it('should convert a JSON object to YAML format', () => {
@@ -8,27 +8,26 @@ describe('toYaml', () => {
88
features: ['JSON to YAML Conversion'],
99
};
1010

11-
const expectedYaml = `name: JsonWeaver\nversion: 1.0.3\nfeatures:\n - JSON to YAML Conversion\n`;
11+
const expectedYaml = `name: JsonWeaver
12+
version: 1.0.3
13+
features:
14+
- JSON to YAML Conversion
15+
`;
1216

1317
const yamlOutput = toYaml(jsonInput);
18+
1419
expect(yamlOutput).toBe(expectedYaml);
1520
});
1621

1722
it('should handle empty JSON objects', () => {
18-
const jsonInput = {};
19-
const expectedYaml = '{}\n';
23+
const yamlOutput = toYaml({});
2024

21-
const yamlOutput = toYaml(jsonInput);
22-
expect(yamlOutput).toBe(expectedYaml);
25+
expect(yamlOutput).toBe('{}\n');
2326
});
2427

2528
it('should throw an error for invalid inputs', () => {
26-
expect(() => toYaml(null as unknown as object)).toThrow(
27-
'Conversion error: Input must be a valid JSON string or object.'
28-
);
29-
expect(() => toYaml(undefined as unknown as object)).toThrow(
30-
'Conversion error: Input must be a valid JSON string or object.'
31-
);
29+
expect(() => toYaml(null as unknown as object)).toThrow('Conversion error: Input must be a valid JSON string or object.');
30+
expect(() => toYaml(undefined as unknown as object)).toThrow('Conversion error: Input must be a valid JSON string or object.');
3231
});
3332

3433
it('should correctly handle nested JSON objects', () => {
@@ -42,9 +41,15 @@ describe('toYaml', () => {
4241
},
4342
};
4443

45-
const expectedYaml = `person:\n name: John\n address:\n city: New York\n zip: '10001'\n`;
44+
const expectedYaml = `person:
45+
name: John
46+
address:
47+
city: New York
48+
zip: '10001'
49+
`;
4650

4751
const yamlOutput = toYaml(jsonInput);
52+
4853
expect(yamlOutput).toBe(expectedYaml);
4954
});
5055

@@ -53,22 +58,31 @@ describe('toYaml', () => {
5358
items: ['item1', 'item2', 'item3'],
5459
};
5560

56-
const expectedYaml = `items:\n - item1\n - item2\n - item3\n`;
61+
const expectedYaml = `items:
62+
- item1
63+
- item2
64+
- item3
65+
`;
5766

5867
const yamlOutput = toYaml(jsonInput);
68+
5969
expect(yamlOutput).toBe(expectedYaml);
6070
});
6171

62-
it('should handle numbers, booleans, and null', () => {
72+
it('should handle numbers, booleans, and null values', () => {
6373
const jsonInput = {
6474
number: 42,
6575
boolean: true,
6676
nullValue: null,
6777
};
6878

69-
const expectedYaml = `number: 42\nboolean: true\nnullValue: null\n`;
79+
const expectedYaml = `number: 42
80+
boolean: true
81+
nullValue: null
82+
`;
7083

7184
const yamlOutput = toYaml(jsonInput);
85+
7286
expect(yamlOutput).toBe(expectedYaml);
7387
});
7488

@@ -78,38 +92,46 @@ describe('toYaml', () => {
7892
emoji: '😊',
7993
};
8094

81-
const expectedYaml = `text: |-\n Line1\n Line2\nemoji: 😊\n`;
95+
const expectedYaml = `text: |-
96+
Line1
97+
Line2
98+
emoji: 😊
99+
`;
82100

83101
const yamlOutput = toYaml(jsonInput);
102+
84103
expect(yamlOutput).toBe(expectedYaml);
85104
});
86105

87-
88106
it('should handle large JSON objects', () => {
89107
const jsonInput = Array.from({ length: 1000 }, (_, i) => ({ index: i }));
90108

91109
expect(() => toYaml(jsonInput)).not.toThrow();
92110
});
93111

94-
it('should produce consistent YAML output for YAML-like input', () => {
112+
it('should produce consistent YAML output for YAML-like strings', () => {
95113
const jsonInput = {
96114
yamlLike: `key: value`,
97115
};
98116

99-
const expectedYaml = `yamlLike: 'key: value'\n`;
117+
const expectedYaml = `yamlLike: 'key: value'
118+
`;
100119

101120
const yamlOutput = toYaml(jsonInput);
121+
102122
expect(yamlOutput).toBe(expectedYaml);
103123
});
104124

105-
it('should handle numeric keys in objects', () => {
125+
it('should handle numeric keys in JSON objects', () => {
106126
const jsonInput = {
107127
123: 'value',
108128
};
109129

110-
const expectedYaml = `'123': value\n`;
130+
const expectedYaml = `'123': value
131+
`;
111132

112133
const yamlOutput = toYaml(jsonInput);
134+
113135
expect(yamlOutput).toBe(expectedYaml);
114136
});
115137
});

0 commit comments

Comments
 (0)