Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Last active November 23, 2022 08:22
Show Gist options
  • Save sauceaaron/6e69fe3de106ea3750b85175ca6c4985 to your computer and use it in GitHub Desktop.
Save sauceaaron/6e69fe3de106ea3750b85175ca6c4985 to your computer and use it in GitHub Desktop.
Get test name and status in before / after hooks with mocha.js
var assert = require("assert");
describe("tests with annotations", function()
{
it("should print the test name in a before hook", function()
{
assert(true);
});
beforeEach("annotate before", function beforeEach()
{
console.log("[BEFORE:currentTest.title]", this.currentTest.title);
console.log("[BEFORE:currentTest.fullTitle()]", this.currentTest.fullTitle());
});
afterEach("annotate after, function afterEach()
{
console.log("[AFTER:currentTest.title]", this.currentTest.title);
console.log("[AFTER:currentTest.fullTitle()]", this.currentTest.fullTitle());
console.log("[AFTER:currentTest.state]", this.currentTest.state);
});
})
@rsweetland
Copy link

Thanks for sharing. Just what I was looking for...

@liznyamu
Copy link

liznyamu commented Nov 29, 2021

@sauceaaron is it possible to get the test config in before / after hooks with mocha.js

var assert = require("assert");

describe("tests with annotations", function()
{
	it("should print the test name in a before hook", {tags=['@production']},function()
	{
		assert(true);
	});

       afterEach("annotate after, function afterEach() 
	{
		console.log("[AFTER:currentTest.configs]", this.currentTest.config());
	});
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment