Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to appropriately reuse the describe blocks of mocha tests?

I have an app that runs in different modes (think of it as running for different platforms as well as using different protocols), one of which has a long loading period every time a page is opened. There are some other minor changes, but all of those could just be taken care of using wdio's setting variables.

Currently I have one test file (with a describe) for each section of the app. Which would be fine if one of the configurations being tested didn't have such a long wait time. Anyway, I've decided to deal with this test case, to handle it all in one file, which will all be on the same page.

Anyway, instead of copying and pasting all the tests I had previously to this one large file I was wondering if I could somehow reuse them, as if they were functions.

As it is right now I did just wrap things in functions, so for example:

// test1.js
module.exports = function test1 () {
  describe('Test1', function () {
    var settings = {}

    before(function () {
     // do something
    })

    it('do something', function () {
      assert.ok(true)
    })
    it('do something else', function () {
          assert.ok(true)
    })
  })
}

In another file we run every single function we created:

test1 = require('./test1')
test2 = require('./test2')
...
test10 = require('./test10')
describe('Main Test', function () {
  test1()
  test2()
  ...
  test10()
}

This would have solved my DRY problem, if I could somehow select which test functions to run upon my command using

wdio wdio/wdio.conf.js --specs wdio/test/spects/browser/test1.js

Which obviously will not work.

Basically I want a solution to be able to reuse my tests (the describe blocks). Is what I was doing the right path? If not, how should it be done?

like image 533
theJuls Avatar asked Jul 27 '17 18:07

theJuls


2 Answers

So I have figured out the best way to go about this after I found some documentation about it here.

I will just do as I previously described, however instead of shoving all those functions in the same file, I'll keep them in their own files. There still may be a better solution out there, but it is still an improvement from copying and pasting all test cases for the different modes of running my app.

like image 58
theJuls Avatar answered Oct 31 '22 23:10

theJuls


Just programmatically create different describe blocks. Wrap the describe block in a function with all the parameters that change (including the name of the block) and simply invoke the function to create the variations.

I made a small repo to show this in practice: https://github.com/fatso83/forum-support-code/commit/cb2bc10b1d8bdae31e8f0a8c4e724c70583a5e11

like image 21
oligofren Avatar answered Oct 31 '22 22:10

oligofren