|
1 | 1 | # RSpec::EnrichedJson |
2 | 2 |
|
| 3 | +[](https://github.com/firstdraft/rspec-enriched_json/actions/workflows/ci.yml) |
| 4 | +[](https://badge.fury.io/rb/rspec-enriched_json) |
| 5 | +[](https://github.com/standardrb/standard) |
| 6 | + |
3 | 7 | A drop-in replacement for RSpec's built-in JSON formatter that enriches the output with structured failure data. This makes it easy to programmatically analyze test results, extract expected/actual values, and build better CI/CD integrations. |
4 | 8 |
|
5 | 9 | ## Quick Demo |
@@ -30,7 +34,7 @@ This interactive demo script runs the same failing tests with both formatters an |
30 | 34 | Add this line to your application's Gemfile: |
31 | 35 |
|
32 | 36 | ```ruby |
33 | | -gem 'rspec-enriched_json' |
| 37 | +gem "rspec-enriched_json" |
34 | 38 | ``` |
35 | 39 |
|
36 | 40 | And then execute: |
@@ -95,14 +99,14 @@ With this gem, you get structured data alongside the original message: |
95 | 99 |
|
96 | 100 | ## Features |
97 | 101 |
|
98 | | -- **Drop-in replacement**: Inherits from RSpec's JsonFormatter, maintaining 100% compatibility |
99 | | -- **Structured data extraction**: Expected and actual values as proper JSON objects |
100 | | -- **Rich object support**: Arrays, hashes, and custom objects are properly serialized |
101 | | -- **Original message preservation**: When you override with a custom message, the original is preserved |
102 | | -- **Graceful degradation**: Regular exceptions (non-expectation failures) work normally |
103 | | -- **Enhanced metadata capture**: Test location, tags, hierarchy, and custom metadata |
104 | | -- **Robust error recovery**: Handles objects that fail to serialize without crashing |
105 | | -- **Diff information**: Includes `diffable` to help tools determine if values can be meaningfully diffed |
| 102 | +- **Drop-in replacement**: Inherits from RSpec's JsonFormatter, maintaining 100% compatibility. |
| 103 | +- **Structured data extraction**: Expected and actual values as proper JSON objects. |
| 104 | +- **Rich object support**: Arrays, hashes, and custom objects are properly serialized. |
| 105 | +- **Original message preservation**: When you override with a custom message, the original is preserved. |
| 106 | +- **Graceful degradation**: Regular exceptions (non-expectation failures) work normally. |
| 107 | +- **Enhanced metadata capture**: Test location, tags, hierarchy, and custom metadata. |
| 108 | +- **Robust error recovery**: Handles objects that fail to serialize without crashing. |
| 109 | +- **Diff information**: Includes `diffable` to help tools determine if values can be meaningfully diffed. |
106 | 110 |
|
107 | 111 | ## Examples |
108 | 112 |
|
@@ -147,36 +151,50 @@ end |
147 | 151 |
|
148 | 152 | ## Use Cases |
149 | 153 |
|
150 | | -- **CI/CD Integration**: Parse test results to create rich error reports |
151 | | -- **Test Analytics**: Track which values commonly cause test failures |
152 | | -- **Debugging Tools**: Build tools that can display expected vs actual diffs |
153 | | -- **Learning Platforms**: Provide detailed feedback on why tests failed |
| 154 | +- **CI/CD Integration**: Parse test results to create rich error reports. |
| 155 | +- **Test Analytics**: Track which values commonly cause test failures. |
| 156 | +- **Debugging Tools**: Build tools that can display expected vs actual diffs. |
| 157 | +- **Learning Platforms**: Provide detailed feedback on why tests failed. |
154 | 158 |
|
155 | 159 | ## How It Works |
156 | 160 |
|
157 | 161 | The gem works by: |
158 | 162 |
|
159 | | -1. Patching RSpec's expectation system to capture structured data when expectations fail |
160 | | -2. Extending the JsonFormatter to include this data in the JSON output |
161 | | -3. Maintaining full backward compatibility with existing tools |
| 163 | +1. Patching RSpec's expectation system to capture structured data when expectations fail. |
| 164 | +2. Extending the JsonFormatter to include this data in the JSON output. |
| 165 | +3. Maintaining full backward compatibility with existing tools. |
162 | 166 |
|
163 | 167 | ## Development |
164 | 168 |
|
165 | 169 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. |
166 | 170 |
|
167 | | -## Performance Considerations |
| 171 | +This project uses [StandardRB](https://github.com/standardrb/standard) for code formatting and style. Before committing: |
| 172 | + |
| 173 | +```bash |
| 174 | +# Check for style violations |
| 175 | +bundle exec standardrb |
| 176 | + |
| 177 | +# Auto-fix style violations |
| 178 | +bundle exec standardrb --fix |
| 179 | +``` |
| 180 | + |
| 181 | +## Additional Features |
| 182 | + |
| 183 | +### Passing Test Value Capture |
| 184 | +The formatter also captures expected/actual values for passing tests, useful for: |
| 185 | +- Test analytics and insights. |
| 186 | +- Understanding test coverage patterns. |
| 187 | +- Debugging flaky tests. |
168 | 188 |
|
169 | | -The enriched formatter adds minimal overhead: |
170 | | -- Only processes failing tests (passing tests have no extra processing) |
171 | | -- Limits serialization depth to prevent infinite recursion |
172 | | -- Truncates large strings and collections to maintain reasonable output sizes |
173 | | -- No impact on test execution time, only on failure reporting |
| 189 | +### Negation Detection |
| 190 | +Tests using `not_to` or `to_not` include a `negated: true` flag in the details. |
174 | 191 |
|
175 | | -Default limits: |
176 | | -- Max serialization depth: 5 levels |
177 | | -- Max array size: 100 items |
178 | | -- Max hash size: 100 keys |
179 | | -- Max string length: 1000 characters |
| 192 | +### Serialization |
| 193 | +Values are serialized using [Oj](https://github.com/ohler55/oj) in object mode, providing: |
| 194 | +- Circular reference handling. |
| 195 | +- Proper Ruby object serialization. |
| 196 | +- Excellent performance. |
| 197 | +- Special handling for Regexp objects (serialized as inspect strings). |
180 | 198 |
|
181 | 199 | ## Contributing |
182 | 200 |
|
|
0 commit comments