zaqqaz/jest-allure

ReferenceError: reporter is not defined

jayanathkarunarathna opened this issue ยท 8 comments

When I execute my test I get the below error
ReferenceError: reporter is not defined

I am new to js and couldn't figure out the solution in my own.
My package.json is as follows

{
"name": "voiceiqqawebjs",
"version": "1.0.0",
"description": "Gui Automation Framework for Voice IQ",
"main": "index.js",
"author": "VoiceIQ",
"license": "MIT",
"dependencies": {
"chromedriver": "^2.46.0",
"geckodriver": "^1.16.0",
"selenium-webdriver": "^4.0.0-alpha.1"
},
"devDependencies": {
"allure-commandline": "^2.9.0",
"jest": "^24.3.0",
"jest-allure": "^0.1.1",
"jest-cli": "^24.3.0"
},
"jest": {
"verbose": true,
"reporters": [
"default",
"jest-allure"
]
},
"scripts": {
"loginTest": "jest tests/Login.test.js",
"settingsTest": "jest tests/Settings.test.js"
}
}

I am seeing the same thing. I cut and paste the code from the examples directory into a blank project and it will not run for the same reason (ReferenceError: reporter is not defined);

@avukich ,can you show code. i think that in youre case will not execute setup script that add to testEnviroment instance of allure-jest reporter.
Also try to add something like setupFilesAfterEnv: ["jest-allure/dist/setup"] and try again

My code is as follows:

jest.config.js:

module.exports = {
    globalSetup: './setup.js',
    globalTeardown: './teardown.js',
    testEnvironment: './puppeteer_environment.js',
    reporters: ["default", "jest-allure"]
};

package.json:

{
  "name": "jestalluretesting",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "allure-commandline": "^2.9.0",
    "chalk": "^2.4.2",
    "jest": "^24.7.1",
    "jest-allure": "^0.1.1",
    "jest-environment-node": "^24.7.1",
    "mkdirp": "^0.5.1",
    "prettier": "^1.17.0",
    "puppeteer": "^1.14.0",
    "rimraf": "^2.6.3",
    "selenium-webdriver": "^4.0.0-alpha.1"
  }
}

puppeteer_environment.js:

const chalk = require('chalk');
const NodeEnvironment = require('jest-environment-node');
const puppeteer = require('puppeteer');
const fs = require('fs');
const os = require('os');
const path = require('path');

const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');

class PuppeteerEnvironment extends NodeEnvironment {
    constructor(config) {
        super(config);
    }

    async setup() {
        console.log(chalk.yellow('Setup Test Environment.'));
        await super.setup();
        const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8');
        if (!wsEndpoint) {
            throw new Error('wsEndpoint not found');
        }
        this.global.__BROWSER__ = await puppeteer.connect({
            browserWSEndpoint: wsEndpoint,
        });
    }

    async teardown() {
        console.log(chalk.yellow('Teardown Test Environment.'));
        await super.teardown();
    }

    runScript(script) {
        return super.runScript(script);
    }
}

module.exports = PuppeteerEnvironment;

setup.js:

const chalk = require('chalk');
const puppeteer = require('puppeteer');
const fs = require('fs');
const mkdirp = require('mkdirp');
const os = require('os');
const path = require('path');

const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');

module.exports = async function() {
    console.log(chalk.green('Setup Puppeteer'));
    const browser = await puppeteer.launch({});
    global.__BROWSER__ = browser;
    mkdirp.sync(DIR);
    fs.writeFileSync(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint())
};

teardown.js:

const chalk = require('chalk');
const puppeteer = require('puppeteer');
const rimraf = require('rimraf');
const os = require('os');
const path = require('path');

const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');

module.exports = async function() {
    console.log(chalk.green('Teardown Puppeteer'));
    await global.__BROWSER__.close();
    rimraf.sync(DIR);
};

spec/jest-allure.spec.js:

const { Severity } = require("jest-allure/dist/Reporter");

const timeout = 5000;

describe(
    '/ (Home Page)',
    () => {
        let page;
        let reporter;

        beforeAll(async () => {
            reporter = global.reporter;
            page = await global.__BROWSER__.newPage();
            await page.goto('https://google.com');
        }, timeout);

        afterAll(async () => {
            await page.close();
        });

        it('should load without error', async () => {
            reporter
                .description("Home Page test suite")
                .story("GOOGL-01")
                .severity(Severity.Critical);

            const text = await page.evaluate(() => document.body.textContent);

            reporter.startStep("Check that home page contain google");
            expect(text).toContain('google');
            reporter.endStep();

            reporter.startStep("Make a screenshot");
            const screenshot = await page.screenshot();
            reporter.addAttachment("Home Page", screenshot, "image/png");
            reporter.endStep();
        })
    },
    timeout
);

If I comment out the lines in the spec with reference to the reporter it runs, but doesn't create a report obviously, but when I leave the lines in it spits out the following:

$ npm test

> jestalluretesting@1.0.0 test /Users/nsd295/projects/JestAllureTesting
> jest

Determining test suites to run...Setup Puppeteer
Setup Test Environment.
 FAIL  spec/jest-alure.spec.js
  / (Home Page)
    โœ• should load without error (3ms)

  โ— / (Home Page) โ€บ should load without error

    TypeError: Cannot read property 'description' of undefined

      21 |         it('should load without error', async () => {
      22 |             reporter
    > 23 |                 .description("Home Page test suite")
         |                  ^
      24 |                 .story("GOOGL-01")
      25 |                 .severity(Severity.Critical);
      26 | 

      at Object.description (spec/jest-alure.spec.js:23:18)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        2.915s, estimated 3s
Ran all test suites.
Teardown Puppeteer
Teardown Test Environment.
npm ERR! Test failed.  See above for more details.

Adding setupFilesAfterEnv: ["jest-allure/dist/setup"] to my jest.config.js worked!

Thank You!

Yep, it's related to https://github.com/zaqqaz/jest-allure#warning but I need to update the doc for the newest version of jest.

Add setupFilesAfterEnv: ["jest-allure/dist/setup"] to jest.config.js it's correct solution.

Thank you, guys!

@zaqqaz it may be caused by changes in detox, I mean using new construction and new jest-circus runner. detoxCircus.getEnv().addEventsListener(assignReporter);

I have the same issue , when I add "testRunner": "jest-circus/runner", to my jest section in package.json then the tests stop working and I get the jasmine not defined error. Any ideas how to fix it? I have setupFilesAfterEnv: ["jest-allure/dist/setup"] and it is working fine when I remove the "testRunner": "jest-circus/runner" line.