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

Undefined error when using mocha's before #216

Closed
SaladFork opened this issue Jul 22, 2016 · 5 comments
Closed

Undefined error when using mocha's before #216

SaladFork opened this issue Jul 22, 2016 · 5 comments
Labels

Comments

@SaladFork
Copy link

beforeEach(function() {
  MyPageObject.visit(); // works fine
});

before(function() {
  MyPageObject.visit(); // visit is not defined
});

Error is thrown at visitable.js:95.

Using:

  • ember-cli-page-object@1.4.2
  • ember-cli-mocha@0.10.4
  • ember-mocha-adapter#0.3.1
  • mocha#2.5.3
  • chai#3.5.0
@san650 san650 added the bug label Jul 22, 2016
@san650
Copy link
Owner

san650 commented Jul 22, 2016

Thanks for reporting the error, I'll take a look at it.

I don't use mocha with ember a lot, so I'm not too familiar with what changes when calling before. What happens if you call visit('/foo') from the before hook? Does the ember test helper exists?

@SaladFork
Copy link
Author

Thanks for the quick reply! I'm embarrassed I didn't try the global helper myself, seems an obvious test now.

I'm seeing the same issue with trying to use the global visit helper. I suspect now this is not an issue with ember-cli-page-object.

Any recommendations as to the right project to report this issue?

@san650
Copy link
Owner

san650 commented Jul 22, 2016

@SaladFork I would say that you should open an issue in ember-mocha rather than opening it in ember-cli-mocha to ask if this is an expected behavior or not.

Reading about Mocha's hooks it kind of make sense that these helpers don't exist on the before hook. This is because you don't have an application instance yet when this hook is executed but you do have one when beforeEach runs.

Remember that when an application is instantiated, it registers all the test helpers in the global context (in the window object).

@SaladFork
Copy link
Author

SaladFork commented Jul 22, 2016

Thanks @san650! Unless I'm misunderstanding, a common pattern in Mocha is to nest describe/context blocks and use before/beforeEach in nested ones in which case the application would be initialized. For example:

describe('Some acceptance test', function() {
  let application;

  beforeEach(function() {
    application = startApp();
  });

  afterEach(function() {
    destroyApp(application);
  });

  context('The main page', function() {
    // No need for `beforeEach` here as we're not changing the page between the below tests,
    // but do want to have each `it` block test only a single thing.
    before(function() {
      MainPageObject.visit();
    });

    it('should have some value on the page', function() {
      // ...
    });

    it('should have another value on the page', function() {
      // ...
    });
  });
});

Thanks again!

@Turbo87
Copy link

Turbo87 commented Nov 17, 2016

@SaladFork Indeed I think you're misunderstanding. The beforeEach() block in your example will still be executed twice, once for each test. In fact the nested before() block will be executed before the outer beforeEach() block.

I've taken your example and put some console.log() calls inside those blocks and this is the output:

  Some acceptance test
    The main page
before
beforeEach
      ✓ should have some value on the page
beforeEach
      ✓ should have another value on the page

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

No branches or pull requests

3 participants