Installation for testing headless blocks using requirejs

This seems to be a kind of search for the Holy Grail, but I'm looking for a setup for a JavaScript module testing environment. Requirements:

  • testing AMD module requirements
  • isolate each module by mocking dependencies
  • can check in browser during development
  • tested in a headless environment for continuous integration

Everything but insane ridicule seems to be straightforward.

So far I have tested the JS-Test-Driver , Karma and Phantomjs , and for ridicule I have used Squire and Isolate - as well as the implementation of the code in response here , and nothing seems to work exactly. The main problem that I constantly encounter is that the test environment returns before all tests are run - primarily because poppies need their own require() dependencies.

Any help (or scaffold) will really help!

[edit]

I put the basic, working Karma project on Github with some trial bullying, using chai-expect as the appropriate library. I will try to add more useful documentation, but if you are familiar with karma, it is quite simple to expand it. Just git clone and then npm install to run it.

+4
source share
1 answer

Typically, each test environment allows you to run asynchronous tests and run tests manually, which should be what you need.

QUnit

For QUnit, you need to set the autostart parameter to false in order to activate the kick tests manually:

 QUnit.config.autostart = false; require(['test/qunit/test.js'], function() { QUnit.start(); }); 

If you load things asynchronously during tests, just use the QUnits stop() and start() methods:

 test('something that loads asynchronously', function() { stop(); define(['something'], function(sth) { ok(sth, 'Something loaded'); start(); }); }); 

Mocha

Mocha works very similarly:

 /*globals mocha */ mocha.setup('bdd'); require([ 'test/mocha/test.js' ], function() { mocha.run(); }); 

Asynchronous tests are even better by declaring a parameter that will be a callback for your specification:

 describe('an async something', function() { it('loads the dependency', function(done) { define(['something'], function(sth) { ok(sth, 'Something loaded'); done(); }); }); }); 

That should work. It's one thing to keep in mind that tests may not always run in the same order if you download test files using RequireJS.

+5
source

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


All Articles