watchdog.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals setTimeout, window, console, document */
  6. import Watchdog from '../src/watchdog';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. describe( 'Watchdog', () => {
  11. afterEach( () => sinon.restore() );
  12. describe( 'create()', () => {
  13. it( 'should create an editor instance', () => {
  14. const watchdog = new Watchdog();
  15. const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
  16. const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
  17. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  18. watchdog.setDestructor( editor => editor.destroy() );
  19. return watchdog.create( document.createElement( 'div' ), {} )
  20. .then( () => {
  21. sinon.assert.calledOnce( editorCreateSpy );
  22. sinon.assert.notCalled( editorDestroySpy );
  23. return watchdog.destroy();
  24. } )
  25. .then( () => {
  26. sinon.assert.calledOnce( editorCreateSpy );
  27. sinon.assert.calledOnce( editorDestroySpy );
  28. } );
  29. } );
  30. it( 'should throw an error when the creator is not defined', () => {
  31. const watchdog = new Watchdog();
  32. watchdog.setDestructor( editor => editor.destroy() );
  33. expect( () => watchdog.create() ).to.throw( CKEditorError, /^watchdog-creator-not-defined/ );
  34. } );
  35. it( 'should throw an error when the destructor is not defined', () => {
  36. const watchdog = new Watchdog();
  37. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  38. expect( () => watchdog.create() ).to.throw( CKEditorError, /^watchdog-destructor-not-defined/ );
  39. } );
  40. } );
  41. describe( 'restart()', () => {
  42. it( 'should restart the editor', () => {
  43. const watchdog = new Watchdog();
  44. const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
  45. const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
  46. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  47. watchdog.setDestructor( editor => editor.destroy() );
  48. return watchdog.create( document.createElement( 'div' ), {} )
  49. .then( () => {
  50. sinon.assert.calledOnce( editorCreateSpy );
  51. sinon.assert.notCalled( editorDestroySpy );
  52. return watchdog.restart();
  53. } )
  54. .then( () => {
  55. sinon.assert.calledTwice( editorCreateSpy );
  56. sinon.assert.calledOnce( editorDestroySpy );
  57. return watchdog.destroy();
  58. } );
  59. } );
  60. it( 'should restart the editor with the same data', () => {
  61. const watchdog = new Watchdog();
  62. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  63. watchdog.setDestructor( editor => editor.destroy() );
  64. return watchdog.create( document.createElement( 'div' ), {
  65. initialData: '<p>foo</p>',
  66. plugins: [ Paragraph ]
  67. } )
  68. .then( () => {
  69. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  70. return watchdog.restart();
  71. } )
  72. .then( () => {
  73. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  74. return watchdog.destroy();
  75. } );
  76. } );
  77. } );
  78. describe( 'editor', () => {
  79. it( 'should be the current editor instance', () => {
  80. const watchdog = new Watchdog();
  81. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  82. watchdog.setDestructor( editor => editor.destroy() );
  83. expect( watchdog.editor ).to.be.null;
  84. let oldEditor;
  85. return watchdog.create( document.createElement( 'div' ), {} )
  86. .then( () => {
  87. oldEditor = watchdog.editor;
  88. expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
  89. return watchdog.restart();
  90. } )
  91. .then( () => {
  92. expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
  93. expect( watchdog.editor ).to.not.equal( oldEditor );
  94. return watchdog.destroy();
  95. } )
  96. .then( () => {
  97. expect( watchdog.editor ).to.be.null;
  98. } );
  99. } );
  100. } );
  101. describe( 'error handling', () => {
  102. it( 'Watchdog should not restart editor during the initialization', () => {
  103. const watchdog = new Watchdog();
  104. watchdog.setCreator( el =>
  105. FakeEditor.create( el )
  106. .then( () => Promise.reject( new Error( 'foo' ) ) )
  107. );
  108. watchdog.setDestructor( editor => editor.destroy() );
  109. return watchdog.create( document.createElement( 'div' ) ).then(
  110. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  111. err => {
  112. expect( err ).to.be.instanceOf( Error );
  113. expect( err.message ).to.equal( 'foo' );
  114. }
  115. );
  116. } );
  117. it( 'Watchdog should not restart editor during the destroy', () => {
  118. const watchdog = new Watchdog();
  119. watchdog.setCreator( el => FakeEditor.create( el ) );
  120. watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
  121. return Promise.resolve()
  122. .then( () => watchdog.create( document.createElement( 'div' ) ) )
  123. .then( () => watchdog.destroy() )
  124. .then(
  125. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  126. err => {
  127. expect( err ).to.be.instanceOf( Error );
  128. expect( err.message ).to.equal( 'foo' );
  129. }
  130. );
  131. } );
  132. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  133. const watchdog = new Watchdog();
  134. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  135. watchdog.setDestructor( editor => editor.destroy() );
  136. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  137. const originalErrorHandler = window.onerror;
  138. window.onerror = undefined;
  139. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  140. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  141. return new Promise( res => {
  142. watchdog.on( 'restart', () => {
  143. window.onerror = originalErrorHandler;
  144. watchdog.destroy().then( res );
  145. } );
  146. } );
  147. } );
  148. } );
  149. it( 'Watchdog should not hide intercepted errors', () => {
  150. const watchdog = new Watchdog();
  151. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  152. watchdog.setDestructor( editor => editor.destroy() );
  153. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  154. const originalErrorHandler = window.onerror;
  155. const windowErrorSpy = sinon.spy();
  156. window.onerror = windowErrorSpy;
  157. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  158. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  159. return new Promise( res => {
  160. watchdog.on( 'restart', () => {
  161. window.onerror = originalErrorHandler;
  162. sinon.assert.calledOnce( windowErrorSpy );
  163. expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.equal( 'Uncaught CKEditorError: foo' );
  164. watchdog.destroy().then( res );
  165. } );
  166. } );
  167. } );
  168. } );
  169. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  170. const watchdog = new Watchdog();
  171. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  172. watchdog.setDestructor( editor => editor.destroy() );
  173. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  174. const originalErrorHandler = window.onerror;
  175. window.onerror = undefined;
  176. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  177. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  178. return new Promise( res => {
  179. watchdog.on( 'restart', () => {
  180. window.onerror = originalErrorHandler;
  181. watchdog.destroy().then( res );
  182. } );
  183. } );
  184. } );
  185. } );
  186. it( 'Watchdog should not intercept non-editor errors', () => {
  187. const watchdog = new Watchdog();
  188. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  189. watchdog.setDestructor( editor => editor.destroy() );
  190. const editorErrorSpy = sinon.spy();
  191. watchdog.on( 'error', editorErrorSpy );
  192. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  193. const originalErrorHandler = window.onerror;
  194. window.onerror = undefined;
  195. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  196. setTimeout( () => {
  197. throw new Error( 'foo' );
  198. } );
  199. return new Promise( res => {
  200. setTimeout( () => {
  201. window.onerror = originalErrorHandler;
  202. sinon.assert.notCalled( editorErrorSpy );
  203. watchdog.destroy().then( res );
  204. }, 5 );
  205. } );
  206. } );
  207. } );
  208. it( 'Watchdog should not intercept other editor errors', () => {
  209. const watchdog1 = new Watchdog();
  210. const watchdog2 = new Watchdog();
  211. watchdog1.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  212. watchdog1.setDestructor( editor => editor.destroy() );
  213. watchdog2.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  214. watchdog2.setDestructor( editor => editor.destroy() );
  215. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  216. const originalErrorHandler = window.onerror;
  217. window.onerror = undefined;
  218. return Promise.all( [
  219. watchdog1.create( document.createElement( 'div' ) ),
  220. watchdog2.create( document.createElement( 'div' ) )
  221. ] ).then( () => {
  222. return new Promise( res => {
  223. const watchdog1ErrorSpy = sinon.spy();
  224. const watchdog2ErrorSpy = sinon.spy();
  225. watchdog1.on( 'restart', watchdog1ErrorSpy );
  226. watchdog2.on( 'restart', watchdog2ErrorSpy );
  227. setTimeout( () => throwCKEditorError( 'foo', watchdog2.editor ) );
  228. // TODO - timing.
  229. setTimeout( () => {
  230. window.onerror = originalErrorHandler;
  231. sinon.assert.notCalled( watchdog1ErrorSpy );
  232. sinon.assert.calledOnce( watchdog2ErrorSpy );
  233. Promise.all( [ watchdog1.destroy(), watchdog2.destroy() ] )
  234. .then( res );
  235. }, 5 );
  236. } );
  237. } );
  238. } );
  239. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the ctx', () => {
  240. const watchdog = new Watchdog();
  241. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  242. watchdog.setDestructor( editor => editor.destroy() );
  243. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  244. const originalErrorHandler = window.onerror;
  245. window.onerror = undefined;
  246. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  247. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor.model.document ) );
  248. return new Promise( res => {
  249. watchdog.on( 'restart', () => {
  250. window.onerror = originalErrorHandler;
  251. watchdog.destroy().then( res );
  252. } );
  253. } );
  254. } );
  255. } );
  256. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the ctx #2', () => {
  257. const watchdog = new Watchdog();
  258. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  259. watchdog.setDestructor( editor => editor.destroy() );
  260. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  261. const originalErrorHandler = window.onerror;
  262. window.onerror = undefined;
  263. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  264. setTimeout( () => throwCKEditorError( 'foo', { foo: [ 1, 2, 3, { bar: watchdog.editor } ] } ) );
  265. return new Promise( res => {
  266. watchdog.on( 'restart', () => {
  267. window.onerror = originalErrorHandler;
  268. watchdog.destroy().then( res );
  269. } );
  270. } );
  271. } );
  272. } );
  273. it( 'editor should be restarted maximum 3 times by default', () => {
  274. const watchdog = new Watchdog();
  275. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  276. watchdog.setDestructor( editor => editor.destroy() );
  277. const errorSpy = sinon.spy();
  278. watchdog.on( 'error', errorSpy );
  279. const restartSpy = sinon.spy();
  280. watchdog.on( 'restart', restartSpy );
  281. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  282. const originalErrorHandler = window.onerror;
  283. window.onerror = undefined;
  284. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  285. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  286. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  287. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  288. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  289. return new Promise( res => {
  290. setTimeout( () => {
  291. expect( errorSpy.callCount ).to.equal( 4 );
  292. expect( watchdog.crashes.length ).to.equal( 4 );
  293. expect( restartSpy.callCount ).to.equal( 3 );
  294. window.onerror = originalErrorHandler;
  295. watchdog.destroy().then( res );
  296. }, 5 );
  297. } );
  298. } );
  299. } );
  300. it( 'editor should be restarted maximum of `crashNumberLimit` if the option is set', () => {
  301. const watchdog = new Watchdog( { crashNumberLimit: 2 } );
  302. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  303. watchdog.setDestructor( editor => editor.destroy() );
  304. const errorSpy = sinon.spy();
  305. watchdog.on( 'error', errorSpy );
  306. const restartSpy = sinon.spy();
  307. watchdog.on( 'restart', restartSpy );
  308. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  309. const originalErrorHandler = window.onerror;
  310. window.onerror = undefined;
  311. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  312. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  313. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  314. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  315. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  316. return new Promise( res => {
  317. setTimeout( () => {
  318. expect( errorSpy.callCount ).to.equal( 4 );
  319. expect( watchdog.crashes.length ).to.equal( 4 );
  320. expect( restartSpy.callCount ).to.equal( 2 );
  321. window.onerror = originalErrorHandler;
  322. watchdog.destroy().then( res );
  323. }, 5 );
  324. } );
  325. } );
  326. } );
  327. it( 'Watchdog should warn if the CKEditorError missing its context', () => {
  328. const watchdog = new Watchdog();
  329. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  330. watchdog.setDestructor( editor => editor.destroy() );
  331. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  332. const originalErrorHandler = window.onerror;
  333. window.onerror = undefined;
  334. sinon.stub( console, 'error' );
  335. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  336. setTimeout( () => throwCKEditorError( 'foo' ) );
  337. return new Promise( res => {
  338. setTimeout( () => {
  339. window.onerror = originalErrorHandler;
  340. expect( watchdog.crashes ).to.deep.equal( [] );
  341. sinon.assert.calledOnce( console.error );
  342. sinon.assert.calledWithExactly(
  343. console.error,
  344. 'The error is missing its context and Watchdog cannot restart the proper editor.'
  345. );
  346. watchdog.destroy().then( res );
  347. }, 5 );
  348. } );
  349. } );
  350. } );
  351. it( 'editor should be restarted with the data before the crash #1', () => {
  352. const watchdog = new Watchdog();
  353. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  354. watchdog.setDestructor( editor => editor.destroy() );
  355. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  356. const originalErrorHandler = window.onerror;
  357. window.onerror = undefined;
  358. return watchdog.create( document.createElement( 'div' ), {
  359. initialData: '<p>foo</p>',
  360. plugins: [ Paragraph ]
  361. } ).then( () => {
  362. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  363. return new Promise( res => {
  364. watchdog.on( 'restart', () => {
  365. window.onerror = originalErrorHandler;
  366. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  367. watchdog.destroy().then( res );
  368. } );
  369. } );
  370. } );
  371. } );
  372. it( 'editor should be restarted with the data before the crash #2', () => {
  373. const watchdog = new Watchdog();
  374. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  375. watchdog.setDestructor( editor => editor.destroy() );
  376. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  377. const originalErrorHandler = window.onerror;
  378. window.onerror = undefined;
  379. return watchdog.create( document.createElement( 'div' ), {
  380. initialData: '<p>foo</p>',
  381. plugins: [ Paragraph ]
  382. } ).then( () => {
  383. const doc = watchdog.editor.model.document;
  384. watchdog.editor.model.change( writer => {
  385. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  386. } );
  387. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  388. return new Promise( res => {
  389. watchdog.on( 'restart', () => {
  390. window.onerror = originalErrorHandler;
  391. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  392. watchdog.destroy().then( res );
  393. } );
  394. } );
  395. } );
  396. } );
  397. it( 'editor should be restarted with the data of the latest document version before the crash', () => {
  398. const watchdog = new Watchdog();
  399. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  400. watchdog.setDestructor( editor => editor.destroy() );
  401. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  402. const originalErrorHandler = window.onerror;
  403. window.onerror = undefined;
  404. return watchdog.create( document.createElement( 'div' ), {
  405. initialData: '<p>foo</p>',
  406. plugins: [ Paragraph ]
  407. } ).then( () => {
  408. const doc = watchdog.editor.model.document;
  409. watchdog.editor.model.document.version = -1000;
  410. watchdog.editor.model.change( writer => {
  411. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  412. } );
  413. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  414. return new Promise( res => {
  415. watchdog.on( 'restart', () => {
  416. window.onerror = originalErrorHandler;
  417. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  418. watchdog.destroy().then( res );
  419. } );
  420. } );
  421. } );
  422. } );
  423. it( 'editor should be restarted with the latest available data before the crash', () => {
  424. const watchdog = new Watchdog();
  425. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  426. watchdog.setDestructor( editor => editor.destroy() );
  427. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  428. const originalErrorHandler = window.onerror;
  429. window.onerror = undefined;
  430. sinon.stub( console, 'error' );
  431. return watchdog.create( document.createElement( 'div' ), {
  432. initialData: '<p>foo</p>',
  433. plugins: [ Paragraph ]
  434. } ).then( () => {
  435. const editorGetDataError = new Error( 'Some error' );
  436. const getDataStub = sinon.stub( watchdog.editor, 'getData' )
  437. .throwsException( editorGetDataError );
  438. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  439. const doc = watchdog.editor.model.document;
  440. watchdog.editor.model.change( writer => {
  441. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  442. } );
  443. return new Promise( res => {
  444. watchdog.on( 'restart', () => {
  445. window.onerror = originalErrorHandler;
  446. sinon.assert.calledOnce( getDataStub );
  447. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  448. sinon.assert.calledWith(
  449. console.error,
  450. editorGetDataError,
  451. 'An error happened during restoring editor data. Editor will be restored from the previously saved data.'
  452. );
  453. watchdog.destroy().then( res );
  454. } );
  455. } );
  456. } );
  457. } );
  458. } );
  459. describe( 'crashes', () => {
  460. it( 'should be an array of caught errors by the Watchdog', () => {
  461. const watchdog = new Watchdog();
  462. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  463. watchdog.setDestructor( editor => editor.destroy() );
  464. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  465. const originalErrorHandler = window.onerror;
  466. window.onerror = undefined;
  467. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  468. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  469. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  470. return new Promise( res => {
  471. // TODO - timing.
  472. setTimeout( () => {
  473. window.onerror = originalErrorHandler;
  474. expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
  475. expect( watchdog.crashes[ 1 ].message ).to.equal( 'bar' );
  476. watchdog.destroy().then( res );
  477. }, 5 );
  478. } );
  479. } );
  480. } );
  481. } );
  482. } );
  483. // Wrap Editor to align the API to the "full" editors.
  484. class FakeEditor extends VirtualTestEditor {
  485. static create( elementOrData, config ) {
  486. return super.create( config );
  487. }
  488. }
  489. function throwCKEditorError( name, ctx ) {
  490. throw new CKEditorError( name, undefined, ctx );
  491. }