Soil Test for UMD

I am trying to figure out a better approach to testing a Javascript module definition using a UMD factory similar to this: https://github.com/umdjs/umd/blob/master/returnExportsGlobal.js

I do not want to test the module itself, I want to check whether it is "exported / created" correctly in various environments:

  • If CommonJS (node), is the module exported correctly?
  • If AMD, is it correctly identified?
  • If the browser (without requirejs), is the global one created correctly?

I would like to run these tests using grunts and jasmine. I can use grunt-contrib-jasmine to test points 2 and 3, but not for point 1.

I think I can use a mixture of grunt-contrib-jasmine and grunt-jasmine-node to verify the correct module definitions (for a specific implementation, I still need to find out), but this is very annoying.

At a high level, does anyone know of any existing methods to achieve this without using multiple grunt plugins?

+6
source share
2 answers

In the end, I decided to go with hybrid tasks using grunt-contrib-jasmine for AMD's global browsers and jasmine_node for testing CommonJS. I have only one file specification that supports all 3 module type tests.

Here is my grunt configuration:

grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), jasmine: { browserGlobal: { src: ['src/Foo.js'], options: { specs: 'spec/**/*.spec.js' } }, browserAMD: { src: ['src/Foo.js'], options: { specs: 'spec/**/*.spec.js', template: require('grunt-template-jasmine-requirejs') } } }, jasmine_node: { specNameMatcher: 'spec', projectRoot: 'spec/' } }); 

My jasmine specification files now support UMD:

 (function (root, factory) { if (typeof module === 'object' && module.exports) { // Node/CommonJS factory( require('modulename') ); } else if (typeof define === 'function' && define.amd) { // AMD define([ 'modulename' ], factory); } else { // Browser globals factory(root.ModuleName); } }(this, function factory(ModuleName) { // Tests here })); 

And here I use the UMD factory for my module:

 (function (root, factory) { if (typeof module === 'object' && module.exports) { // Node/CommonJS module.exports = factory(); } else if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(factory); } else { // Browser globals root.ModuleName = factory(); } }(this, function factory() { // public API return { foo: 'bar' }; })); 
+4
source

You can also use uRequire and save yourself from all UMD templates in all of your modules using declarative functions .

You simply write simple AMD or simple CommonJS modules (or a combination of the two) and convert it to UMD (or optimized rjs combined.js , which works like nodejs , Web / AMD and Web / Script ) with a simple way to build and configure , or in the CLI or in grunt .

The produced UMD is based on well-known templates, such as https://github.com/umdjs/umd/blob/master/returnExportsGlobal.js , with various settings, one of which is declaratively export to window / global .

Then you can convert your simple AMD or commonJS specifications to UMD and / or "combined.js" and hit both in the browser and in fraud mode. See uBerscore for many simpler and more complex examples.

0
source

Source: https://habr.com/ru/post/946591/


All Articles