to-have-been-warned.js 981 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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(() => {
  22. spyOn(console, 'error')
  23. jasmine.addMatchers({
  24. toHaveBeenWarned: () => {
  25. return {
  26. compare: 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. })