UNPKG

6.31 kBMarkdownView Raw
1# Documentation
2
3## Index
4
5- ### `Class`:
6 - #### [`Gunner.constructor`](#new-gunner-options)
7
8- ### `Methods`:
9 - #### [`Gunner#test`](#gunnertest-title-implementation)
10 - #### [`Gunner#before`](#gunnerbefore-title-implementation)
11 - #### [`Gunner#after`](#gunnerafter-title-implementation)
12 - #### [`Gunner#run`](#gunnerrun-options)
13
14- ### `Constants`:
15 - #### `[Gunner.Start]`
16 - #### `[Gunner.End]`
17
18- ### [`State and Advanced Usage`](#state)
19
20---
21
22## API
23
24### new Gunner (options)
25
26Creates a new Gunner instance.
27
28#### Options
29
30- **`name`** [default: undefined]: A name for this Gunner instance or suite.
31
32#### Example
33
34```JavaScript
35const gunner = new Gunner(name);
36```
37
38[`INDEX`](#index)
39
40### Gunner#test (title, implementation)
41
42Registers a new test. A test can have multiple expect statements by using `expectMany`. The first expect to fail will cause the test to fail.
43
44The `expect` function exported with Gunner is expected to be called and returned, but any assertion module may be used, as long it either throws an error, or you return the rejection. If you use a different assert module such as `chai`, remember to return Promises properly, else you may have false positives as tests will pass, but failures will become `unhandledRejections`.
45
46A [state object (explained below)](#state) is passed into the callback function.
47
48#### Example
49
50```JavaScript
51const { Gunner, expect } = require('@klenty/gunner');
52
53gunner.test('sum should equal 3', () => {
54
55 const sum = 1 + 2;
56 return expect(sum).equal(3);
57
58});
59```
60
61You can also pass a function with parameters to be called with:
62
63```JavaScript
64const { Gunner, expect } = require('@klenty/gunner');
65const sum = (a, b) => a + b;
66
67gunner.test('sum should equal 3', () => expect(sum, [ 1, 2 ]).equal(3));
68```
69
70Expecting multiple results:
71
72```JavaScript
73const { Gunner, expect, expectMany } = require('@klenty/gunner');
74
75gunner.test('multiple expects should be true', () => {
76
77 const a = 1 + 2;
78 const b = 'Hello World';
79
80 return expectMany(
81 expect(a).equal(3),
82 expect(b).equal('Goodbye World'),
83 );
84
85});
86```
87
88Asynchronous tests:
89
90```JavaScript
91gunner.test('asynchronous test', async () => {
92
93 const response = await axios.post(url, request);
94 const userObject = await db.find('userdetails', { username });
95
96 await expect(response.status).equal(200);
97 await expect(userObject).deepEquals(testUser);
98
99})
100```
101
102[`INDEX`](#index)
103
104### Gunner#before (title, implementation)
105
106Registers a new `before` hook. `before` hooks run before the selected test(s). The implementation callback is similar to that of a test. `state` will accumulate over multiple hooks. The third parameter is a label to store to `state`. Multiple hooks with the same label will override, and hooks without labels will not contribute to state.
107
108The first argument can be one of:
109
110- title of a test, which causes the hook to run once before the mentioned test
111
112- `'*'`, which causes the hook to run once before _every_ test
113
114- either of the constants: `Gunner.Start`, and `Gunner.End`.
115
116`gunner.before(Gunner.Start, () => {})` will run once before Gunner starts running any tests. The `Gunner.End` equivalent will run once after running all tests (before ending).
117
118#### Example
119
120```JavaScript
121gunner.before('insert to db should not error', async () => {
122
123 // Clear db before test
124 await db.remove('users', { username: 'mkrhere' });
125
126});
127
128gunner.test('insert to db should not error', async () => {
129
130 const user = await db.insert({
131 username: 'mkrhere',
132 firstname: 'muthu',
133 });
134
135 await expect(user).hasPair('firstname', 'muthu');
136
137});
138```
139
140[`INDEX`](#index)
141
142### Gunner#after (title, implementation)
143
144Registers a new `after` hook. `after` hooks run after the corresponding test(s). The implementation callback is similar to that of a test, with the exception that no expect object will be passed.
145
146The first argument is similar to `Gunner#before`, but does not accept `Gunner.Start` and `Gunner.End` constants, only `'*'` or test description.
147
148#### Example
149
150```JavaScript
151gunner.test('insert to db should not error', expect => {
152
153 const user = await db.insert({
154 username: 'mkrhere',
155 firstname: 'muthu',
156 });
157 return expect(user).hasPair('firstname', 'muthu');
158
159});
160
161gunner.after('insert to db should not error', () => {
162
163 // Clear db after test
164 return db.remove('users', { username: 'mkrhere' });
165
166});
167```
168
169[`INDEX`](#index)
170
171### Gunner#run (options)
172
173Starts running Gunner tests. Takes an options object as optional parameter.
174
175#### Options
176
177- **`log`** [default: true]: Turn logs on or off (returns array of results)
178- **`trace`** [default: false]: Turn stack traces on or off
179
180#### Example
181
182```JavaScript
183const options = { logs: true, trace: true };
184gunner.run(options);
185```
186
187[`INDEX`](#index)
188
189### State
190
191> `[ADVANCED]`
192
193Additionally, hooks contribute to the state object with their return values that will be passed down hierarchically to other hooks, and their matching tests. The state object is passed as the first argument to all tests and hooks. State can only be created by hooks, by passing a label as the third argument.
194
195State has four hierarchies:
196
197- `'@start'` (from the `Gunner.Start` hooks).
198- `'@every'` (from the `'*'` hooks).
199- `'@this'` (from the hook registered to this test).
200- `'@results'` (results from all tests, passed only to the `Gunner.End` hook).
201
202#### Example
203
204```JavaScript
205gunner.before(
206 Gunner.Start,
207 () => DBModule.createDbConnection(),
208 'db'
209);
210
211gunner.before(
212 'test user should exist in db',
213 state => {
214
215 // Receives '@start' and '@every' states if exists
216 const db = state['@start'].db;
217
218 const testUser = await db.insert('users', {
219 username: 'mkrhere',
220 firstname: 'muthu',
221 });
222 return testUser.username;
223
224 },
225 'username'
226);
227
228gunner.test('test user should exist in db', state => {
229
230 // Receives '@start', '@every', and '@this' states
231 // Each state level is an array because multiple hooks may exist per level
232 const db = state['@start'].db;
233 const username = state['@this'].username;
234
235 const user = await db.find('users', { username });
236 return expect(user).hasPair('firstname', 'muthu');
237
238});
239
240gunner.after('test user should exist in db', state => {
241
242 const db = state['@start'][0];
243 const db = state['@this'][0];
244
245 return db.remove('users', { username });
246
247});
248```
249
250[`INDEX`](#index)
251
252[`Back to README`](README.md)
\No newline at end of file