JavaScript compression error while compressing JSDOM with JSMin

198 views Asked by At

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?

0

There are 0 answers