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
var shared = require(./shouldBehaveLikeACustomDomain.js);
is an object that containsshouldBehaveLikeACustomDomain
? – Alexey Kucherenko