Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test JavaScript minification output

We recently upgraded to a newer build of a JavaScript minification library.

After a significant amount of quality assurance work by the testing team, it was discovered that the new version of our minifier had an issue that changed the intention and meaning behind a block of code.

(Life lesson: don't upgrade JS minifiers unless you are really convinced you need the new version.)

The minifier is used for client side JavaScript code with a heavy emphasis on DOM related activity, not nearly as much "business logic".

A simplified example of what was broken by the minifier upgrade:

function process(count)
{
     var value = ""; 
     value += count; //1. Two consecutive += statements
     value += count;
     count++;        //2. Some other statement
     return value;   //3. Return
}

Was minified incorrectly to the following:

function process(n){var t="";return t+n+n,n++,t}

While we could write some unit tests to catch some of the issues potentially, given that the JavaScript is heavy on DOM interactions (data input, etc.), it's very difficult to test thoroughly without user testing (non-automated). We'd pondered using a JS to AST library like Esprima, but given the nature of the changes that could be done to the minified code, it would produce far too many false positives.

We also considered trying to write representative tests, but that seems like a never-ending task (and likely to miss cases).

FYI: This is a very sophisticated web application with several hundred thousand lines of JavaScript code.

We're looking for a methodology for testing the minification process short of "just test everything again, thoroughly, and repeat." We'd like to apply a bit more rigor/science to the process.

Ideally, we could try multiple minifiers without fear of each breaking our code in new subtle ways if we had a better scientific method for testing.

Update:

One idea we had was to:

  1. take minification with old version
  2. beautify it
  3. minify with new version,
  4. beautify, and
  5. visually diff.

It did seem like a good idea, however the differences were so common that the diff tool flagged nearly every line as being different.

like image 453
WiredPrairie Avatar asked Feb 12 '13 20:02

WiredPrairie


People also ask

How do you check if a js file is minified or not?

Clicking the audit reveals the list of JavaScript files that can be minified. GTmetrix reports the file size savings possible through JavaScript minification. GTmetrix also estimates the JavaScript file size savings possible through minification.

How do you do minification in JavaScript?

To minify JavaScript, try UglifyJS. The Closure Compiler is also very effective. You can create a build process that uses these tools to minify and rename the development files and save them to a production directory.

Should you minify JavaScript?

It is important to minify your CSS and minimise JavaScript files so they can load faster on your web pages. There are many reasons why you should minify your CSS and JavaScript: Reduce file size: The more code there is in a file, the larger it will be. Minified code is usually much smaller than the original version.

How do js minify cause work?

Minification works by analyzing and rewriting the text-based parts of a website to reduce its overall file size. Minification extends to scripts, style sheets, and other components that the web browser uses to render the site. Minification is performed on the web server before a response is sent.


1 Answers

Have you considered a unit test framework, such as QUnitjs ? It would be quite a bit of work to write the unit tests, but in the end you would have a repeatable test procedure.

like image 99
Eric Sexton Avatar answered Nov 15 '22 12:11

Eric Sexton