Skip to content

Commit 3de4dc4

Browse files
committed
Fixed many broken unit tests and made submit return a promise.
1 parent f33943b commit 3de4dc4

File tree

2 files changed

+87
-78
lines changed

2 files changed

+87
-78
lines changed

src/exceptionless-spec.ts

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@
44
//import { Configuration, ExceptionlessClient } from 'Exceptionless';
55
module Exceptionless {
66
describe('ExceptionlessClient', () => {
7-
it('should use event reference ids', () => {
7+
it('should use event reference ids', (done) => {
88
var error = new Error('From Unit Test');
99

1010
var client = new ExceptionlessClient('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
1111
expect(client.config.lastReferenceIdManager.getLast()).toBe(null);
12-
client.submitException(error);
13-
expect(client.config.lastReferenceIdManager.getLast()).toBe(null);
12+
client.submitException(error).then(
13+
() => expect(client.config.lastReferenceIdManager.getLast()).toBe(null),
14+
() => expect(client.config.lastReferenceIdManager.getLast()).toBe(null)
15+
);
1416

1517
var numberOfPlugins = client.config.plugins.length;
1618
client.config.useReferenceIds();
1719
expect(client.config.plugins.length).toBe(numberOfPlugins + 1);
1820

19-
client.submitException(error);
20-
expect(client.config.lastReferenceIdManager.getLast()).not.toBe(null);
21-
});
21+
client.submitException(error)
22+
.then(
23+
() => expect(client.config.lastReferenceIdManager.getLast()).not.toBe(null),
24+
() => expect(client.config.lastReferenceIdManager.getLast()).toBe(null))
25+
.then(done);
26+
}, 5000);
2227
});
2328

2429
describe('Configuration', () => {
@@ -48,8 +53,8 @@ module Exceptionless {
4853
it('should not add duplicate plugin', () => {
4954
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
5055
expect(config.plugins).not.toBe(null);
51-
for (var plugin of config.plugins) {
52-
config.removePlugin(plugin);
56+
while(config.plugins.length > 0) {
57+
config.removePlugin(config.plugins[0]);
5358
}
5459

5560
config.addPlugin('test', 20, (context:EventPluginContext) => {});
@@ -60,8 +65,8 @@ module Exceptionless {
6065
it('should generate plugin name and priority', () => {
6166
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
6267
expect(config.plugins).not.toBe(null);
63-
for (var plugin of config.plugins) {
64-
config.removePlugin(plugin);
68+
while(config.plugins.length > 0) {
69+
config.removePlugin(config.plugins[0]);
6570
}
6671

6772
config.addPlugin(null, null, (context:EventPluginContext) => {});
@@ -73,8 +78,8 @@ module Exceptionless {
7378
it('should sort plugins by priority', () => {
7479
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
7580
expect(config.plugins).not.toBe(null);
76-
for (var plugin of config.plugins) {
77-
config.removePlugin(plugin);
81+
while(config.plugins.length > 0) {
82+
config.removePlugin(config.plugins[0]);
7883
}
7984

8085
config.addPlugin('3', 3, (context:EventPluginContext) => {});
@@ -149,6 +154,28 @@ module Exceptionless {
149154
.then(done);
150155
}, 5000);
151156

157+
158+
it('should submit invalid object data', (done) => {
159+
function processResponse(response:SubmissionResponse) {
160+
if (response.success) {
161+
expect(response.message).toBe(null);
162+
} else {
163+
expect(response.message).toBe('Unable to connect to server.');
164+
}
165+
}
166+
167+
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
168+
var event:IEvent = { type: 'log', message: 'From js client', reference_id: '123454321', data: {
169+
name: 'blake',
170+
age: function() { throw new Error('Test'); }
171+
}};
172+
173+
var submissionClient = new SubmissionClient();
174+
submissionClient.submit([event], config)
175+
.then(processResponse, processResponse)
176+
.then(done);
177+
}, 5000);
178+
152179
it('should submit user description', (done) => {
153180
function processResponse(response:SubmissionResponse) {
154181
if (response.success) {
@@ -182,12 +209,6 @@ module Exceptionless {
182209
.then(processResponse, processResponse)
183210
.then(done);
184211
}, 5000);
185-
186-
it('should submit invalid object data', (done) => {
187-
//{setPropertybject: {foo: 'bar'}}, function() {
188-
// throw new Error('foo');
189-
//});
190-
});
191212
});
192213

193214
describe('Storage', () => {
@@ -265,30 +286,16 @@ module Exceptionless {
265286
});
266287
});
267288

268-
// TODO: We should move this logic out into an exported utils class to make it easier to test.
269289
describe('ModuleInfoPlugin', () => {
270-
it('should parse version from script source', () => {
271-
function getVersion(source:string) {
272-
if (!source) {
273-
return null;
274-
}
275-
276-
var versionRegex = /(v?((\d+)\.(\d+)(\.(\d+))?)(?:-([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?(?:\+([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?)/;
277-
var matches = versionRegex.exec(source);
278-
if (matches && matches.length > 0) {
279-
return matches[0];
280-
}
281-
282-
return null;
283-
}
284-
285-
expect(getVersion('https://code.jquery.com/jquery-2.1.3.js')).toBe('2.1.3');
286-
expect(getVersion('//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css')).toBe('3.3.4');
287-
expect(getVersion('https://cdnjs.cloudflare.com/ajax/libs/1140/2.0/1140.css')).toBe('2.0');
288-
expect(getVersion('https://cdnjs.cloudflare.com/ajax/libs/Base64/0.3.0/base64.min.js')).toBe('0.3.0');
289-
expect(getVersion('https://cdnjs.cloudflare.com/ajax/libs/angular-google-maps/2.1.0-X.10/angular-google-maps.min.js')).toBe('2.1.0-X.10');
290-
expect(getVersion('https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/2.1.8-M1/swagger-ui.min.js')).toBe('2.1.8-M1');
291-
expect(getVersion('https://cdnjs.cloudflare.com/BLAH/BLAH.min.js')).toBe(null);
292-
});
290+
it('should parse version from script source', () => {
291+
var mi = new Exceptionless.ModuleInfoPlugin();
292+
expect(mi.getVersion('https://code.jquery.com/jquery-2.1.3.js')).toBe('2.1.3');
293+
expect(mi.getVersion('//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css')).toBe('3.3.4');
294+
expect(mi.getVersion('https://cdnjs.cloudflare.com/ajax/libs/1140/2.0/1140.css')).toBe('2.0');
295+
expect(mi.getVersion('https://cdnjs.cloudflare.com/ajax/libs/Base64/0.3.0/base64.min.js')).toBe('0.3.0');
296+
expect(mi.getVersion('https://cdnjs.cloudflare.com/ajax/libs/angular-google-maps/2.1.0-X.10/angular-google-maps.min.js')).toBe('2.1.0-X.10');
297+
expect(mi.getVersion('https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/2.1.8-M1/swagger-ui.min.js')).toBe('2.1.8-M1');
298+
expect(mi.getVersion('https://cdnjs.cloudflare.com/BLAH/BLAH.min.js')).toBe(null);
299+
});
293300
});
294301
}

src/exceptionless.ts

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// TODO: Handle Server Settings
77
// TODO: Lock configuration.
88
// TODO: Look into using templated strings `${1 + 1}`.
9-
// TODO: Add plugin for populating module info.
109

1110
module Exceptionless {
1211
export class ExceptionlessClient {
@@ -22,8 +21,8 @@ module Exceptionless {
2221
return this.createEvent(pluginContextData).setType('error');
2322
}
2423

25-
public submitException(exception:Error): void {
26-
this.createException(exception).submit();
24+
public submitException(exception:Error): Promise<any> {
25+
return this.createException(exception).submit();
2726
}
2827

2928
public createUnhandledException(exception:Error): EventBuilder {
@@ -33,16 +32,16 @@ module Exceptionless {
3332
return builder;
3433
}
3534

36-
public submitUnhandledException(exception:Error): void {
37-
this.createUnhandledException(exception).submit();
35+
public submitUnhandledException(exception:Error): Promise<any> {
36+
return this.createUnhandledException(exception).submit();
3837
}
3938

4039
public createFeatureUsage(feature:string): EventBuilder {
4140
return this.createEvent().setType('usage').setSource(feature);
4241
}
4342

44-
public submitFeatureUsage(feature:string): void {
45-
this.createFeatureUsage(feature).submit();
43+
public submitFeatureUsage(feature:string): Promise<any> {
44+
return this.createFeatureUsage(feature).submit();
4645
}
4746

4847
public createLog(message:string): EventBuilder;
@@ -64,57 +63,59 @@ module Exceptionless {
6463
return builder;
6564
}
6665

67-
public submitLog(message:string): void;
68-
public submitLog(source:string, message:string): void;
69-
public submitLog(source:string, message:string, level:string): void;
70-
public submitLog(sourceOrMessage:string, message?:string, level?:string): void {
71-
this.createLog(sourceOrMessage, message, level).submit();
66+
public submitLog(message:string): Promise<any>;
67+
public submitLog(source:string, message:string): Promise<any>;
68+
public submitLog(source:string, message:string, level:string): Promise<any>;
69+
public submitLog(sourceOrMessage:string, message?:string, level?:string): Promise<any> {
70+
return this.createLog(sourceOrMessage, message, level).submit();
7271
}
7372

7473
public createNotFound(resource:string): EventBuilder {
7574
return this.createEvent().setType('404').setSource(resource);
7675
}
7776

78-
public submitNotFound(resource:string): void {
79-
this.createNotFound(resource).submit();
77+
public submitNotFound(resource:string): Promise<any> {
78+
return this.createNotFound(resource).submit();
8079
}
8180

8281
public createSessionStart(sessionId:string): EventBuilder {
8382
return this.createEvent().setType('start').setSessionId(sessionId);
8483
}
8584

86-
public submitSessionStart(sessionId:string): void {
87-
this.createSessionStart(sessionId).submit();
85+
public submitSessionStart(sessionId:string): Promise<any> {
86+
return this.createSessionStart(sessionId).submit();
8887
}
8988

9089
public createSessionEnd(sessionId:string): EventBuilder {
9190
return this.createEvent().setType('end').setSessionId(sessionId);
9291
}
9392

94-
public submitSessionEnd(sessionId:string): void {
95-
this.createSessionEnd(sessionId).submit();
93+
public submitSessionEnd(sessionId:string): Promise<any> {
94+
return this.createSessionEnd(sessionId).submit();
9695
}
9796

9897
public createEvent(pluginContextData?:ContextData): EventBuilder {
9998
return new EventBuilder({ date: new Date() }, this, pluginContextData);
10099
}
101100

102-
public submitEvent(event:IEvent, pluginContextData?:ContextData) {
101+
public submitEvent(event:IEvent, pluginContextData?:ContextData): Promise<any> {
103102
if (!event) {
104-
return;
103+
return Promise.reject(new Error('Unable to submit undefined event.'));
105104
}
106105

107106
if (!this.config.enabled) {
108-
this.config.log.info('Event submission is currently disabled');
109-
return;
107+
var message:string = 'Event submission is currently disabled.';
108+
this.config.log.info(message);
109+
return Promise.reject(new Error(message));
110110
}
111111

112112
var context = new EventPluginContext(this, event, pluginContextData);
113-
EventPluginManager.run(context)
113+
return EventPluginManager.run(context)
114114
.then(() => {
115115
if (context.cancel) {
116-
context.log.info('Event submission cancelled by plugin": id=' + event.reference_id + ' type=' + event.type);
117-
return;
116+
var message:string = 'Event submission cancelled by plugin": id=' + event.reference_id + ' type=' + event.type;
117+
this.config.log.info(message);
118+
return Promise.reject(new Error(message));
118119
}
119120

120121
// ensure all required data
@@ -129,16 +130,17 @@ module Exceptionless {
129130
this.config.log.info('Submitting event: type=' + event.type + !!event.reference_id ? ' refid=' + event.reference_id : '');
130131
this.config.queue.enqueue(event);
131132

132-
if (!event.reference_id || event.reference_id.length === 0) {
133-
return;
133+
if (event.reference_id && event.reference_id.length > 0) {
134+
this.config.log.info('Setting last reference id "' + event.reference_id + '"');
135+
this.config.lastReferenceIdManager.setLast(event.reference_id);
134136
}
135137

136-
this.config.log.info('Setting last reference id "' + event.reference_id + '"');
137-
this.config.lastReferenceIdManager.setLast(event.reference_id);
138+
return Promise.resolve();
138139
})
139140
.catch((error:Error) => {
140-
var message:string = error && error.message ? error.message : <any>error;
141-
this.config.log.error('Event submission cancelled. An error occurred while running the plugins: ' + message);
141+
var message:string = 'Event submission cancelled. An error occurred while running the plugins: ' + error && error.message ? error.message : <any>error;
142+
this.config.log.error(message);
143+
return Promise.reject(new Error(message));
142144
});
143145
}
144146

@@ -865,8 +867,8 @@ module Exceptionless {
865867
return this;
866868
}
867869

868-
public submit(): void {
869-
this.client.submitEvent(this.target, this.pluginContextData);
870+
public submit(): Promise<any> {
871+
return this.client.submitEvent(this.target, this.pluginContextData);
870872
}
871873

872874
private isValidIdentifier(value:string): boolean {
@@ -1078,7 +1080,7 @@ module Exceptionless {
10781080
}
10791081
}
10801082

1081-
class ModuleInfoPlugin implements IEventPlugin {
1083+
export class ModuleInfoPlugin implements IEventPlugin {
10821084
public priority:number = 40;
10831085
public name:string = 'ModuleInfoPlugin';
10841086

@@ -1109,7 +1111,7 @@ module Exceptionless {
11091111
return Promise.resolve();
11101112
}
11111113

1112-
private getVersion(source:string): string {
1114+
public getVersion(source:string): string {
11131115
if (!source) {
11141116
return null;
11151117
}
@@ -1123,7 +1125,7 @@ module Exceptionless {
11231125
return null;
11241126
}
11251127

1126-
private getHashCode(source:string): string {
1128+
public getHashCode(source:string): string {
11271129
if (!source || source.length === 0) {
11281130
return null;
11291131
}

0 commit comments

Comments
 (0)