Xotabu4/jasmine-protractor-matchers

ReferenceError: beforeEach is not defined

Danieler opened this issue · 5 comments

in my protractor.conf.js i get the following error
ReferenceError: beforeEach is not defined

 onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
    var protractorMatchers = require('jasmine-protractor-matchers');
    beforeEach(function() {
      jasmine.addMatchers(protractorMatchers);
      //Some code that needs to be executed before every test.
    });
    // jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }

@Danieler Hi! This looks like your onPrepare function is called at wrong time. Can you share full protractor config file and way how you start tests?

Could be beause i have custom cucumber as framework instead jasmine....?

`exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    'e2e/**/*.feature'
  ],
  multiCapabilities: [
    {
      browserName: 'chrome',
      chromeOptions: {
        args: ['disable-infobars']
      },
      shardTestFiles: true,
      maxInstances: 1
    }
  ],
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'custom',
  frameworkPath: require.resolve('protractor-cucumber-framework'),
  cucumberOpts: {
    compiler: "ts:ts-node/register",
    format: 'pretty',
    require: ['e2e/**/*.steps.ts'],
    tags: ''
  },
  onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
    var protractorMatchers = require('jasmine-protractor-matchers');
    beforeEach(function() {
      jasmine.addMatchers(protractorMatchers);
      //Some code that needs to be executed before every test.
    });
    // jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};`

@Danieler I see. Thats just a typo. Your onPrepare function must be declared as object key, with function value, not calling onPrepare.

Protractor will call passed onPrepare() function when browser will be ready, and test framework is ready. Then your beforeEach will be available.

See the difference:

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    'e2e/**/*.feature'
  ],
  multiCapabilities: [
    {
      browserName: 'chrome',
      chromeOptions: {
        args: ['disable-infobars']
      },
      shardTestFiles: true,
      maxInstances: 1
    }
  ],
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'custom',
  frameworkPath: require.resolve('protractor-cucumber-framework'),
  cucumberOpts: {
    compiler: "ts:ts-node/register",
    format: 'pretty',
    require: ['e2e/**/*.steps.ts'],
    tags: ''
  },
  onPrepare: function () {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
    var protractorMatchers = require('jasmine-protractor-matchers');
    beforeEach(function() {
      jasmine.addMatchers(protractorMatchers);
      //Some code that needs to be executed before every test.
    });
    // jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};

it continues given me the same error with that sintax...
i think that could be for the framework property

@Danieler Sorry i missed -
Could be beause i have custom cucumber as framework instead jasmine....?

Thats the reason. These matchers are only applicable for Jasmine, and other frameworks are not supported. Sorry for late response.