MochaWebpack.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = undefined;
  6. var _extends2 = require('babel-runtime/helpers/extends');
  7. var _extends3 = _interopRequireDefault(_extends2);
  8. var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
  9. var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
  10. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  11. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  12. var _createClass2 = require('babel-runtime/helpers/createClass');
  13. var _createClass3 = _interopRequireDefault(_createClass2);
  14. require('nodent-runtime');
  15. var _TestRunner = require('./runner/TestRunner');
  16. var _TestRunner2 = _interopRequireDefault(_TestRunner);
  17. var _testRunnerReporter = require('./runner/testRunnerReporter');
  18. var _testRunnerReporter2 = _interopRequireDefault(_testRunnerReporter);
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20. var MochaWebpack = function () {
  21. function MochaWebpack() {
  22. (0, _classCallCheck3.default)(this, MochaWebpack);
  23. this.entries = [];
  24. this.includes = [];
  25. this.options = {
  26. cwd: process.cwd(),
  27. webpackConfig: {},
  28. bail: false,
  29. reporter: 'spec',
  30. reporterOptions: {},
  31. ui: 'bdd',
  32. invert: false,
  33. ignoreLeaks: true,
  34. fullStackTrace: false,
  35. useInlineDiffs: false,
  36. timeout: 2000,
  37. slow: 75,
  38. asyncOnly: false,
  39. delay: false,
  40. interactive: !!process.stdout.isTTY,
  41. quiet: false
  42. };
  43. }
  44. /**
  45. * Files to run test against
  46. *
  47. * @private
  48. */
  49. /**
  50. * Files to include into the bundle
  51. *
  52. * @private
  53. */
  54. /**
  55. * Options
  56. *
  57. * @private
  58. */
  59. (0, _createClass3.default)(MochaWebpack, [{
  60. key: 'addEntry',
  61. /**
  62. * Add file run test against
  63. *
  64. * @public
  65. * @param {string} file file or glob
  66. * @return {MochaWebpack}
  67. */
  68. value: function addEntry(file) {
  69. this.entries = [].concat((0, _toConsumableArray3.default)(this.entries), [file]);
  70. return this;
  71. }
  72. /**
  73. * Add file to include into the test bundle
  74. *
  75. * @public
  76. * @param {string} file absolute path to module
  77. * @return {MochaWebpack}
  78. */
  79. }, {
  80. key: 'addInclude',
  81. value: function addInclude(file) {
  82. this.includes = [].concat((0, _toConsumableArray3.default)(this.includes), [file]);
  83. return this;
  84. }
  85. /**
  86. * Sets the current working directory
  87. *
  88. * @public
  89. * @param {string} cwd absolute working directory path
  90. * @return {MochaWebpack}
  91. */
  92. }, {
  93. key: 'cwd',
  94. value: function (_cwd) {
  95. function cwd(_x) {
  96. return _cwd.apply(this, arguments);
  97. }
  98. cwd.toString = function () {
  99. return _cwd.toString();
  100. };
  101. return cwd;
  102. }(function (cwd) {
  103. this.options = (0, _extends3.default)({}, this.options, {
  104. cwd: cwd
  105. });
  106. return this;
  107. })
  108. /**
  109. * Sets the webpack config
  110. *
  111. * @public
  112. * @param {Object} config webpack config
  113. * @return {MochaWebpack}
  114. */
  115. }, {
  116. key: 'webpackConfig',
  117. value: function webpackConfig() {
  118. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  119. this.options = (0, _extends3.default)({}, this.options, {
  120. webpackConfig: config
  121. });
  122. return this;
  123. }
  124. /**
  125. * Enable or disable bailing on the first failure.
  126. *
  127. * @public
  128. * @param {boolean} [bail]
  129. * @return {MochaWebpack}
  130. */
  131. }, {
  132. key: 'bail',
  133. value: function (_bail) {
  134. function bail() {
  135. return _bail.apply(this, arguments);
  136. }
  137. bail.toString = function () {
  138. return _bail.toString();
  139. };
  140. return bail;
  141. }(function () {
  142. var bail = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  143. this.options = (0, _extends3.default)({}, this.options, {
  144. bail: bail
  145. });
  146. return this;
  147. })
  148. /**
  149. * Set reporter to `reporter`, defaults to "spec".
  150. *
  151. * @param {string|Function} reporter name or constructor
  152. * @param {Object} reporterOptions optional options
  153. * @return {MochaWebpack}
  154. */
  155. }, {
  156. key: 'reporter',
  157. value: function (_reporter) {
  158. function reporter(_x2, _x3) {
  159. return _reporter.apply(this, arguments);
  160. }
  161. reporter.toString = function () {
  162. return _reporter.toString();
  163. };
  164. return reporter;
  165. }(function (reporter, reporterOptions) {
  166. this.options = (0, _extends3.default)({}, this.options, {
  167. reporter: reporter,
  168. reporterOptions: reporterOptions
  169. });
  170. return this;
  171. })
  172. /**
  173. * Set test UI, defaults to "bdd".
  174. *
  175. * @public
  176. * @param {string} ui bdd/tdd
  177. * @return {MochaWebpack}
  178. */
  179. }, {
  180. key: 'ui',
  181. value: function (_ui) {
  182. function ui(_x4) {
  183. return _ui.apply(this, arguments);
  184. }
  185. ui.toString = function () {
  186. return _ui.toString();
  187. };
  188. return ui;
  189. }(function (ui) {
  190. this.options = (0, _extends3.default)({}, this.options, {
  191. ui: ui
  192. });
  193. return this;
  194. })
  195. /**
  196. * Only run tests containing <string>
  197. *
  198. * @public
  199. * @param {string} str
  200. * @return {MochaWebpack}
  201. */
  202. }, {
  203. key: 'fgrep',
  204. value: function fgrep(str) {
  205. this.options = (0, _extends3.default)({}, this.options, {
  206. fgrep: str
  207. });
  208. return this;
  209. }
  210. /**
  211. * Only run tests matching <pattern>
  212. *
  213. * @public
  214. * @param {string|RegExp} pattern
  215. * @return {MochaWebpack}
  216. */
  217. }, {
  218. key: 'grep',
  219. value: function grep(pattern) {
  220. this.options = (0, _extends3.default)({}, this.options, {
  221. grep: pattern
  222. });
  223. return this;
  224. }
  225. /**
  226. * Invert `.grep()` matches.
  227. *
  228. * @public
  229. * @return {MochaWebpack}
  230. */
  231. }, {
  232. key: 'invert',
  233. value: function invert() {
  234. this.options = (0, _extends3.default)({}, this.options, {
  235. invert: true
  236. });
  237. return this;
  238. }
  239. /**
  240. * Ignore global leaks.
  241. *
  242. * @public
  243. * @param {boolean} ignore
  244. * @return {MochaWebpack}
  245. */
  246. }, {
  247. key: 'ignoreLeaks',
  248. value: function ignoreLeaks(ignore) {
  249. this.options = (0, _extends3.default)({}, this.options, {
  250. ignoreLeaks: ignore
  251. });
  252. return this;
  253. }
  254. /**
  255. * Display long stack-trace on failing
  256. *
  257. * @public
  258. * @return {MochaWebpack}
  259. */
  260. }, {
  261. key: 'fullStackTrace',
  262. value: function fullStackTrace() {
  263. this.options = (0, _extends3.default)({}, this.options, {
  264. fullStackTrace: true
  265. });
  266. return this;
  267. }
  268. /**
  269. * Emit color output.
  270. *
  271. * @public
  272. * @param {boolean} colors
  273. * @return {MochaWebpack}
  274. */
  275. }, {
  276. key: 'useColors',
  277. value: function useColors(colors) {
  278. this.options = (0, _extends3.default)({}, this.options, {
  279. colors: colors
  280. });
  281. return this;
  282. }
  283. /**
  284. * Quiet informational messages.
  285. *
  286. * @public
  287. * @return {MochaWebpack}
  288. */
  289. }, {
  290. key: 'quiet',
  291. value: function quiet() {
  292. this.options = (0, _extends3.default)({}, this.options, {
  293. quiet: true
  294. });
  295. return this;
  296. }
  297. /**
  298. * Use inline diffs rather than +/-.
  299. *
  300. * @public
  301. * @param {boolean} inlineDiffs
  302. * @return {MochaWebpack}
  303. */
  304. }, {
  305. key: 'useInlineDiffs',
  306. value: function useInlineDiffs(inlineDiffs) {
  307. this.options = (0, _extends3.default)({}, this.options, {
  308. useInlineDiffs: inlineDiffs
  309. });
  310. return this;
  311. }
  312. /**
  313. * Set the timeout in milliseconds. Value of 0 disables timeouts
  314. *
  315. * @public
  316. * @param {number} timeout time in ms
  317. * @return {MochaWebpack}
  318. */
  319. }, {
  320. key: 'timeout',
  321. value: function (_timeout) {
  322. function timeout(_x5) {
  323. return _timeout.apply(this, arguments);
  324. }
  325. timeout.toString = function () {
  326. return _timeout.toString();
  327. };
  328. return timeout;
  329. }(function (timeout) {
  330. this.options = (0, _extends3.default)({}, this.options, {
  331. timeout: timeout
  332. });
  333. return this;
  334. })
  335. /**
  336. * Set the number of times to retry failed tests.
  337. *
  338. * @public
  339. * @param {number} count retry times
  340. * @return {MochaWebpack}
  341. */
  342. }, {
  343. key: 'retries',
  344. value: function retries(count) {
  345. this.options = (0, _extends3.default)({}, this.options, {
  346. retries: count
  347. });
  348. return this;
  349. }
  350. /**
  351. * Set slowness threshold in milliseconds.
  352. *
  353. * @public
  354. * @param {number} threshold time in ms
  355. * @return {MochaWebpack}
  356. */
  357. }, {
  358. key: 'slow',
  359. value: function slow(threshold) {
  360. this.options = (0, _extends3.default)({}, this.options, {
  361. slow: threshold
  362. });
  363. return this;
  364. }
  365. /**
  366. * Makes all tests async (accepting a callback)
  367. *
  368. * @public
  369. * @return {MochaWebpack}
  370. */
  371. }, {
  372. key: 'asyncOnly',
  373. value: function asyncOnly() {
  374. this.options = (0, _extends3.default)({}, this.options, {
  375. asyncOnly: true
  376. });
  377. return this;
  378. }
  379. /**
  380. * Delay root suite execution.
  381. *
  382. * @public
  383. * @return {MochaWebpack}
  384. */
  385. }, {
  386. key: 'delay',
  387. value: function delay() {
  388. this.options = (0, _extends3.default)({}, this.options, {
  389. delay: true
  390. });
  391. return this;
  392. }
  393. /**
  394. * Force interactive mode (default enabled in terminal)
  395. *
  396. * @public
  397. * @param {boolean} interactive
  398. * @return {MochaWebpack}
  399. */
  400. }, {
  401. key: 'interactive',
  402. value: function (_interactive) {
  403. function interactive(_x6) {
  404. return _interactive.apply(this, arguments);
  405. }
  406. interactive.toString = function () {
  407. return _interactive.toString();
  408. };
  409. return interactive;
  410. }(function (interactive) {
  411. this.options = (0, _extends3.default)({}, this.options, {
  412. interactive: interactive
  413. });
  414. return this;
  415. })
  416. /**
  417. * Enable growl notification support
  418. *
  419. * @public
  420. * @param {boolean} growl
  421. * @return {MochaWebpack}
  422. */
  423. }, {
  424. key: 'growl',
  425. value: function growl() {
  426. this.options = (0, _extends3.default)({}, this.options, {
  427. growl: true
  428. });
  429. return this;
  430. }
  431. /**
  432. * Run tests
  433. *
  434. * @public
  435. * @return {Promise<number>} a Promise that gets resolved with the number of failed tests or rejected with build error
  436. */
  437. }, {
  438. key: 'run',
  439. value: function run() {
  440. return new Promise(function ($return, $error) {
  441. var runner = new _TestRunner2.default(this.entries, this.includes, this.options);
  442. (0, _testRunnerReporter2.default)({
  443. eventEmitter: runner,
  444. interactive: this.options.interactive,
  445. quiet: this.options.quiet,
  446. cwd: this.options.cwd
  447. });
  448. return $return(runner.run());
  449. }.$asyncbind(this));
  450. }
  451. /**
  452. * Run tests and rerun them on changes
  453. * @public
  454. */
  455. }, {
  456. key: 'watch',
  457. value: function watch() {
  458. return new Promise(function ($return, $error) {
  459. var runner;
  460. runner = new _TestRunner2.default(this.entries, this.includes, this.options);
  461. (0, _testRunnerReporter2.default)({
  462. eventEmitter: runner,
  463. interactive: this.options.interactive,
  464. quiet: this.options.quiet,
  465. cwd: this.options.cwd
  466. });
  467. return runner.watch().then(function ($await_1) {
  468. return $return();
  469. }.$asyncbind(this, $error), $error);
  470. }.$asyncbind(this));
  471. }
  472. }]);
  473. return MochaWebpack;
  474. }();
  475. exports.default = MochaWebpack;