forked from yads/ember-mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (122 loc) · 4 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
/* eslint-disable no-console, n/no-process-exit */
'use strict';
const path = require('path');
const resolvePackagePath = require('resolve-package-path');
module.exports = {
name: require('./package').name,
options: {
autoImport: {
exclude: ['mocha'],
},
},
init() {
this._super.init && this._super.init.apply(this, arguments);
this.overrideTestCommandFilter();
this.setTestGenerator();
},
postBuild() {
this.checkPackages();
},
checkPackages() {
const packages = Object.keys(this.project.addonPackages);
if (packages.indexOf('ember-qunit') !== -1) {
console.warn(
'\nIt looks like you are using "ember-qunit" which can cause issues with "@eflexsystems/ember-mocha", please remove this package.\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' });
let addonOptions = this.targetOptions();
let explicitlyDisabledStyles = addonOptions.disableContainerStyles === true;
if (!explicitlyDisabledStyles) {
this.import('vendor/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']);
this._targetOptions = targetOptions || {};
}
return this._targetOptions;
},
treeForVendor(tree) {
const MergeTrees = require('broccoli-merge-trees');
const Funnel = require('broccoli-funnel');
let mochaPackagePath = resolvePackagePath('mocha', this.parent.root);
let mochaPath = path.dirname(mochaPackagePath);
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';
const Funnel = require('broccoli-funnel');
let scopedInputTree = new Funnel(tree, {
destDir: '@eflexsystems/ember-mocha',
});
return this.preprocessJs(scopedInputTree, '/', this.name, {
annotation: `ember-mocha - treeForAddonTestSupport`,
registry: this.registry,
treeType: 'addon-test-support',
});
},
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) {
let 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' +
` const error = new chai.AssertionError('${test.errorMessage}');\n` +
' error.stack = undefined;\n' +
' throw error;\n';
}
output += ' });\n';
});
output += '});\n';
return output;
};
},
};