question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unknown mocha options cause tests to hang

See original GitHub issue

I’m trying to run some code before my tests using the mocha require option, which I thought maybe I could do in the config file as mentioned by https://modern-web.dev/docs/test-runner/test-frameworks/mocha/#configuring-mocha-options.

I’ve found that when I add a require option, my tests just hang and do not execute, eventually timing out. Similarly, if I add a foo option, I get the same thing, whereas another known option like ui: 'tdd' works just fine.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
IanVScommented, May 21, 2021

@EvgeniyLoginov that’s cool. I added a script after my script with src="${testFramework}", and at that point it’s possible to call beforeAll from mocha. I guess either way will work.

0reactions
IanVScommented, May 26, 2021

Oh interesting, I see now why adding rootHooks to the config file doesn’t work. The values there are stringified and added to the window (https://github.com/modernweb-dev/web/blob/6c5893cc79c91e82f9aec35d3011c6e33ce878cc/packages/test-runner-core/src/server/plugins/serveTestRunnerHtmlPlugin.ts#L93), so any functions are lost. That’s unfortunate, since I like the idea of specifying the rootHooks in the config file much better than either of our other approaches.

In the meantime, I’ve switched to using your approach, since it seems to be more reliable with safari. I now have this in my web-test-runner.config file:

module.exports = {
  testRunnerHtml: (testFramework) => `<html>
    <body>
    <script type="module" src="/test-setup/globals.js"></script>
    <script type="module" src="/test-setup/mocha-hooks.js"></script>
    <script id='uit' type="module" src="${testFramework}"></script>
    </body>
  </html>`,
  testFramework: {
    config: {
      rootHooks: {
        // We can't actually add them here, since WTR serializes this config to a string.
        // But we can create the structure and then modify it in our mocha-hooks.js file
      },
    },
  },
};

And then in mocha-hooks.js:

// HACK:  WTR serializes the config to a string, so we can't define our hooks in
// web-test-runner.config.cjs.  Instead, we'll modify it from the global window here.
(window as any).__WTR_CONFIG__.testFrameworkConfig.rootHooks = {
  afterEach() {
    cleanup();
  },
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

mocha 4 doesn't exit unlike mocha 3 · Issue #3044 - GitHub
I have been running specific set of tests for few years now and have been always upgrading to the latest mocha and everything...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, ... Use .only until you find the test that...
Read more >
mochajs/mocha - Gitter
Hello. I am having an issue running tests in a suite based on a variable condition. For example, I have a suite that...
Read more >
Mocha tests in node.js hanging or missing - Stack Overflow
The problem I am having is: At the current stage of the program when I run "npm test" I get the tests hanging...
Read more >
Unit testing — Vuetify
Because of this, Vue has its own testing utility called vue-test-utils . ... instance and pass an instance to the mount functions options....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found