1- import { beforeEach , describe , it , expect } from 'vitest' ;
1+ import { beforeEach , describe , it , expect , vi } from 'vitest' ;
22import JavaScriptCompiler from '../../src/javascript/JavaScriptCompiler.js' ;
33import type BrowserWindow from '../../src/window/BrowserWindow.js' ;
44import Window from '../../src/window/Window.js' ;
55import BrowserErrorCaptureEnum from '../../src/browser/enums/BrowserErrorCaptureEnum.js' ;
6+ import * as PropertySymbol from '../../src/PropertySymbol.js' ;
67
78describe ( 'JavaScriptCompiler' , ( ) => {
89 let window : BrowserWindow ;
@@ -22,7 +23,7 @@ describe('JavaScriptCompiler', () => {
2223
2324 class TestClass {
2425 constructor() {
25- console.log('Hello \\\ 'World');
26+ console.log('Hello \\'World');
2627 }
2728
2829 async greet() {
@@ -34,13 +35,16 @@ describe('JavaScriptCompiler', () => {
3435 ` ;
3536 const compiler = new JavaScriptCompiler ( window ) ;
3637 const result = compiler . compile ( 'http://localhost:8080/js/app/main.js' , code ) ;
38+ const evaluateScript = vi . spyOn ( < any > window , PropertySymbol . evaluateScript ) ;
3739
38- expect ( result . execute . toString ( ) ) . toBe ( `function anonymous($happy_dom) {
40+ result . execute ( { dispatchError : ( ) => { } , dynamicImport : vi . fn ( ) } ) ;
41+
42+ expect ( evaluateScript . mock . calls [ 0 ] [ 0 ] ) . toBe ( `
3943 const variable = 'hello';
4044
4145 class TestClass {
4246 constructor() {
43- console.log('Hello \\\ 'World');
47+ console.log('Hello \\'World');
4448 }
4549
4650 async greet() {
@@ -49,12 +53,12 @@ describe('JavaScriptCompiler', () => {
4953 return someModule.getGreeting();
5054 }
5155 }
52- } ` ) ;
56+ ` ) ;
5357 } ) ;
5458
5559 it ( 'Handles import statement in strings.' , ( ) => {
5660 const code = `
57- var r = new RegExp(/^([1-9][0-9]*)(["″†'′ ´]?)\s*([1-9][0-9]*\\/[1-9][0-9]*)["″†]?$/);
61+ var r = new RegExp(/^([1-9][0-9]*)(["″â€'′à ´]?)\s*([1-9][0-9]*\\/[1-9][0-9]*)["″â€]?$/);
5862 const hexLookUp=Array.from({length:127},(n,e)=>/[^!"$&'()*+,\-.;=_\`a-z{}~]/u.test(String.fromCharCode(e)))
5963 class R{constructor(){this.lastTime=Date.now(),this.lastValue=0,this.__speed=0}set value(e){this.__speed=(e-this.lastValue)/(Date.now()-this.lastTime),this.lastValue=e,this.lastTime=Date.now()}}
6064 const n=["@import",\`url(\${JSON.stringify(t.href)}) import('@package/debugger')\`];const t="";
@@ -64,39 +68,43 @@ describe('JavaScriptCompiler', () => {
6468 ` ;
6569 const compiler = new JavaScriptCompiler ( window ) ;
6670 const result = compiler . compile ( 'http://localhost:8080/js/app/main.js' , code ) ;
71+ const evaluateScript = vi
72+ . spyOn ( < any > window , PropertySymbol . evaluateScript )
73+ . mockImplementation ( ( ) => { } ) ;
74+
75+ result . execute ( { dispatchError : ( ) => { } , dynamicImport : vi . fn ( ) } ) ;
6776
68- expect ( result . execute . toString ( ) ) . toBe ( `function anonymous($happy_dom) {
69- var r = new RegExp(/^([1-9][0-9]*)(["″†'′ ´]?)\s*([1-9][0-9]*\\/[1-9][0-9]*)["″†]?$/);
77+ expect ( evaluateScript . mock . calls [ 0 ] [ 0 ] ) . toBe ( `
78+ var r = new RegExp(/^([1-9][0-9]*)(["″â€'′à ´]?)\s*([1-9][0-9]*\\/[1-9][0-9]*)["″â€]?$/);
7079 const hexLookUp=Array.from({length:127},(n,e)=>/[^!"$&'()*+,-.;=_\`a-z{}~]/u.test(String.fromCharCode(e)))
7180 class R{constructor(){this.lastTime=Date.now(),this.lastValue=0,this.__speed=0}set value(e){this.__speed=(e-this.lastValue)/(Date.now()-this.lastTime),this.lastValue=e,this.lastTime=Date.now()}}
7281 const n=["@import",\`url(\${JSON.stringify(t.href)}) import('@package/debugger')\`];const t="";
7382 function log(){return console.log('To use the debugger you must import "@package/debugger"')}
7483 var i = "test";
7584 $happy_dom.dynamicImport("@package/debugger");
76- } ` ) ;
85+ ` ) ;
7786 } ) ;
7887
7988 it ( 'Adds try and catch statement if settings.errorCapture is set to "tryAndCatch".' , ( ) => {
8089 const window = new Window ( ) ;
8190
82- const code = `
83- const variable = 'hello';
84- console.log('Hello \\\'World');
85- ` ;
91+ const code = `throw new Error('Hello World');` ;
8692 const compiler = new JavaScriptCompiler ( window ) ;
8793 const result = compiler . compile ( 'http://localhost:8080/js/app/main.js' , code ) ;
94+ const dispatchError = vi . fn ( ) ;
8895
89- expect ( result . execute . toString ( ) ) . toBe ( `function anonymous($happy_dom) {try {
90- const variable = 'hello';
91- console.log('Hello \\'World');
92- } catch (error) { $happy_dom.dispatchError(error); }}` ) ;
96+ result . execute ( { dispatchError, dynamicImport : vi . fn ( ) } ) ;
97+
98+ expect ( dispatchError ) . toHaveBeenCalledWith ( new Error ( 'Hello World' ) ) ;
9399 } ) ;
94100
95101 it ( 'Throws await in top level error' , ( ) => {
96102 const code = `const StringUtility = await import('http://localhost:8080/js/utilities/StringUtility.js');` ;
97103 const compiler = new JavaScriptCompiler ( window ) ;
104+ const result = compiler . compile ( 'http://localhost:8080/js/app/main.js' , code ) ;
105+
98106 expect ( ( ) => {
99- compiler . compile ( 'http://localhost:8080/js/app/main.js' , code ) ;
107+ result . execute ( { dispatchError : ( ) => { } , dynamicImport : vi . fn ( ) } ) ;
100108 } ) . toThrow (
101109 `Failed to parse JavaScript in 'http://localhost:8080/js/app/main.js': await is only valid in async functions and the top level bodies of modules`
102110 ) ;
0 commit comments