question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling shared tests does not honor before/after hooks

See original GitHub issue

Prerequisites

  • Checked that your issue hasn’t already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn’t just a feature that actually isn’t supported in the environment in question or a bug in your code.
  • ‘Smoke tested’ the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

Steps to Reproduce

/* eslint-env mocha */
const assert = require('assert');

const genVar = () => new Promise((resolve) => {
  setTimeout(resolve(1), 500);
});

const sharedTest = (i) => {
  it('should be 1', () => {
    assert.equal(i, 1);
  });
};

describe('shared', () => {
  let i = 0;
  before(async () => {
    i = await genVar();
  });

  it('should be 1', () => {
    assert.equal(i, 1);
  });

  sharedTest(i);  // failed because i == 0

  after(() => {
    i = 2;
  });
});

Expected behavior: both tests passed

Actual behavior: [What actually happens]

sharedTest(i) would failed.

Reproduces how often: [What percentage of the time does it reproduce?] 100%

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 6.2.0
  • The output of node --version: v10.16.2
  • Your operating system
    • name and version: manjaro
    • architecture (32 or 64-bit): 64
  • Your shell (e.g., bash, zsh, PowerShell, cmd): zsh
  • Your browser and version (if running browser tests):
  • Any third-party Mocha-related modules (and their versions):
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version):

Additional Information

I was tring to implement some Shared Behaviours in https://github.com/mochajs/mocha/wiki/Shared-Behaviours to reuse some test cases. But I got some async call in my before hook, calling the shared test would not wait the promise in before hook to resolve.

Wrapping sharedTest(i); in another it would cause other problems.

If calling paramized test cases would always fail, how to do it correctly?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
gwh-cpnetcommented, Aug 21, 2019

@DrizzlingCattus Thanks for your comment. The this context matters. So I have to write the tests in this way:

/* eslint-env mocha */
/* eslint-disable no-console */
const assert = require('assert');

const genVar = () => new Promise((resolve) => {
  setTimeout(resolve, 500, 1);
});

const sharedTest = function () {
  it('should be 2', function () {
    assert.equal(this.i, 2);
  });
};

describe('shared', () => {
  before(function (done) {
    console.log('before plain');
    genVar().then((i) => {
      this.i = i;
      done();
    });
  });

  it('should be 1', function () {
    assert.equal(this.i, 1);
    this.i += 1;
  });

  sharedTest();

  it('should be 2', function () {
    assert.equal(this.i, 2);
    this.i += 1;
  });

  after(function () {
    this.i += 1;
    console.log('after');
    assert.equal(this.i, 4);
  });
});

Instead of passing test target as an argument to shared test, we should use this. Well, it works, all tests pass, but the code just looks from the es5 dynasty.

0reactions
gwh-cpnetcommented, Aug 22, 2019
/* eslint-disable no-console */
const assert = require('assert');

const init = () => new Promise((resolve) => {
  setTimeout(resolve, 500, { k: 1 });
});

const sharedTest = (x, v) => {
  it(`x.k should be ${v}`, function () {
    console.log(this.x);
    assert.equal(x.k, v); // always failed
  });

  it(`this.x.k should be ${v}`, function () {
    assert.equal(this.x.k, v);
  });
};

describe('shared', () => {
  let x = { k: 0 };
  before(async function () {
    console.log('before plain');
    this.x = await init();
    x = await init();
  });

  it('should be 1', function () {
    assert.equal(x.k, 1);
    assert.equal(this.x.k, 1);
    x.k += 1;
    this.x.k += 1;
  });

  sharedTest(x, 2);

  it('should be 2', function () {
    assert.equal(x.k, 2);
    assert.equal(this.x.k, 2);
    x.k += 1;
    this.x.k += 1;
  });

  sharedTest(x, 3);

  after(function () {
    console.log('after');
    x.k += 1;
    this.x.k += 1;
    assert.equal(x.k, 4);
    assert.equal(this.x.k, 4);
  });
});

here is a test case for passing arguments to shared tests. this.x works as expected. But the local x works interesting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

before and after global hooks? Concurrently testing a shared ...
I'm doing some database testing & I have multiple test files. When all the tests complete, I'd like to drop the table. The...
Read more >
Before and After Suite execution hook in jUnit 4.x
I've tried using maven's pre- & post-integration-test phases, but, if a test fails, maven stops and doesn't run post-integration-test , which is no...
Read more >
Avoid Nesting when you're Testing - Kent C. Dodds
Why using hooks like beforeEach as a mechanism for code reuse leads to unmaintainable tests and how to avoid it.
Read more >
Spock Framework Reference Documentation
Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful...
Read more >
A Complete Guide to Testing React Hooks - Toptal
This is because the state update in useStaleRefresh hook happens outside act(). To make sure DOM updates are processed timely, React recommends you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found