error.spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import Alpine from 'alpinejs'
  2. import { wait } from '@testing-library/dom'
  3. global.MutationObserver = class {
  4. observe() {}
  5. }
  6. jest.spyOn(window, 'setTimeout').mockImplementation((callback) => {
  7. callback()
  8. })
  9. const mockConsoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => {})
  10. beforeEach(() => {
  11. jest.clearAllMocks()
  12. })
  13. test('error in x-data eval contains element, expression and original error', async () => {
  14. document.body.innerHTML = `
  15. <div x-data="{ foo: 'bar' ">
  16. <span x-bind:foo="foo.bar"></span>
  17. </div>
  18. `
  19. await expect(Alpine.start()).rejects.toThrow()
  20. expect(mockConsoleWarn).toHaveBeenCalledWith(
  21. `Alpine Error: "SyntaxError: Unexpected token ')'"\n\nExpression: "{ foo: 'bar' "\nElement:`,
  22. document.querySelector('[x-data]'),
  23. )
  24. })
  25. test('error in x-init eval contains element, expression and original error', async () => {
  26. document.body.innerHTML = `
  27. <div x-data x-init="foo.bar = 'baz'">
  28. </div>
  29. `
  30. await Alpine.start()
  31. expect(mockConsoleWarn).toHaveBeenCalledWith(
  32. `Alpine Error: "ReferenceError: foo is not defined"\n\nExpression: "foo.bar = 'baz'"\nElement:`,
  33. document.querySelector('[x-data]'),
  34. )
  35. })
  36. test('error in x-spread eval contains element, expression and original error', async () => {
  37. document.body.innerHTML = `
  38. <div x-data x-spread="foo.bar">
  39. </div>
  40. `
  41. // swallow the rendering error
  42. await expect(Alpine.start()).rejects.toThrow()
  43. expect(mockConsoleWarn).toHaveBeenCalledWith(
  44. `Alpine Error: "ReferenceError: foo is not defined"\n\nExpression: "foo.bar"\nElement:`,
  45. document.querySelector('[x-data]'),
  46. )
  47. })
  48. test('error in x-bind eval contains element, expression and original error', async () => {
  49. document.body.innerHTML = `
  50. <div x-data="{ foo: null }">
  51. <span x-bind:foo="foo.bar"></span>
  52. </div>
  53. `
  54. await Alpine.start()
  55. expect(mockConsoleWarn).toHaveBeenCalledWith(
  56. `Alpine Error: "TypeError: Cannot read property 'bar' of null"\n\nExpression: "foo.bar"\nElement:`,
  57. document.querySelector('[x-bind:foo]'),
  58. )
  59. })
  60. test('error in x-model eval contains element, expression and original error', async () => {
  61. document.body.innerHTML = `
  62. <div x-data="{ foo: null }">
  63. <input x-model="foo.bar">
  64. </div>
  65. `
  66. await Alpine.start()
  67. expect(mockConsoleWarn).toHaveBeenCalledWith(
  68. `Alpine Error: "TypeError: Cannot read property 'bar' of null"\n\nExpression: "foo.bar"\nElement:`,
  69. document.querySelector('[x-model]',)
  70. )
  71. })
  72. test('error in x-for eval contains element, expression and original error', async () => {
  73. document.body.innerHTML = `
  74. <div x-data="{}">
  75. <template x-for="element in foo">
  76. <span x-text="element"></span>
  77. </template>
  78. </div>
  79. `
  80. await expect(Alpine.start()).rejects.toThrow()
  81. expect(mockConsoleWarn).toHaveBeenCalledWith(
  82. `Alpine Error: "ReferenceError: foo is not defined"\n\nExpression: "foo"\nElement:`,
  83. document.querySelector('[x-for]',)
  84. )
  85. })
  86. test('error in x-on eval contains element, expression and original error', async () => {
  87. document.body.innerHTML = `
  88. <div
  89. x-data="{hello: null}"
  90. x-on:click="hello.world"
  91. ></div>
  92. `
  93. await Alpine.start()
  94. document.querySelector('div').click()
  95. await wait(() => {
  96. expect(mockConsoleWarn).toHaveBeenCalledWith(
  97. `Alpine Error: "TypeError: Cannot read property 'world' of null"\n\nExpression: "hello.world"\nElement:`,
  98. document.querySelector('[x-data]',)
  99. )
  100. })
  101. })