index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Vue from 'vue'
  2. Vue.config.preserveWhitespace = false
  3. if (typeof console === 'undefined') {
  4. window.console = {
  5. error: function () {}
  6. }
  7. }
  8. function hasWarned (msg) {
  9. var count = console.error.calls.count()
  10. var args
  11. while (count--) {
  12. args = console.error.calls.argsFor(count)
  13. if (args.some(containsMsg)) {
  14. return true
  15. }
  16. }
  17. function containsMsg (arg) {
  18. if (arg instanceof Error) throw arg
  19. return typeof arg === 'string' && arg.indexOf(msg) > -1
  20. }
  21. }
  22. // define custom matcher for warnings
  23. beforeEach(function () {
  24. spyOn(console, 'error')
  25. jasmine.addMatchers({
  26. toHaveBeenWarned: function () {
  27. return {
  28. compare: function (msg) {
  29. var warned = Array.isArray(msg)
  30. ? msg.some(hasWarned)
  31. : hasWarned(msg)
  32. return {
  33. pass: warned,
  34. message: warned
  35. ? 'Expected message "' + msg + '" not to have been warned'
  36. : 'Expected message "' + msg + '" to have been warned'
  37. }
  38. }
  39. }
  40. }
  41. })
  42. })
  43. // helper for async assertions.
  44. // Use like this:
  45. //
  46. // vm.a = 123
  47. // waitForUpdate(() => {
  48. // expect(vm.$el.textContent).toBe('123')
  49. // vm.a = 234
  50. // })
  51. // .then(() => {
  52. // // more assertions...
  53. // done()
  54. // })
  55. // .catch(done)
  56. window.waitForUpdate = initialCb => {
  57. let onError
  58. const queue = [initialCb]
  59. function shift () {
  60. const job = queue.shift()
  61. let hasError = false
  62. try {
  63. job()
  64. } catch (e) {
  65. hasError = true
  66. if (onError) {
  67. onError(e)
  68. }
  69. }
  70. if (!hasError) {
  71. if (queue.length) {
  72. Vue.nextTick(shift)
  73. }
  74. }
  75. }
  76. Vue.nextTick(shift)
  77. const chainer = {
  78. then: nextCb => {
  79. queue.push(nextCb)
  80. return chainer
  81. },
  82. catch: errorCb => {
  83. onError = errorCb
  84. return chainer
  85. }
  86. }
  87. return chainer
  88. }
  89. // require all test files
  90. const testsContext = require.context('./', true, /\.spec$/)
  91. testsContext.keys().forEach(testsContext)