Is there a way of getting the name of the currently running test?
Some (heavily simplified) code may help explain. I want to avoid the duplication of "test1" / "test2"
in the calls to performTest
:
describe("My test category", function () { function performTest(uniqueName, speed) { var result = functionUnderTest(uniqueName, speed); expect(result).toBeTruthy(); } it("test1", function () { performTest("test1", "fast"); }); it("test2", function () { performTest("test2", "slow"); }); });
UPDATE I see the information I need is in:
jasmine.currentEnv_.currentSpec.description
or probably better:
jasmine.getEnv().currentSpec.description
Speak Human. Label your test suites ( describe blocks) and specs ( it blocks) in a way that clearly conveys the intention of each unit test. Note that the name of each test is the title of its it preceded by all its parent describe names. Favor assertive verbs and avoid ones like "should."
Using the done() Method in Your Jasmine-driven Asynchronous JavaScript Tests. Jasmine. Async is an add-on library for Jasmine that provides additional functionality to do asynchronous testing. Modeled after Mocha's async test support, it brings the done() function to the Jasmine unit testing environment.
var throwMeAnError = function() { //throw new Error(); }; describe("Different Methods of Expect Block",function() { var exp = 25; it("Hey this will throw an Error ", function() { expect(throwMeAnError). toThrow(); }); });
spec_dir : specifies where Jasmine looks for test files.
jasmine.getEnv().currentSpec.description
It's not pretty (introduces a global variable) but you can do it with a custom reporter:
// current-spec-reporter.js global.currentSpec = null; class CurrentSpecReporter { specStarted(spec) { global.currentSpec = spec; } specDone() { global.currentSpec = null; } } module.exports = CurrentSpecReporter;
Add it to jasmine when you add your other reporters...
const CurrentSpecReporter = require('./current-spec-reporter.js'); // ... jasmine.getEnv().addReporter(new CurrentSpecReporter());
Then extract the test name during your test/setup as needed...
it('Should have an accessible description', () => { expect(global.currentSpec.description).toBe('Should have an accessible description'); }
At last, see a sample of currentSpec
structure:
{ "id":"spec15", "description":"test1", "fullName":"My test category test1", "failedExpectations":[], "passedExpectations":[], "deprecationWarnings":[], "pendingReason":"", "duration":null, "properties":null }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With