to-have-been-warned.js 1007 B

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