forked from emberjs/ember-mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (127 loc) · 4.75 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
/* eslint-env node */
'use strict';
const path = require('path');
const stripIndent = require('common-tags').stripIndent;
module.exports = {
name: 'ember-mocha',
init() {
this._super.init && this._super.init.apply(this, arguments);
this.overrideTestCommandFilter();
this.setTestGenerator();
},
postBuild() {
this.checkPackages();
},
checkPackages() {
var packages = Object.keys(this.project.addonPackages);
if (packages.indexOf('ember-cli-qunit') !== -1) {
console.warn('\nIt looks like you are using "ember-cli-qunit" which can cause issues with "ember-cli-mocha", please remove this package.\n');
process.exit(1);
}
if (packages.indexOf('ember-cli-htmlbars-inline-precompile') === -1) {
console.warn('\nIt looks like you\'re not on ember-cli 1.13, which includes ember-cli-htmlbars-inline-precompile by default. Please run: ember install ember-cli-htmlbars-inline-precompile.\n');
process.exit(1);
}
},
included() {
this._super.included.apply(this, arguments);
this.import('vendor/mocha/mocha.js', { type: 'test' });
this.import('vendor/mocha/mocha.css', { type: 'test' });
this.import('vendor/ember-mocha/mocha-configuration.js', { type: 'test' });
this.import('vendor/ember-mocha/ember-mocha-adapter.js', { type: 'test' });
this.import('vendor/ember-mocha/test-loader.js', { type: 'test' });
let addonOptions = this.targetOptions();
let explicitlyDisabledStyles = addonOptions.disableContainerStyles === true;
if (!explicitlyDisabledStyles) {
this.import('vendor/ember-mocha/test-container-styles.css', { type: 'test' });
}
},
targetOptions() {
if (!this._targetOptions) {
// 1. check this.parent.options['ember-mocha']
let targetOptions = this.parent.options && this.parent.options['ember-mocha'];
// 2. check this.app.options['ember-mocha']
targetOptions = targetOptions || (this.app && this.app.options && this.app.options['ember-mocha']);
// 3. check this.parent.options['ember-cli-mocha']
targetOptions = targetOptions || (this.parent.options && this.parent.options['ember-cli-mocha']);
// 4. check this.app.options['ember-cli-mocha']
targetOptions = targetOptions || (this.app && this.app.options && this.app.options['ember-cli-mocha']);
this._targetOptions = targetOptions || {};
}
return this._targetOptions;
},
contentFor(type) {
// Skip if insertContentForTestBody === false.
if (type === 'test-body' && !(this.targetOptions().insertContentForTestBody === false)) {
return stripIndent`
<div id="mocha"></div>
<div id="mocha-fixture"></div>
<div id="ember-testing-container">
<div id="ember-testing"></div>
</div>
`;
}
},
treeForVendor(tree) {
const MergeTrees = require('broccoli-merge-trees');
const Funnel = require('broccoli-funnel');
let mochaPath = path.dirname(require.resolve('mocha'));
let mochaTree = new Funnel(this.treeGenerator(mochaPath), {
destDir: 'mocha',
annotation: 'ember-mocha#treeForVendor',
});
return new MergeTrees([mochaTree, tree]);
},
treeForAddonTestSupport(tree) {
// intentionally not calling _super here
// so that can have our `import`'s be
// import { ... } from 'ember-mocha';
return this.preprocessJs(tree, '/', this.name, {
registry: this.registry,
});
},
overrideTestCommandFilter() {
let TestCommand = this.project.require('ember-cli/lib/commands/test');
TestCommand.prototype.buildTestPageQueryString = function(options) {
let params = [];
if (options.filter) {
params.push(`grep=${options.filter}`);
if (options.invert) {
params.push('invert=1');
}
}
if (options.query) {
params.push(options.query);
}
return params.join('&');
};
TestCommand.prototype.availableOptions.push({
name: 'invert',
type: Boolean,
default: false,
description: 'Invert the filter specified by the --filter argument',
aliases: ['i']
});
},
setTestGenerator() {
this.project.generateTestFile = function(moduleName, tests) {
var output = `describe('${moduleName}', function() {\n`;
tests.forEach(function(test) {
output += ` it('${test.name}', function() {\n`;
if (test.passed) {
output +=
" // precompiled test passed\n";
} else {
output +=
" // precompiled test failed\n" +
` var error = new chai.AssertionError('${test.errorMessage}');\n` +
" error.stack = undefined;\n" +
" throw error;\n";
}
output += " });\n";
});
output += "});\n";
return output;
};
},
};