5

I could not find how to remove code duplication in Javascript (basically what I would achieve in Java with base classes).

The concrete example is (at least) the following code, which is common to all spec files (and potentially page objects, since I am testing with that pattern in protractor):

var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
var expect = chai.expect;

Can I do something to have expect available everywhere? I tried in the protractor configuration to load a file with that before the specs:

specs: [
  'e2e/helpers/commonDefinitions.js',
  'e2e/**/*.spec.js'
]

or use beforeLaunch or onPrepare (but want a function, not sure how to expose vars that way), but without success.

However, I would prefer a generic Javascript approach to this kind of code reuse.

Is there any good way to avoid repeating this kind of common code everywhere, especially in tests (mocha, karma, protractor)?

1 Answer 1

3

One approach is to place that code in a separate file:

assert-styles.js

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var expect = chai.expect;
var should = chai.should;

module.exports = {
  expect: expect,
  should: should
};

and then import it to your other test files:

test1.js

var assertStyles = require('./assert-styles');
var expect = assertStyles.expect;
var should = assertStyles.should;
5
  • Thanks, this is good if eventually I need only expect, as is the case here. But would there be a way to export multiple vars, making really a file exposing many modules needed by others? Because if I do a require per file/module, I will gain very little reusability :)
    – cowst
    Feb 11, 2015 at 12:43
  • @cowst definitely, just export and object module.exports = {expect: expect, assert: assert}; and then require like so, var common = require('./common'); var expect common.expect; var assert = common.assert; Feb 11, 2015 at 17:50
  • Ok, this way is convenient to keep all helper libs in one place, and in cases like the above, where we use 4 lines to get to the 1 var we need. If we would just declare directly 1 needed var per line, we would rewrite each line in the files using them, so instead of saving code we would write one line extra everywhere (the require) :D I hoped for something more convenient, were injecting one file (written in the right way) would provide you already the visibility of the vars. Anyway, please edit your answer so I can mark it as "best we can do" for my needs :)
    – cowst
    Feb 12, 2015 at 7:36
  • please edit your answer so I can mark it as "best we can do" for my needs :)
    – cowst
    Feb 16, 2015 at 10:06
  • 1
    @cowst I don't think there's a good way around it. You can use the GLOBAL object but it's considered bad practice. stackoverflow.com/questions/5447771/node-js-global-variables Feb 16, 2015 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.