Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to retrieve current test name in jest-circus? #137

Closed
aesyondu opened this issue Jun 8, 2020 · 7 comments
Closed

[Question] How to retrieve current test name in jest-circus? #137

aesyondu opened this issue Jun 8, 2020 · 7 comments

Comments

@aesyondu
Copy link
Contributor

aesyondu commented Jun 8, 2020

I mentioned here how I retrieve the current test name when using the jasmine runner.

Now I've updated jest-playwright-preset to 0.2.3 and noted that I'm getting an error:

ReferenceError: jasmine is not defined

I noticed that in the 0.2.0 release the jest-circus became the default runner, which is probably the cause of the error. I'm currently searching for it but I don't suppose anyone knows how to retrieve the current test name in the jest-circus runner?

For reference, this is how I currently do it:

// jest.setup.js
// save the current test object to a global variable
jasmine.getEnv().addReporter({
  specStarted: result => jasmine.currentTest = result,
  specDone: result => jasmine.currentTest = result,
});

// my-test.spec.js
// use to check if the current test has errors, if it has, screenshot it
// code before ...
  afterEach(async () => {
    if (jasmine.currentTest.failedExpectations.length > 0) {
      await saveScreenshot(page, fileName, "error");
    }
  });
// code after ...

// helpers.js
// used for logging somewhere the current test name
// code before...
jasmine.currentTest.fullName
// code after...

So I'm actually using it to do two things:

  1. check if the current test has any failures
  2. retrieve the current test name
@aesyondu
Copy link
Contributor Author

aesyondu commented Jun 8, 2020

Oops I think this is explained in the readme. My bad.

@aesyondu aesyondu closed this as completed Jun 8, 2020
@hananmalka
Copy link

@aesyondu
Can you share where it's actually described?
I'm trying to use jasmine and keep getting "jasmine is not defined"

@aesyondu
Copy link
Contributor Author

aesyondu commented Jan 17, 2021

@aesyondu
Can you share where it's actually described?
I'm trying to use jasmine and keep getting "jasmine is not defined"

Hi @hananmalka, I was referring to the readme of jest-circus

It is explained there how to bind to events:

import {Event, State} from 'jest-circus';
import NodeEnvironment from 'jest-environment-node';

class MyCustomEnvironment extends NodeEnvironment {
  //...

  async handleTestEvent(event: Event, state: State) {
    if (event.name === 'test_start') {
      // ...
    }
  }
}

Therefore, in order to get the current test name, I used something like this:

// CustomEnvironment.js
const fs = require("fs");
const PlaywrightEnvironment = require("jest-playwright-preset/lib/PlaywrightEnvironment")
  .default;

class CustomEnvironment extends PlaywrightEnvironment {
  async setup() {
    await super.setup();
    // Your setup
  }

  async teardown() {
    // Your teardown
    await super.teardown();
  }

  async handleTestEvent(event) {
    if (event.name === "test_start") {
      let testNames = [];
      let currentTest = event.test;
      while (currentTest) {
        testNames.push(currentTest.name);
        currentTest = currentTest.parent;
      }
    }

    // etc...
  }
}

module.exports = CustomEnvironment;

@aesyondu
Copy link
Contributor Author

aesyondu commented Jan 17, 2021

Oops I think I referenced the jest-playwright readme itself, not the jest-circus one:

https://github.com/playwright-community/jest-playwright#usage-with-custom-testenvironment

EDIT: Either way, the general idea is the same.

@hananmalka
Copy link

hananmalka commented Jan 17, 2021 via email

@aesyondu
Copy link
Contributor Author

Ah I see, hmm, I'm not really sure. Have you tried this?

zaqqaz/jest-allure#8 (comment)

@kevinoliveira Hello, I have the same issue as you had, I am curious if you solved it?

Hi, I had the same situation and found no one answer wich worked for me.
Now I using another way. I added this row for jest.config.js
testRunner : 'jasmine2'

it's resolve issue with
ReferenceError: jasmine is not defined
at registerAllureReporter

and report generate successed

Sorry I'm not too familiar with using jasmine with jest-playwright.

@hananmalka
Copy link

Yes...tried this one. It's not working :(
jest-playwright uses environment jest-circus under the hood and seems like it can't be override.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants