New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@web/test-runner-mocha

Package Overview
Dependencies
Maintainers
6
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@web/test-runner-mocha

Mocha test framework for Web Test Runner

0.3.0
Source
npm
Version published
Weekly downloads
79K
0.84%
Maintainers
6
Weekly downloads
 
Created
Source

title: Test Runner Mocha eleventyNavigation: key: Mocha parent: Test Runner order: 100

Test Runner Mocha

Test framework implementation of Mocha for Web Test Runner

See @web/test-runner for a default implementation and CLI for the test runner.

Writing JS tests

Mocha relies on global variables, in any JS test file describe and it are available globally and can be used directly:

describe('my test', () => {
  it('foo is bar', () => {
    if ('foo' !== 'bar') {
      throw new Error('foo does not equal bar');
    }
  });
});

Writing HTML tests

If you're writing tests as HTML, you can import this library to run tests with mocha.

Inline tests

You can write tests inline in the HTML page

View example
<html>
  <body>
    <script type="module">
      import { mocha, sessionFinished, sessionFailed } from '@web/test-runner-mocha';

      try {
        // setup mocha
        mocha.setup({ ui: 'bdd' });

        // write your actual tests
        describe('HTML tests', () => {
          it('works', () => {
            expect('foo').to.equal('foo');
          });
        });

        // run the tests, and notify the test runner after finishing
        mocha.run(() => {
          sessionFinished();
        });
      } catch (error) {
        console.error(error);
        // notify the test runner about errors
        sessionFailed(error);
      }
    </script>
  </body>
</html>

Loading test files

You can also use dynamic imports to load tests written in JS from the HTML page.

View example
<html>
  <body>
    <script type="module">
      import { mocha, sessionFinished } from '@web/test-runner-mocha';

      async function run() {
        try {
          // setup mocha
          mocha.setup({ ui: 'bdd' });

          // import your tests
          await import('./my-test.js');

          // run the tests, and notify the test runner after finishing
          mocha.run(() => {
            sessionFinished();
          });
        } catch (error) {
          console.error(error);
          // notify the test runner about errors
          sessionFailed(error);
        }
      }

      run();
    </script>
  </body>
</html>

Loading polyfills or libraries

From the HTML page you can load polyfills or libraries to be set up before your tests are run. This is great for testing your code in different environments.

Load polyfills or libraries
<html>
  <body>
    <script src="./some-polyfill.js"></script>

    <script type="module">
      import { mocha, sessionFinished } from '@web/test-runner-mocha';

      // see examples above
    </script>
  </body>
</html>

Configuring browser environment

You can use elements in the <head> to configure different browser environment, such as base path or CSP rules.

Load polyfills or libraries
<html>
  <head>
    <meta
      http-equiv="Content-Security-Policy"
      content="default-src 'self'; img-src https://*; child-src 'none';"
    />
  </head>
  <body>
    <script type="module">
      import { mocha, sessionFinished } from '@web/test-runner-mocha';

      // see examples above
    </script>
  </body>
</html>

Configuring mocha options

You can configure mocha options using the testFramework.config option:

module.exports = {
  testFramework: {
    config: {
      ui: 'bdd',
      timeout: '2000',
    },
  },
};

The config entry accepts any of the official mocha browser options.

Libraries

@open-wc/testing is a general purpose library, including assertions via chai, HTML test fixtures, a11y tests and test helpers.

It is an opinionated implementation which brings together multiple libraries. You could also use the individual libraries together:

For stubbing and mocking, we recommend sinon which ships an es module variant out of the box:

import { stub, useFakeTimers } from 'sinon';

Keywords

web

FAQs

Package last updated on 11 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts