-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (155 loc) · 4.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const requireg = require('requireg');
const { cropLongData } = require('@kronoslive/codeceptjs-utils');
let mochawesome;
let utils;
let SwiftMock;
class Swift extends Helper {
constructor(config) {
super(config);
SwiftMock = requireg('swift-mock');
this._validateConfig(config);
}
_validateConfig(config) {
this.options = {
saveIncomingMessages: true,
delete: true,
};
this.isRunning = false;
this.isFinished = false;
this.receivedMessages = {};
// override defaults with config
Object.assign(this.options, config);
if (!this.options.in || !this.options.in) {
throw new Error(`
Swift requires at input folder and output folder to be set.
Check your codeceptjs config file to ensure this is set properly
{
"helpers": {
"Swift": {
"in": "Input folder",
"out": "Output folder"
}
}
}
`);
}
}
// eslint-disable-next-line consistent-return
static _checkRequirements() {
try {
requireg('swift-mock');
} catch (e) {
return ['swift-mock'];
}
// eslint-disable-next-line consistent-return
}
_beforeSuite(test, mochawesomeHelper) {
mochawesome = mochawesomeHelper || this.helpers.Mochawesome;
utils = this.helpers.Utils;
}
_runSwift() {
if (this.isRunning) {
return true;
}
if (this.isFinished) {
return true;
}
this.swift = new SwiftMock(this.options);
this.isRunning = true;
return this.swift.run();
}
async sendSwiftMessage(data, filenamePrefix) {
await this._runSwift();
return this.swift.send(data, filenamePrefix);
}
async subscribeToSwiftMessage(predicate, callback, options = { logging: true }) {
await this._runSwift();
const loggingFn = (msg) => {
if (options.logging) {
mochawesome.addMochawesomeContext({
title: 'Received Swift message',
value: {
body: msg,
},
});
}
return predicate(msg);
};
return this.swift.on(loggingFn, callback);
}
async expectSwiftMessageUntil(predicate, timeout) {
await this._runSwift();
const seen = {};
let predicateErr;
return utils.waitUntil(() => Promise.resolve((this.swift.getMessages() || [])
.find((msg, i) => {
try {
if (!seen[i]) {
seen[i] = true;
return predicate(msg);
}
return false;
} catch (err) {
predicateErr = err;
return true;
}
})), timeout, 'timeout', 100)
.then(() => {
if (predicateErr) {
throw new Error(`predicate return err (${predicateErr.code}), but it should return boolean value`);
}
mochawesome.addMochawesomeContext({
title: 'Wait swift message with predicate',
value: predicate.toString(),
});
mochawesome.addMochawesomeContext({
title: 'Latest swift message',
value: cropLongData((this.swift.getMessages())[(this.swift.getMessages()).length - 1]),
});
}).catch((err) => {
mochawesome.addMochawesomeContext({
title: 'Wait swift message with predicate',
value: predicate.toString(),
});
mochawesome.addMochawesomeContext({
title: 'Latest swift message',
value: cropLongData((this.swift.getMessages())[(this.swift.getMessages()).length - 1]),
});
if (err.message === 'timeout') {
throw new Error(`swift timeout while expecting message with predicate${predicate.toString()}`);
} else throw err;
});
}
async dontExpectSwiftMessageUntil(predicate, timeout) {
await this._runSwift();
return new Promise((resolve, reject) => {
setTimeout(() => {
const found = (this.swift.getMessages() || []).find(predicate);
if (found !== undefined) {
reject(new Error(`Found some not expected: ${found}`));
} else {
resolve();
}
}, timeout);
});
}
async _finishTest() {
if (!this.isRunning) {
return true;
}
this.isFinished = true;
this.isRunning = false;
return this.swift.watcher.close();
}
_failed() {
}
async _after() {
if (!this.isRunning) {
return true;
}
await this.swift.cleanListeners();
await this.swift.cleanMessages();
return true;
}
}
module.exports = Swift;