UNPKG

9.02 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4
5var _getIterator2 = require("babel-runtime/core-js/get-iterator");
6
7var _getIterator3 = _interopRequireDefault(_getIterator2);
8
9var _values = require("babel-runtime/core-js/object/values");
10
11var _values2 = _interopRequireDefault(_values);
12
13var _keys = require("babel-runtime/core-js/object/keys");
14
15var _keys2 = _interopRequireDefault(_keys);
16
17var _extends2 = require("babel-runtime/helpers/extends");
18
19var _extends3 = _interopRequireDefault(_extends2);
20
21var _stringify = require("babel-runtime/core-js/json/stringify");
22
23var _stringify2 = _interopRequireDefault(_stringify);
24
25exports.default = function (fixturesLoc, name) {
26 var suiteOpts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
27 var taskOpts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
28 var dynamicOpts = arguments[4];
29
30 var suites = (0, _babelHelperFixtures2.default)(fixturesLoc);
31
32 var _loop = function _loop() {
33 if (_isArray) {
34 if (_i >= _iterator.length) return "break";
35 _ref = _iterator[_i++];
36 } else {
37 _i = _iterator.next();
38 if (_i.done) return "break";
39 _ref = _i.value;
40 }
41
42 var testSuite = _ref;
43
44 if ((0, _includes2.default)(suiteOpts.ignoreSuites, testSuite.title)) return "continue";
45
46 describe(name + "/" + testSuite.title, function () {
47 var _loop3 = function _loop3() {
48 if (_isArray2) {
49 if (_i2 >= _iterator2.length) return "break";
50 _ref2 = _iterator2[_i2++];
51 } else {
52 _i2 = _iterator2.next();
53 if (_i2.done) return "break";
54 _ref2 = _i2.value;
55 }
56
57 var task = _ref2;
58
59 if ((0, _includes2.default)(suiteOpts.ignoreTasks, task.title) || (0, _includes2.default)(suiteOpts.ignoreTasks, testSuite.title + "/" + task.title)) return "continue";
60
61 it(task.title, !task.disabled && function () {
62 function runTask() {
63 run(task);
64 }
65
66 (0, _defaults2.default)(task.options, {
67 filenameRelative: task.expect.filename,
68 sourceFileName: task.actual.filename,
69 sourceMapTarget: task.expect.filename,
70 suppressDeprecationMessages: true,
71 babelrc: false,
72 sourceMap: !!(task.sourceMappings || task.sourceMap),
73 inputSourceMap: task.inputSourceMap || undefined
74 });
75
76 (0, _extend2.default)(task.options, taskOpts);
77
78 if (dynamicOpts) dynamicOpts(task.options, task);
79
80 var throwMsg = task.options.throws;
81 if (throwMsg) {
82 delete task.options.throws;
83
84 _assert2.default.throws(runTask, function (err) {
85 return throwMsg === true || err.message.indexOf(throwMsg) >= 0;
86 });
87 } else {
88 if (task.exec.code) {
89 var result = run(task);
90 if (result && typeof result.then === "function") {
91 return result;
92 }
93 } else {
94 runTask();
95 }
96 }
97 });
98 };
99
100 _loop4: for (var _iterator2 = testSuite.tests, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
101 var _ref2;
102
103 var _ret2 = _loop3();
104
105 switch (_ret2) {
106 case "break":
107 break _loop4;
108
109 case "continue":
110 continue;}
111 }
112 });
113 };
114
115 _loop2: for (var _iterator = suites, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
116 var _ref;
117
118 var _ret = _loop();
119
120 switch (_ret) {
121 case "break":
122 break _loop2;
123
124 case "continue":
125 continue;}
126 }
127};
128
129var _babelCore = require("babel-core");
130
131var babel = _interopRequireWildcard(_babelCore);
132
133var _babelHelperFixtures = require("babel-helper-fixtures");
134
135var _babelHelperFixtures2 = _interopRequireDefault(_babelHelperFixtures);
136
137var _sourceMap = require("source-map");
138
139var _sourceMap2 = _interopRequireDefault(_sourceMap);
140
141var _babelCodeFrame = require("babel-code-frame");
142
143var _babelCodeFrame2 = _interopRequireDefault(_babelCodeFrame);
144
145var _defaults = require("lodash/defaults");
146
147var _defaults2 = _interopRequireDefault(_defaults);
148
149var _includes = require("lodash/includes");
150
151var _includes2 = _interopRequireDefault(_includes);
152
153var _helpers = require("./helpers");
154
155var helpers = _interopRequireWildcard(_helpers);
156
157var _extend = require("lodash/extend");
158
159var _extend2 = _interopRequireDefault(_extend);
160
161var _merge = require("lodash/merge");
162
163var _merge2 = _interopRequireDefault(_merge);
164
165var _assert = require("assert");
166
167var _assert2 = _interopRequireDefault(_assert);
168
169var _chai = require("chai");
170
171var _chai2 = _interopRequireDefault(_chai);
172
173require("babel-polyfill");
174
175var _fs = require("fs");
176
177var _fs2 = _interopRequireDefault(_fs);
178
179var _path = require("path");
180
181var _path2 = _interopRequireDefault(_path);
182
183function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
184
185function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
186
187var babelHelpers = eval((0, _babelCore.buildExternalHelpers)(null, "var"));
188
189function wrapPackagesArray(type, names, optionsDir) {
190 return (names || []).map(function (val) {
191 if (typeof val === "string") val = [val];
192
193 if (val[0][0] === ".") {
194
195 if (!optionsDir) {
196 throw new Error("Please provide an options.json in test dir when using a " + "relative plugin path.");
197 }
198
199 val[0] = _path2.default.resolve(optionsDir, val[0]);
200 } else {
201 val[0] = __dirname + "/../../babel-" + type + "-" + val[0];
202 }
203
204 return val;
205 });
206}
207
208function run(task) {
209 var actual = task.actual;
210 var expect = task.expect;
211 var exec = task.exec;
212 var opts = task.options;
213 var optionsDir = task.optionsDir;
214
215 function getOpts(self) {
216 var newOpts = (0, _merge2.default)({
217 filename: self.loc
218 }, opts);
219
220 newOpts.plugins = wrapPackagesArray("plugin", newOpts.plugins, optionsDir);
221 newOpts.presets = wrapPackagesArray("preset", newOpts.presets, optionsDir).map(function (val) {
222 if (val.length > 2) {
223 throw new Error("Unexpected extra options " + (0, _stringify2.default)(val.slice(2)) + " passed to preset.");
224 }
225
226 return val;
227 });
228
229 return newOpts;
230 }
231
232 var execCode = exec.code;
233 var result = void 0;
234 var resultExec = void 0;
235
236 if (execCode) {
237 var execOpts = getOpts(exec);
238 var execDirName = _path2.default.dirname(exec.loc);
239 result = babel.transform(execCode, execOpts);
240 execCode = result.code;
241
242 try {
243 resultExec = runExec(execOpts, execCode, execDirName);
244 } catch (err) {
245 err.message = exec.loc + ": " + err.message;
246 err.message += (0, _babelCodeFrame2.default)(execCode);
247 throw err;
248 }
249 }
250
251 var actualCode = actual.code;
252 var expectCode = expect.code;
253 if (!execCode || actualCode) {
254 result = babel.transform(actualCode, getOpts(actual));
255 if (!expect.code && result.code && !opts.throws && _fs2.default.statSync(_path2.default.dirname(expect.loc)).isDirectory() && !process.env.CI) {
256 console.log("New test file created: " + expect.loc);
257 _fs2.default.writeFileSync(expect.loc, result.code);
258 } else {
259 actualCode = result.code.trim();
260 _chai2.default.expect(actualCode).to.be.equal(expectCode, actual.loc + " !== " + expect.loc);
261 }
262 }
263
264 if (task.sourceMap) {
265 _chai2.default.expect(result.map).to.deep.equal(task.sourceMap);
266 }
267
268 if (task.sourceMappings) {
269 var consumer = new _sourceMap2.default.SourceMapConsumer(result.map);
270
271 task.sourceMappings.forEach(function (mapping) {
272 var actual = mapping.original;
273
274 var expect = consumer.originalPositionFor(mapping.generated);
275 _chai2.default.expect({ line: expect.line, column: expect.column }).to.deep.equal(actual);
276 });
277 }
278
279 if (execCode && resultExec) {
280 return resultExec;
281 }
282}
283
284function runExec(opts, execCode, execDirname) {
285 var sandbox = (0, _extends3.default)({}, helpers, {
286 babelHelpers: babelHelpers,
287 assert: _chai2.default.assert,
288 transform: babel.transform,
289 opts: opts,
290 exports: {},
291 require: function (_require) {
292 function require(_x) {
293 return _require.apply(this, arguments);
294 }
295
296 require.toString = function () {
297 return _require.toString();
298 };
299
300 return require;
301 }(function (id) {
302 return require(id[0] === "." ? _path2.default.resolve(execDirname, id) : id);
303 })
304 });
305
306 var fn = new (Function.prototype.bind.apply(Function, [null].concat((0, _keys2.default)(sandbox), [execCode])))();
307 return fn.apply(null, (0, _values2.default)(sandbox));
308}
309
310module.exports = exports["default"];
\No newline at end of file