Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit e145979

Browse files
committed
Add Plugins unit tests
1 parent 9ea9bc6 commit e145979

3 files changed

Lines changed: 163 additions & 28 deletions

File tree

jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = {
1717
// An object that configures minimum threshold enforcement for coverage results
1818
coverageThreshold: {
1919
global: {
20-
branches: 93,
21-
functions: 93,
22-
lines: 93,
23-
statements: 93
20+
branches: 100,
21+
functions: 100,
22+
lines: 100,
23+
statements: 100
2424
}
2525
},
2626

lib/core/Plugins.js

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ class Plugins {
5151
}
5252

5353
_catchInitError(error) {
54+
this._pluginsInitialized = this._pluginsInitialized - 1;
5455
tracer.error("Error initializating plugin");
5556
tracer.debug(error);
5657
return Promise.resolve();
5758
}
5859

5960
_catchStartError(error) {
61+
this._pluginsStarted = this._pluginsStarted - 1;
6062
tracer.error("Error starting plugin");
6163
tracer.debug(error);
6264
return Promise.resolve();
@@ -102,65 +104,57 @@ class Plugins {
102104
}
103105

104106
_initPlugins(pluginIndex = 0) {
105-
if (!this._pluginsInstances[pluginIndex]) {
107+
if (pluginIndex === this._pluginsInstances.length) {
106108
return Promise.resolve();
107109
}
110+
this._pluginsInitialized++;
108111
tracer.debug(`Initializing plugin ${pluginIndex}`);
109112
const initNextPlugin = () => {
110113
return this._initPlugins(pluginIndex + 1);
111114
};
112115

113116
if (!this._pluginsInstances[pluginIndex].init) {
117+
this._pluginsInitialized = this._pluginsInitialized - 1;
114118
return initNextPlugin();
115119
}
116120
let pluginInit;
117121
try {
118122
pluginInit = this._pluginsInstances[pluginIndex].init();
119123
} catch (error) {
120-
return this._catchInitError(error);
124+
return this._catchInitError(error).then(initNextPlugin);
121125
}
122126

123127
if (!isPromise(pluginInit)) {
124-
this._pluginsInitialized++;
125-
return Promise.resolve();
128+
return initNextPlugin();
126129
}
127-
return pluginInit
128-
.then(() => {
129-
this._pluginsInitialized++;
130-
return Promise.resolve();
131-
})
132-
.then(initNextPlugin)
133-
.catch(this._catchInitError);
130+
return pluginInit.catch(this._catchInitError).then(initNextPlugin);
134131
}
135132

136133
_startPlugins(pluginIndex = 0) {
137-
if (!this._pluginsInstances[pluginIndex]) {
134+
if (pluginIndex === this._pluginsInstances.length) {
138135
return Promise.resolve();
139136
}
137+
this._pluginsStarted++;
140138
tracer.debug(`Starting plugin ${pluginIndex}`);
141139
const startNextPlugin = () => {
142140
return this._startPlugins(pluginIndex + 1);
143141
};
142+
144143
if (!this._pluginsInstances[pluginIndex].start) {
144+
this._pluginsStarted = this._pluginsStarted - 1;
145145
return startNextPlugin();
146146
}
147147
let pluginStart;
148148
try {
149149
pluginStart = this._pluginsInstances[pluginIndex].start();
150150
} catch (error) {
151-
return this._catchStartError(error);
151+
return this._catchStartError(error).then(startNextPlugin);
152152
}
153+
153154
if (!isPromise(pluginStart)) {
154-
this._pluginsStarted++;
155-
return Promise.resolve();
155+
return startNextPlugin();
156156
}
157-
return pluginStart
158-
.then(() => {
159-
this._pluginsStarted++;
160-
return Promise.resolve();
161-
})
162-
.then(startNextPlugin)
163-
.catch(this._catchStartError);
157+
return pluginStart.catch(this._catchStartError).then(startNextPlugin);
164158
}
165159
}
166160

test/unit/core/Plugins.spec.js

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,13 @@ describe("Settings", () => {
6666
expect(tracer.verbose.calledWith("Registered 0 plugins")).toEqual(true);
6767
});
6868

69-
it("should register function plugins", async () => {
70-
const fooPlugin = () => {};
69+
it("should register function plugins executing them passing the core", async () => {
70+
expect.assertions(3);
71+
const fooPlugin = sinon.spy();
7172
plugins = new Plugins([fooPlugin], coreInstance);
7273
await plugins.register();
74+
expect(fooPlugin.calledWith(coreInstance)).toEqual(true);
75+
expect(fooPlugin.callCount).toEqual(1);
7376
expect(tracer.verbose.calledWith("Registered 1 plugins")).toEqual(true);
7477
});
7578

@@ -121,4 +124,142 @@ describe("Settings", () => {
121124
expect(tracer.verbose.calledWith("Registered 3 plugins")).toEqual(true);
122125
});
123126
});
127+
128+
describe("init method", () => {
129+
it("should do nothing if there are no plugins to register", async () => {
130+
plugins = new Plugins(null, coreInstance);
131+
await plugins.register();
132+
await plugins.init();
133+
expect(tracer.verbose.calledWith("Initializated 0 plugins")).toEqual(true);
134+
});
135+
136+
it("should init object plugins with an init property", async () => {
137+
expect.assertions(2);
138+
const fooPlugin = {
139+
init: sinon.spy()
140+
};
141+
plugins = new Plugins([fooPlugin], coreInstance);
142+
await plugins.register();
143+
await plugins.init();
144+
expect(fooPlugin.init.callCount).toEqual(1);
145+
expect(tracer.verbose.calledWith("Initializated 1 plugins")).toEqual(true);
146+
});
147+
148+
it("should accept init methods non returning a Promise", async () => {
149+
expect.assertions(1);
150+
const fooPlugin = {
151+
init: () => true
152+
};
153+
const fooPlugin2 = {
154+
init: () => Promise.resolve()
155+
};
156+
plugins = new Plugins([fooPlugin, fooPlugin2], coreInstance);
157+
await plugins.register();
158+
await plugins.init();
159+
expect(tracer.verbose.calledWith("Initializated 2 plugins")).toEqual(true);
160+
});
161+
162+
it("should catch init method errors", async () => {
163+
expect.assertions(1);
164+
const fooPlugin = {
165+
init: () => {
166+
throw new Error();
167+
}
168+
};
169+
const fooPlugin2 = {
170+
init: () => Promise.resolve()
171+
};
172+
const fooPlugin3 = {
173+
init: () => Promise.resolve()
174+
};
175+
plugins = new Plugins([fooPlugin, fooPlugin2, fooPlugin3], coreInstance);
176+
await plugins.register();
177+
await plugins.init();
178+
expect(tracer.verbose.calledWith("Initializated 2 plugins")).toEqual(true);
179+
});
180+
181+
it("should accept plugins with no init method", async () => {
182+
expect.assertions(1);
183+
const fooPlugin = {};
184+
const fooPlugin2 = {
185+
init: () => Promise.resolve()
186+
};
187+
const fooPlugin3 = {
188+
init: () => Promise.resolve()
189+
};
190+
plugins = new Plugins([fooPlugin, fooPlugin2, fooPlugin3], coreInstance);
191+
await plugins.register();
192+
await plugins.init();
193+
expect(tracer.verbose.calledWith("Initializated 2 plugins")).toEqual(true);
194+
});
195+
});
196+
197+
describe("start method", () => {
198+
it("should do nothing if there are no plugins to register", async () => {
199+
plugins = new Plugins(null, coreInstance);
200+
await plugins.register();
201+
await plugins.start();
202+
expect(tracer.verbose.calledWith("Started 0 plugins")).toEqual(true);
203+
});
204+
205+
it("should start object plugins with an init property", async () => {
206+
expect.assertions(2);
207+
const fooPlugin = {
208+
start: sinon.spy()
209+
};
210+
plugins = new Plugins([fooPlugin], coreInstance);
211+
await plugins.register();
212+
await plugins.start();
213+
expect(fooPlugin.start.callCount).toEqual(1);
214+
expect(tracer.verbose.calledWith("Started 1 plugins")).toEqual(true);
215+
});
216+
217+
it("should accept start methods non returning a Promise", async () => {
218+
expect.assertions(1);
219+
const fooPlugin = {
220+
start: () => true
221+
};
222+
const fooPlugin2 = {
223+
start: () => Promise.resolve()
224+
};
225+
plugins = new Plugins([fooPlugin, fooPlugin2], coreInstance);
226+
await plugins.register();
227+
await plugins.start();
228+
expect(tracer.verbose.calledWith("Started 2 plugins")).toEqual(true);
229+
});
230+
231+
it("should catch start method errors", async () => {
232+
expect.assertions(1);
233+
const fooPlugin = {
234+
start: () => {
235+
throw new Error();
236+
}
237+
};
238+
const fooPlugin2 = {
239+
start: () => Promise.resolve()
240+
};
241+
const fooPlugin3 = {
242+
start: () => Promise.resolve()
243+
};
244+
plugins = new Plugins([fooPlugin, fooPlugin2, fooPlugin3], coreInstance);
245+
await plugins.register();
246+
await plugins.start();
247+
expect(tracer.verbose.calledWith("Started 2 plugins")).toEqual(true);
248+
});
249+
250+
it("should accept plugins with no start method", async () => {
251+
expect.assertions(1);
252+
const fooPlugin = {};
253+
const fooPlugin2 = {
254+
start: () => Promise.resolve()
255+
};
256+
const fooPlugin3 = {
257+
start: () => Promise.resolve()
258+
};
259+
plugins = new Plugins([fooPlugin, fooPlugin2, fooPlugin3], coreInstance);
260+
await plugins.register();
261+
await plugins.start();
262+
expect(tracer.verbose.calledWith("Started 2 plugins")).toEqual(true);
263+
});
264+
});
124265
});

0 commit comments

Comments
 (0)