3

My project is developed with TypeScript for frontend.

  • Mocha - for unit test
  • JSDOM - for DOM mocking
  • Hammer.js - for touch UX
  • Grunt, JSMin, etc.

The project file structure is something like this:

index.ts
mymodule.ts
mymodule_test.ts

As Hammer.js uses the global window and document object internally, I added some tricky code before the import statement for test only purposes.

mymodule.ts

1: import * as jsdom from 'jsdom';
2: if (typeof window === 'undefined') {
3:     let testDom: HTMLDocument = jsdom.jsdom('', undefined);
4:     (<any> global).window = testDom.defaultView;
5:     (<any> global).document = testDom;
6: }
7: import * as Hammer from 'hammerjs';
...

Now test runs successfuly. But build fails and spits out the following error:

Running "shell:minify" (shell) task

/home/yunbo/Workspace/jobkey/frontend/node_modules/jsmin/jsmin.js:231
            throw 'Error: unterminated string literal: ' + a;
            ^
Error: unterminated string literal: 

I searched for a while and found some Q&As about such an error. To avoid that error, I should modify the JSDOM's internal code or remove 1 - 6 lines in mymodule.ts. But both are not affordable.

  • If I remove 1 - 6 lines in mymodule.ts, I cannot run the tests.
  • modifying the original code of JSDOM or JSMin seems not the right way.

How can I solve this issue? Should I use another JavaScript minifier? Or should not test mymodule.js that includes Hammer.js import statement?

DumTux
  • 668
  • 1
  • 9
  • 24
  • I temporarily solved this problem using __Terser__. As a friend of mine proposed, I tried UglifyJS 3, JSMin 2, and Terser. Only Terser was able to compress JSDOM. – DumTux Feb 10 '20 at 12:38
  • But a more detailed explanation or another approach will be accepted as a valid answer. Please let me know more about this issue. Thank you. – DumTux Feb 10 '20 at 12:40

0 Answers0