index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. if (typeof console === 'undefined') {
  2. window.console = {
  3. error: function () {}
  4. }
  5. }
  6. function hasWarned (msg) {
  7. var count = console.error.calls.count()
  8. var args
  9. while (count--) {
  10. args = console.error.calls.argsFor(count)
  11. if (args.some(containsMsg)) {
  12. return true
  13. }
  14. }
  15. function containsMsg (arg) {
  16. if (arg instanceof Error) throw arg
  17. return typeof arg === 'string' && arg.indexOf(msg) > -1
  18. }
  19. }
  20. // define custom matcher for warnings
  21. beforeEach(function () {
  22. spyOn(console, 'error')
  23. jasmine.addMatchers({
  24. toHaveBeenWarned: function () {
  25. return {
  26. compare: function (msg) {
  27. var warned = Array.isArray(msg)
  28. ? msg.some(hasWarned)
  29. : hasWarned(msg)
  30. return {
  31. pass: warned,
  32. message: warned
  33. ? 'Expected message "' + msg + '" not to have been warned'
  34. : 'Expected message "' + msg + '" to have been warned'
  35. }
  36. }
  37. }
  38. }
  39. })
  40. })
  41. // require all test files
  42. var testsContext = require.context('./', true, /\.spec$/)
  43. testsContext.keys().forEach(testsContext)