UNPKG

2.93 kBJavaScriptView Raw
1/* eslint-env node */
2'use strict';
3
4const path = require('path');
5const resolvePackagePath = require('resolve-package-path');
6const stripIndent = require('common-tags').stripIndent;
7
8const validatePeerDependencies = require('validate-peer-dependencies');
9
10module.exports = {
11 name: 'ember-qunit',
12
13 init() {
14 this._super.init && this._super.init.apply(this, arguments);
15
16 this.setTestGenerator();
17 },
18
19 included() {
20 this._super.included.apply(this, arguments);
21
22 validatePeerDependencies(__dirname, {
23 resolvePackagePathFrom: this.parent.root,
24 });
25
26 // TODO: figure out how to make this not needed, AFAICT ember-auto-import
27 // does not provide any ability to import styles
28 this.import('vendor/qunit/qunit.css', { type: 'test' });
29
30 let addonOptions = this.targetOptions();
31 let explicitlyDisabledStyles = addonOptions.disableContainerStyles === true;
32 if (!explicitlyDisabledStyles) {
33 this.import('vendor/ember-qunit/test-container-styles.css', {
34 type: 'test',
35 });
36 }
37 },
38
39 targetOptions() {
40 if (!this._targetOptions) {
41 // 1. check this.parent.options['ember-qunit']
42 let targetOptions =
43 this.parent.options && this.parent.options['ember-qunit'];
44
45 // 2. check this.app.options['ember-qunit']
46 targetOptions =
47 targetOptions ||
48 (this.app && this.app.options && this.app.options['ember-qunit']);
49 this._targetOptions = targetOptions || {};
50 }
51
52 return this._targetOptions;
53 },
54
55 treeForVendor: function (tree) {
56 const MergeTrees = require('broccoli-merge-trees');
57 const Funnel = require('broccoli-funnel');
58
59 let qunitPackagePath = resolvePackagePath('qunit', this.parent.root);
60 let qunitPath = path.join(path.dirname(qunitPackagePath), 'qunit');
61
62 let qunitTree = new Funnel(this.treeGenerator(qunitPath), {
63 destDir: 'qunit',
64 annotation: 'ember-qunit#treeForVendor',
65 });
66
67 return new MergeTrees([qunitTree, tree]);
68 },
69
70 treeForAddonTestSupport(tree) {
71 // intentionally not calling _super here
72 // so that can have our `import`'s be
73 // import { ... } from 'ember-qunit';
74
75 const Funnel = require('broccoli-funnel');
76
77 let scopedInputTree = new Funnel(tree, { destDir: 'ember-qunit' });
78
79 return this.preprocessJs(scopedInputTree, '/', this.name, {
80 annotation: `ember-qunit - treeForAddonTestSupport`,
81 registry: this.registry,
82 treeType: 'addon-test-support',
83 });
84 },
85
86 setTestGenerator: function () {
87 this.project.generateTestFile = function (moduleName, tests) {
88 let output = `QUnit.module('${moduleName}');\n`;
89
90 tests.forEach(function (test) {
91 output += stripIndent`
92 QUnit.test('${test.name}', function(assert) {
93 assert.expect(1);
94 assert.ok(${test.passed}, '${test.errorMessage}');
95 });
96 `;
97 });
98
99 return output;
100 };
101 },
102};