-
Notifications
You must be signed in to change notification settings - Fork 2
/
suite.js
182 lines (153 loc) · 4.46 KB
/
suite.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
import { remote } from 'webdriverio';
import Mugshot from 'mugshot';
import WebdriverIOAdapter from 'mugshot-webdriverio';
import path from 'path';
import fs from 'fs';
import { expect } from 'chai';
import {
runnerAfter,
runnerBefore,
runnerBeforeEach,
runnerIt,
runnerDescribe
} from '../mocha-runner.js';
const { BROWSER = 'chrome', SELENIUM_HOST = 'selenium' } = process.env;
let suiteNesting = 0;
// These will hold root suite level instances. Since most, if not all test
// runners run tests inside of a suite sequentially and since we only set
// up the browser once per root test suite, these should be "thread safe".
let rootSuiteBrowser, rootSuiteMugshot;
/**
* Run your gui tests in a fresh Selenium session.
*
* Nested calls will preserve the root session.
*
* Tests and hooks will receive the browser instance.
*
* @param {String} name
* @param {() => void} definition
*/
export function describe(name, definition) {
suiteNesting++;
runnerDescribe(name, function() {
// We only want to set up hooks once - for the root suite.
suiteNesting === 1 && setupHooks();
definition();
});
suiteNesting--;
}
/**
* @param {(Webdriver) => Promise|void} definition
*/
export function beforeEach(definition) {
runnerBeforeEach(function() {
return definition(rootSuiteBrowser);
});
}
/**
* Run a test with optional coverage report.
*
* @param {String} name
* @param {(Webdriver) => Promise|void} definition
*/
export function it(name, definition) {
runnerIt(name, testName => {
const promise = Promise.resolve(definition(rootSuiteBrowser));
if (process.env.NODE_ENV !== 'coverage') {
return promise;
}
return promise.then(() => collectCoverage(testName));
});
}
/**
* Perform a visual test alongside a normal test.
*
* The visual test will not be performed if the test in `definition` fails.
*
* @param {String} name The name of the test. The screenshot will be taken under
* the full test name (including any parent suite's name(s)).
* @param {(Webdriver) => Promise|void} definition
* @param {CSSSelector} selector
*/
export function vit(name, definition, selector = '.todoapp') {
runnerIt(name, testName => {
let promise = Promise.resolve(definition(rootSuiteBrowser));
// Don't want to make debugging tests more noisy than it needs to be.
if (!process.env.DEBUG) {
promise = promise.then(() => checkForVisualChanges(testName, selector));
}
if (process.env.NODE_ENV === 'coverage') {
promise = promise.then(() => collectCoverage(testName));
}
return promise;
});
}
export { expect };
/**
* @param {String} name
* @param {CSSSelector} selector
*
* @returns {Promise<undefined>}
*/
function checkForVisualChanges(name, selector = 'body > *') {
return new Promise((resolve, reject) => {
rootSuiteMugshot.test({ name: getSafeFilename(name), selector }, (err, result) => {
if (err) {
return reject(err);
}
expect(result.isEqual, 'Visual changes detected. Check screenshots').to.be.true;
return resolve();
});
});
}
/**
* @param {string} testName
*
* @returns {Promise<undefined>}
*/
function collectCoverage(testName) {
const safeTestName = getSafeFilename(testName);
return rootSuiteBrowser.execute(function getCoverage() {
return JSON.stringify(window.__coverage__);
}).then(({ value: coverage }) => {
fs.writeFileSync(
path.join(__dirname, 'results', 'coverage', `${BROWSER}_${safeTestName}.json`),
coverage
);
});
}
/**
* Turn the given file name into something that's safe to save on the FS.
*
* @param {String} fileName
*
* @returns {String}
*/
function getSafeFilename(fileName) {
return fileName
.replace(/\//g, '_')
.replace(/ /g, '_')
.toLowerCase();
}
function setupHooks() {
runnerBefore(function connectToSelenium() {
const options = {
host: SELENIUM_HOST,
desiredCapabilities: { browserName: BROWSER }
};
rootSuiteBrowser = remote(options).init();
const adapter = new WebdriverIOAdapter(rootSuiteBrowser);
rootSuiteMugshot = new Mugshot(adapter, {
rootDirectory: path.join(__dirname, 'screenshots', BROWSER),
acceptFirstBaseline: false
});
return rootSuiteBrowser;
});
runnerAfter(function endSession() {
return rootSuiteBrowser.end();
});
runnerBeforeEach(function waitForRender() {
return rootSuiteBrowser.url('http://app:3000/')
.then(() => rootSuiteBrowser.waitForVisible('.todoapp', 5 * 1000));
});
}