-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
62 lines (51 loc) · 1.97 KB
/
server.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
import {Meteor} from 'meteor/meteor';
import {onTest, onInterrupt} from 'meteor/universe:test-hooks';
import {mocha, before, setMochaOptions} from './lib/mocha';
import {createBrowser, closeAllBrowsers} from './lib/puppeteer';
// Allow easier context checking inside tests suites
Meteor.isE2E = true;
// In case of Meteor server restart, close all browser windows
onInterrupt(closeAllBrowsers);
// Re-export test-hooks api so this package could be used as a test driver
export {start} from 'meteor/universe:test-hooks';
export {onTest, onInterrupt};
// Export mocha functions (describe, it etc.) and other public APIs
export * from './lib/mocha';
export {createBrowser} from './lib/puppeteer';
// Export extra helpers
export * from './helpers';
// Prepare bindings for default Puppeteer instances created on setup
export let browser, page;
/**
* Setup test environment
* @param {object} [mocha] - custom options for mocha
* @param {object} [browser] - custom options for createBrowser and puppeteer
* @param {boolean} [browser.createDefaultBrowser=true] - when true will automatically create new browser
* @void
*/
export async function setup({
mocha: mochaOptions,
browser: {
createDefaultBrowser = true,
...browserOptions
} = {},
} = {}) {
// Support for custom mocha configuration
if (mochaOptions) {
setMochaOptions(mocha, mochaOptions);
}
// Create default browser instance before the tests
if (createDefaultBrowser) {
before(async () => ({browser, page} = await createBrowser(browserOptions)));
}
onTest(done => {
// Run the tests using Mocha
// All tests should be registered in app by now
mocha.run(async errorCount => {
// Tests are over, close browsers
await closeAllBrowsers();
// End the onTest hook, with an error if some tests failed
errorCount > 0 ? done(new Error(`Mocha reported ${errorCount} errors`)) : done();
});
});
}