|
1 | 1 | import { BacktraceReport, BacktraceStringAttachment } from '../../src/index.js'; |
| 2 | +import { AttributeManager } from '../../src/modules/attribute/AttributeManager.js'; |
2 | 3 | import { BacktraceTestClient } from '../mocks/BacktraceTestClient.js'; |
| 4 | +import { testHttpClient } from '../mocks/testHttpClient.js'; |
3 | 5 | describe('Client tests', () => { |
| 6 | + afterEach(() => { |
| 7 | + jest.restoreAllMocks(); |
| 8 | + }); |
| 9 | + |
4 | 10 | describe('Send tests', () => { |
5 | 11 | const client = BacktraceTestClient.buildFakeClient(); |
6 | 12 |
|
@@ -128,4 +134,103 @@ describe('Client tests', () => { |
128 | 134 | ); |
129 | 135 | }); |
130 | 136 | }); |
| 137 | + |
| 138 | + describe('Validation tests', () => { |
| 139 | + it('should throw on initialize when application and application.version attributes are missing', () => { |
| 140 | + const instance = new BacktraceTestClient({}, testHttpClient); |
| 141 | + expect(() => instance.initialize()).toThrow( |
| 142 | + 'application and application.version attributes must be defined.', |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should throw on initialize when application attribute is missing', () => { |
| 147 | + const instance = new BacktraceTestClient({}, testHttpClient, [ |
| 148 | + { |
| 149 | + type: 'scoped', |
| 150 | + get: () => ({ |
| 151 | + 'application.version': '1.2.3', |
| 152 | + }), |
| 153 | + }, |
| 154 | + ]); |
| 155 | + expect(() => instance.initialize()).toThrow( |
| 156 | + 'application and application.version attributes must be defined.', |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should throw on initialize when application.version attribute is missing', () => { |
| 161 | + const instance = new BacktraceTestClient({}, testHttpClient, [ |
| 162 | + { |
| 163 | + type: 'scoped', |
| 164 | + get: () => ({ |
| 165 | + application: 'my-app', |
| 166 | + }), |
| 167 | + }, |
| 168 | + ]); |
| 169 | + expect(() => instance.initialize()).toThrow( |
| 170 | + 'application and application.version attributes must be defined.', |
| 171 | + ); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should not throw on initialize when application and application.version attributes are defined as scoped', () => { |
| 175 | + const instance = new BacktraceTestClient({}, testHttpClient, [ |
| 176 | + { |
| 177 | + type: 'scoped', |
| 178 | + get: () => ({ |
| 179 | + application: 'my-app', |
| 180 | + 'application.version': '1.2.3', |
| 181 | + }), |
| 182 | + }, |
| 183 | + ]); |
| 184 | + expect(() => instance.initialize()).not.toThrow(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should not throw on initialize when application and application.version attributes are defined as dynamic', () => { |
| 188 | + const instance = new BacktraceTestClient({}, testHttpClient, [ |
| 189 | + { |
| 190 | + type: 'dynamic', |
| 191 | + get: () => ({ |
| 192 | + application: 'my-app', |
| 193 | + 'application.version': '1.2.3', |
| 194 | + }), |
| 195 | + }, |
| 196 | + ]); |
| 197 | + expect(() => instance.initialize()).not.toThrow(); |
| 198 | + }); |
| 199 | + |
| 200 | + it('should only test scoped attributes and not all when application and application.version attributes are defined as scoped', () => { |
| 201 | + const instance = new BacktraceTestClient({}, testHttpClient, [ |
| 202 | + { |
| 203 | + type: 'scoped', |
| 204 | + get: () => ({ |
| 205 | + application: 'my-app', |
| 206 | + 'application.version': '1.2.3', |
| 207 | + }), |
| 208 | + }, |
| 209 | + ]); |
| 210 | + |
| 211 | + const getAttributesSpy = jest.spyOn(AttributeManager.prototype, 'get'); |
| 212 | + instance.initialize(); |
| 213 | + |
| 214 | + expect(getAttributesSpy).toHaveBeenCalledWith('scoped'); |
| 215 | + expect(getAttributesSpy).not.toHaveBeenCalledWith(); |
| 216 | + }); |
| 217 | + |
| 218 | + it('should test both scoped attributes and all when application and application.version attributes are defined as dynamic', () => { |
| 219 | + const instance = new BacktraceTestClient({}, testHttpClient, [ |
| 220 | + { |
| 221 | + type: 'dynamic', |
| 222 | + get: () => ({ |
| 223 | + application: 'my-app', |
| 224 | + 'application.version': '1.2.3', |
| 225 | + }), |
| 226 | + }, |
| 227 | + ]); |
| 228 | + |
| 229 | + const getAttributesSpy = jest.spyOn(AttributeManager.prototype, 'get'); |
| 230 | + instance.initialize(); |
| 231 | + |
| 232 | + expect(getAttributesSpy).toHaveBeenCalledWith('scoped'); |
| 233 | + expect(getAttributesSpy).toHaveBeenCalledWith(); |
| 234 | + }); |
| 235 | + }); |
131 | 236 | }); |
0 commit comments