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

Support Mocha #104

Open
lolmaus opened this issue Jul 3, 2019 · 1 comment
Open

Support Mocha #104

lolmaus opened this issue Jul 3, 2019 · 1 comment
Labels

Comments

@lolmaus
Copy link

lolmaus commented Jul 3, 2019

Hi!

I've been able to run ember-cli-fastboot-testing with ember-mocha.

Unfortunately, yarn link breaks the app for me, so I wasn't able to make a PR quickly. Also, I used a weird hack. Hopefully, you'll be able to find a better solution.

When porting the test file to Mocha, I ran into two issues.

  1. ember-mocha requires running a setup helper. It has a few to offer, and every one of them runs setupContext and teardownContext. Since running those twice crashes the test pipeline, ember-cli-fastboot-testing can't be used, so I had to make my own without setupContext and teardownContext.

  2. My tests started passing, but the test pipeline crashed on teardown after all tests. The error was from inside Glimmer, where it was doing something like parent.remove(child) and crashing with "this child does not belong to this parent".

    I was able to identify the parent as the testing container. The child turned out to be the application div inside the container, that happens to be there before visit from ember-cli-fastboot-testing is called.

    I managed to fix the issue with a weird workaround. In the before hook, I remove the content of the test container and stash it. In the after hook, I put it back! It feels like an ugly hack 🙈, but it did unblock ember-cli-fastboot-testing for me.

So here's my setup helper that enables ember-cli-fastboot-testing to be used with ember-mocha:

import { mockServer } from 'ember-cli-fastboot-testing/test-support';
import getRootElement from '@ember/test-helpers/dom/get-root-element';


export function setupMockServer(hooks) {
  let children;

  hooks.beforeEach(async function() {
    await mockServer.cleanUp();
    children = removeChildrenFromParent();
  });

  hooks.afterEach(async function() {
    await mockServer.cleanUp();
    removeChildrenFromParent();
    restoreChildrenToParent(children);
  });
}


function removeChildrenFromParent(parent = getRootElement()) {
  const children = [];

  while (parent.firstChild) {
    children.push(parent.firstChild);
    parent.removeChild(parent.firstChild);
  }

  return children;
}


function restoreChildrenToParent(children, parent = getRootElement()) {
  children.forEach(child => {
    parent.appendChild(child);
  })
}
@ryanto ryanto added the bug label Jul 3, 2019
@ryanto
Copy link
Member

ryanto commented Jul 3, 2019

Thanks for the detailed bug report! I'm hoping we can get this fixed an ember-cli-fastboot-testing.

Could you share your mocha tests that are causing the crash? It sounds like having two mocha tests is enough to crash the server?

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

2 participants