0
votes

I am currently following this example on shared behaviors for my mocha, sinon, and chai tests.

https://github.com/mochajs/mocha/wiki/Shared-Behaviours

Currently, I have tests that are suppose to be reused as I test my application.

In my shared test, I am passing a context which is going to pass an object component of a React store which i call var testObj = new ReactStore(data, ContainerModel);

Problem

I'm having trouble figuring out how to use my shared tests when I import them from the shared test file.

This is how the structure of the SharedTest.js looks like

   var shouldBehaveLikeACustomDomain = function (context) {
    //custom domain test
    describe("Custom Domain", function () {
        var domainData = context.domainData;
        //Methods for extended class
        describe("Methods", function () {
            //test for refresh method
            it("_refresh should call __onRefresh", function (done) {
                var customDomainClass = class extends context.domain {
                    constructor (domainData, StoreContainer) {
                        super(domainData, StoreContainer);
                    }
                    //recaluclate some properties every time domainData is refreshed
                    __onRefresh() {
                        assert(true);
                        done();
                    }
                };
                var url = new RegExp("^http\\:\\/\\/example\\.com\\/api\\/logEntries\\/" + domainData + "(\\?(.*))?$", "i");
                //server sends get request
                context.respondWith("GET", url, function (request) {
                    try {
                        request.respond(200, {}, "{}");
                    }
                    catch (e) {
                        done(e);
                    }
                });
                var testCustomObj = new customDomainClass(domainData, StoreContainer);
                testCustomObj._refresh();
            });

            //test load
            it ("#Actions:load should call __.onLoad()", function () {
                var isLoaded = false,
                    customDomainClass = class extends context.domain {
                        constructor (domainData, StoreContainer) {
                            super({}, domainData, StoreContainer);
                        }
                        //keep custom property up-to-date by using an onLoad hook
                        __onLoad() {
                            isLoaded = true;
                        }
                    };
                //create default response
                context.respondWith(SinonUtil.persistentResponse);
                //create instance
                var testCustomObj = new customDomainClass(domainData,StoreContainer);
                testCustomObj.Actions().load(testCustomObj.get('id'));
                assert.equal(isLoaded, true);
            });
        });
    });
};
module.exports = shouldBehaveLikeACustomDomain;

AnotherTestFile.js

var shared = require(./SharedTest.js);

describe ('#Domains', function () {
 beforeEach(function (){
   var testObj = new ReactStore(data, ContainerModel);
  });
   shared.shouldBehaveLikeACustomDomain();

 //some other tests here
});

Here is the error I am getting

    C:\Users\Demon\Desktop\hapnin.js\test\EventStore.test.js:217
        shared.shouldBehaveLikeACustomDomain(testObj);
               ^

TypeError: shared.shouldBehaveLikeACustomDomain is not a function
    at Suite.<anonymous> (C:\Users\Demon\Desktop\hapnin.js\test\EventStore.test.js:217:16)
    at Object.create (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\interfaces\common.js:114:19)
    at context.describe.context.context (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\interfaces\bdd.js:44:27)
    at Suite.<anonymous> (C:\Users\Demon\Desktop\hapnin.js\test\EventStore.test.js:215:5)
    at Object.create (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\interfaces\common.js:114:19)
    at context.describe.context.context (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\interfaces\bdd.js:44:27)
    at Object.<anonymous> (C:\Users\Demon\Desktop\hapnin.js\test\EventStore.test.js:166:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\mocha.js:230:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\mocha.js:227:14)
    at Mocha.run (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\mocha.js:495:10)
    at Object.<anonymous> (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\bin\_mocha:469:18)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
        at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3
1
are you sure that var shared = require(./shouldBehaveLikeACustomDomain.js); is an object that contains shouldBehaveLikeACustomDomain ?Alexey Kucherenko
oops. I'm suppose to import shared file. thanks for catching that.Demon
but it still doesnt solve my problem.Demon
same error or another?Alexey Kucherenko
It is still the same error.Demon

1 Answers

0
votes

This has nothing to do with Sinon, Mocha or Chai: this is simply to do with CommonJS and can be seen from the error message: you are not dealing with a function.

In your client code, you import ./SharedTest.js and try to reference a field on that module called shouldBehaveLikeACustomDomain. Then you get an error about there not being such a thing.

For you to have been able to use a field called shouldBehaveLikeACustomDomain, your module.exports would need to be an object containing a field with that name. It would look something like:

module.exports = { shouldBehaveLikeACustomDomain: shouldBehaveLikeACustomDomain };

It is not: you are directly exposing the function as the exported module.

module.exports = shouldBehaveLikeACustomDomain;

Therefore you only need to use the module directly in your code, not any embedded fields.

Instead of this

var shared = require(./SharedTest.js);

describe ('#Domains', function () {
 beforeEach(function (){
   var testObj = new ReactStore(data, ContainerModel);
  });
   shared.shouldBehaveLikeACustomDomain();

Just delete a few words:

const shouldBehaveLikeACustomDomain = require(./SharedTest.js);

describe ('#Domains', function () {
 beforeEach(function (){
   const testObj = new ReactStore(data, ContainerModel);
  });
   shouldBehaveLikeACustomDomain(); // this is missing a required argument

P.S. This still won't work, as your shouldBehaveLikeACustomDomain function requires an argument, which you are not passing in, but at least this answers your immediate question. Open another one if you have problems fixing this last one to avoid lowering Signal/Noise.