Jasmine2: get current spec name

前端 未结 6 694
天涯浪人
天涯浪人 2020-12-15 19:34

In Jasmine 1.3, we had this option to the get current spec and suite names:

describe(\"name for describe\", function () {
    it(\"name for it\", function ()         


        
相关标签:
6条回答
  • 2020-12-15 20:18

    I add a new jasmine reporter, then get the spec name without define N variable on each spec. Hope can help, thanks.

    var reporterCurrentSpec = {
         specStarted: function(result) {
             this.name = result.fullName;
         }
     };
    jasmine.getEnv().addReporter(reporterCurrentSpec);
    
    0 讨论(0)
  • 2020-12-15 20:26

    The reason this no longer works is because this is not the test. You can introduce a subtle change to your declarations however that fix it. Instead of just doing:

    it("name for it", function() {});
    

    Define the it as a variable:

    var spec = it("name for it", function() {
       console.log(spec.description); // prints "name for it"
    });
    

    This requires no plug-ins and works with standard Jasmine.

    0 讨论(0)
  • 2020-12-15 20:28
        var currentSpecName = describe('Test1', function() {
            var currentStepName = it("Step1", function(){
               console.log(currentStepName.description); // Prints It Name
               console.log(currentSpecName.getFullName()); //Prints Describe Name
            });
        });
    
    0 讨论(0)
  • 2020-12-15 20:32

    As far as Jasmine 2 is concerned currentSpec is discontinued on purpose. However there is a custom plugin/library developed that is based on jasmine reporter plugin which you can use. Here's the Link. Hope it helps with your requirement.

    Its very simple to use, install the package with npm command -

    npm install -g jasmine-test-container-support

    Get the test container support by writing below lines before your describe or test suite -

    var JasmineTestContainerSupport = window.JasmineTestContainerSupport || require('jasmine-test-container-support');
    
    JasmineTestContainerSupport.extend(jasmine);
    

    Later use the test container in your spec's to get its description -

    var specDesc = jasmine.getEnv().getTestContainer();
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-15 20:37

    This is probably a bit late but you can get the suite name outside the spec. Please try the following code:

      describe("name for describe", function () {
        console.log(this.getFullName()); // would print "name for describe"
        it("name for it", function () {
          //Your test spec
        });
      });
    
    0 讨论(0)
  • 2020-12-15 20:39

    This worked for me in jasmine 3.5+

    I know this is a relatively old question but found something which worked for me

    describe('Desc1',() => {
       
        afterEach(() => {
          const myReporter = {
            specDone: (result) => {
              console.log('Spec FullName: ' + result.fullName);
              console.log('Spec Result: ' + result.status);
            }
          };
          jasmine.getEnv().addReporter(myReporter);
        });
    })
    

    Credit for the solution : https://groups.google.com/g/jasmine-js/c/qqOk6Nh7m4c/m/Nyovy2EjAgAJ

    0 讨论(0)
提交回复
热议问题