1

I have this simple example unit test in Mocha:

const {Builder, By, Key, until} = require('selenium-webdriver')
const chai = require('chai')

chai.should()

var customer
var gigger
var admin

/* eslint-disable no-unused-expressions */

/* globals describe,it */

describe('start all tests', async function () {
  describe('create customer user', async function () {
    it('creates a user browser', async function () {
      customer = await new Builder().forBrowser('chrome').build()
      customer.should.not.be.null

      this.timeout(5000)

      await customer.get('http://www.google.com/ncr')
      await customer.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN)
      await customer.wait(until.titleIs('webdriver - Google Search'), 1000)
    })
    it('creates a gigger browser', async function () {
      gigger = await new Builder().forBrowser('chrome').build()
      gigger.should.not.be.null
    })

    it('creates an admin browser', async function () {
      admin = await new Builder().forBrowser('chrome').build()
      admin.should.not.be.null
    })
  })

  describe('close it all down', async function () {
    it('close all browsers down', async function () {
      await customer.quit()
      await gigger.quit()
      await admin.quit()
    })
  })
})

Everything works fine. BUT! If I un-comment the last describe:

//describe('close it all down', async function () {
  it('close all browsers down', async function () {
    await customer.quit()
    await gigger.quit()
    await admin.quit()
  })
//})

What actually happens is that close all browsers down actually runs straight away.

Is that because describe functions must contain either all describe functions, rather than it as well? If so, this is not documented.

Or, am I missing something?

ADDENDUM: In fact, how does Mocha actually work? I got so used to just typing it, that I never quite got into the actual workings of it. What does Mocha's describe() actually do? Does it just run the callback? When another nested describe() is called, how does it figure out that it's a nested one, and which one the parent is? How does it all work, "in general"?

Merc
  • 16,277
  • 18
  • 79
  • 122

2 Answers2

2

all your tests are running asynchronously so you cant control the execution flow of them, for what you want to do, you should use an after() see mocha hooks

For your other question you should see this thread where its really well explained. Happy Testing !

Constantin Guidon
  • 1,892
  • 16
  • 21
2

Yes, describe can contain a mix of describe and it in Mocha. And also, the beforeEach and afterEach defined in outer describe will apply to test cases in nested describe as well.

Run below example:

describe('test suite', () => {

  before(() => {
    console.log('    >>> outer before all');
  });

  after(() => {
    console.log('    <<< outer after all');
  });

  beforeEach(() => {
    console.log('      >>> outer before each');
  });

  afterEach(() => {
    console.log('      <<< outer after each');
  });

  describe('# nested test suite', () => {
    before(() => {
      console.log('      >>> # nested before all');
    });

    after(() => {
      console.log('      <<< # nested after all');
    });

    beforeEach(() => {
      console.log('      >>> # nested before each');
    });

    afterEach(() => {
      console.log('      <<< # nested after each');
    });

    it('nested test', () => { })

  })

  it('test', () => { })
});

From the output you can see the order of the hooks:

  test suite
    >>> outer before all                 \
      >>> outer before each     \         |
    ✓ test                       |        |
      <<< outer after each      /         |
    # nested test suite               \   |
      >>> # nested before all          |  |
      >>> outer before each        \   |  |
      >>> # nested before each  \   |  |  |
      ✓ nested test              |  |  |  |
      <<< # nested after each   /   |  |  |
      <<< outer after each         /   |  |
      <<< # nested after all          /   |
    <<< outer after all                  /
aleung
  • 9,848
  • 3
  • 55
  • 69