testr.js icon indicating copy to clipboard operation
testr.js copied to clipboard

Problem adapting testr to current test spec config.

Open jackysee opened this issue 12 years ago • 2 comments

Currently I have some test specs already in-place and would like to use testr. The spec itself use require.js to load the target module for testing. I'm not sure how should I config testr properly. Is it a must to use testr.run? The code is like:

runner.html

<html>
    <head>
        <script src="lib/jasmine.js"></script>
        <script src="lib/jasmine-html.js"></script>
        <script src="lib/jasmine-helper.js"></script>
        <scritp src="lib/require.js"></script>
        <!-- include testr here?? -->
        <script>
            require([
                'spec/xxx_spec.js',
                'spec/yyy_spec.js'
            ], function(){
                jasmineEnv.execute();
            });
        </script>
    </head>
</html>

xxx_spec.js

define(['components/xxx'], function(xxx){
    describe("xxx", function(){
        beforeEach(function(){
            //testr here?
        });
        it("should be defined", function(){
            expect(xxx).toBeDefined();
        });
    });

})

jackysee avatar Feb 06 '13 09:02 jackysee

@jackysee testr.js doesn't encourage the use of specs which are defined as AMD modules, as this doesn't really serve much of a purpose. The way your page should be sequenced:

  1. Load require.js
  2. Load testr.js - this will monkey-patch the require and define methods, allowing testr.js to capture each module "factory" function.
  3. Use the testr.run method, set the first parameter to the path of the file which contains your requirejs configuration. This file will be loaded, and subsequently load all of your top-level dependencies. Once all of your app-only code (ie. no spec/test code) has loaded.
  4. The callback is now executed, letting you know that all of your application code has been captured. Here, you can define and/or kick off your test suite, e.g. by calling mocha.run().

You have a few options as to how you want to get the specs into your page. The simplest being the use of multiple script tags. testr.js can also be configured to automatically load a spec file for each module it captures (see the readme). Or, you could use requirejs to load these files - remember that files loaded by requirejs don't have to contain a call to define. Requirejs will still load the file and run the callback once the file has loaded, a module does not need to be defined for each file loaded.

If you point me to a repo I'd be happy to give you a hand getting your tests setup correctly.

Cheers, Matt.

mattfysh avatar Feb 06 '13 10:02 mattfysh

A sample repo https://github.com/jackysee/testr-sample

On Wed, Feb 6, 2013 at 6:25 PM, Matt [email protected] wrote:

@jackysee https://github.com/jackysee doesn't encourage the use of specs which are defined as AMD modules, as this doesn't really serve much of a purpose. The way your page should be sequenced:

  1. Load require.js
  2. Load testr.js - this will monkey-patch the require and definemethods, allowing testr.js to capture each module "factory" function.
  3. Use the testr.run method, set the first parameter to the path of the file which contains your requirejs configuration. This file will be loaded, and subsequently load all of your top-level dependencies. Once all of your app-only code (ie. no spec/test code) has loaded.
  4. The callback is now executed, letting you know that all of your application code has been captured. Here, you can define and/or kick off your test suite, e.g. by calling mocha.run().

You have a few options as to how you want to get the specs into your page. The simplest being the use of multiple script tags. testr.js can also be configured to automatically load a spec file for each module it captures (see the readme). Or, you could use requirejs to load these files - remember that files loaded by requirejs don't have to contain a call to define. Requirejs will still load the file and run the callback once the file has loaded, a module does not need to be defined for each file loaded.

If you point me to a repo I'd be happy to give you a hand getting your tests setup correctly.

Cheers, Matt.

— Reply to this email directly or view it on GitHubhttps://github.com/mattfysh/testr.js/issues/14#issuecomment-13175361.

Best Regards, Jacky See

jackysee avatar Feb 06 '13 11:02 jackysee