-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (101 loc) · 3.72 KB
/
index.js
File metadata and controls
122 lines (101 loc) · 3.72 KB
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
/* global Testem */
import require from 'require';
import { assert } from '@ember/debug';
import { getOwner } from '@ember/application';
import { getContext } from '@ember/test-helpers';
import { uploadInteraction, finalize } from './upload';
export function setupPact(hooks = {}, options = {}) {
// QUnit passes hooks explicitly; with Mocha they're implicit
if (typeof hooks.beforeEach !== 'function') {
options = hooks;
hooks = makeMochaHooks();
}
// Mocha will have unset the context by the time our `afterEach` runs,
// so we stash it in `beforeEach` instead.
let context;
hooks.beforeEach(function(assert) {
context = getContext();
setupServices(context, this, options);
setupProvider(context, options, assert.test.testName);
});
hooks.afterEach(function() {
return teardownProvider(context, options);
});
}
function makeMochaHooks() {
let mocha = require('mocha');
return {
afterEach: mocha.afterEach,
beforeEach(callback) {
mocha.beforeEach(function() {
// Quick'n'dirty way to pass the test name from Mocha the same as from QUnit
let assert = { test: { testName: this.currentTest.title } };
return callback.call(this, assert);
});
}
};
}
function setupServices(context, target, options) {
for (let service of getConfigValue(context, options, 'serviceInjections')) {
target[service] = () => findOwner(context).lookup(`service:${service}`);
}
}
function getConfigValue(context, options, key) {
if (key in options) {
return options[key];
} else {
return getConfig(context)['ember-cli-pact'][key];
}
}
function getConfig(context) {
return findOwner(context).resolveRegistration('config:environment');
}
function findOwner(context) {
return context.owner || getOwner(context);
}
function setupProvider(context, options, testName) {
let MockProvider = loadMockProvider(context, options);
let provider = context._pactProvider = new MockProvider(getConfig(context));
assertSingleConsumerName(context, options);
provider.startInteraction(testName);
}
function loadMockProvider(context, options) {
let name = getConfigValue(context, options, 'mockProvider');
try {
// Start by looking for a custom provider in the helpers directory
let { modulePrefix } = getConfig(context);
return require(`${modulePrefix}/tests/helpers/pact-providers/${name}`).default;
} catch (error) {
// Fall back to the default provider location from addon-test-support
return require(`ember-cli-pact/pact-providers/${name}`).default;
}
}
function assertSingleConsumerName(context, options) {
let localConsumerName = options.consumerName;
let globalConsumerName = getConfig(context)['ember-cli-pact'].consumerName;
let hasOverriddenConsumer = localConsumerName && localConsumerName !== globalConsumerName;
assert(`ember-cli-pact doesn't currently support testing multiple consumers`, !hasOverriddenConsumer);
}
function teardownProvider(context, options) {
let provider = context._pactProvider;
let interaction = provider.serializeInteraction(getConfigValue(context, options, 'pactVersion'));
let upload = uploadInteraction(interaction, {
version: getConfigValue(context, options, 'pactVersion'),
provider: getConfigValue(context, options, 'providerName'),
consumer: getConfigValue(context, options, 'consumerName')
});
registerFinalizeCallback();
provider.endInteraction();
return upload;
}
let addedFinalizeCallback = false;
function registerFinalizeCallback() {
if (!addedFinalizeCallback) {
addedFinalizeCallback = true;
Testem.afterTests((config, data, callback) => {
finalize()
.catch((error) => setTimeout(() => { throw error; }))
.then(() => setTimeout(callback));
});
}
}