npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wct-mocha

v1.1.0

Published

Client-side library for testing web-components with Mocha.

Downloads

2,341

Readme

NPM version Build Status Gitter

wct-mocha makes testing your web components a breeze!

You get a streamlined browser-based testing environment, designed to work with web-component-tester or on its own.

Getting Started

Install mocha and wct-mocha as devDependencies

$ npm install --save-dev mocha
$ npm install --save-dev wct-mocha

Install web-component-tester or polymer-cli globally

$ npm install --global web-component-tester
$ npm install --global polymer-cli

Run wct or polymer test

$ wct --npm
$ polymer test --npm

.html Suites

Your test suites can be .html documents. For example, test/awesomest-tests.html:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
  <script src="../node_modules/mocha/mocha.js"></script>
  <script src="../node_modules/chai/chai.js"></script>
  <script src="../node_modules/@polymer/test-fixture/test-fixture.js"></script>
  <script src="../node_modules/wct-mocha/wct-mocha.js"></script>
</head>
<body>
  <awesome-element id="fixture"></awesome-element>
  <script type="module">
    import {AwesomeElement} from '../src/awesome-element.js';
    suite('<awesome-element>', () => {
      test('is awesomest', () => {
        const element = document.getElementById('fixture');
        chai.assert.instanceOf(AwesomeElement);
        chai.assert.isTrue(element.awesomest);
      });
    });
  </script>
</body>
</html>

.js Suites

Alternatively, you can write tests in separate .js sources. For example, test/awesome-tests.js:

suite('AwesomeLib', () => {
  test('is awesome', () => {
    assert.isTrue(AwesomeLib.awesome);
  });
});

Special Features

Nested Suites

To help support this case, you can also directly define an index that will load any desired tests:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
    <script src="../node_modules/mocha/mocha.js"></script>
    <script src="../node_modules/wct-mocha/wct-mocha.js"></script>
  </head>
  <body>
    <script>
      WCT.loadSuites([
        'awesome-tests.js',
        'awesomest-tests.html',
      ]);
    </script>
  </body>
</html>

When you use wct or polymer test on the command line, it is generating an index like this for you based on the suites you ask it to load.

Web Components Support

By default, WCT will defer tests until the WebComponentsReady event has been emitted by @webcomponents/webcomponents-loader.js or one of its polyfill bundles. This saves you from having to wait for elements to upgrade and all that yourself.

If you need to test something that occurs before that event, the testImmediate helper can be used.

Alternately, if you are not using the @webcomponents/webcomponentjs polyfills or loader or otherwise simply want tests to run as soon as possible, you can disable this delay by setting WCT.waitForFrameworks = false (though, they are still async due to Mocha).

Mocha

WCT supports Mocha's TDD (suite/test/etc) and BDD (describe/it/etc) interfaces, and will call mocha.setup/mocha.run for you. Just write your tests, and you're set.

Custom Environments

If you would like to have WCT load libraries on your behalf, you can define a list of scripts to load. There are two ways of doing this:

Inside your test code (before wct-mocha/wct-mocha.js is loaded):

<script>
  WCT = {
    environmentScripts: [
      '../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js',
      '../node_modules/mocha/mocha.js',
      '../node_modules/chai/chai.js',
      '../node_modules/@polymer/test-fixture/test-fixture.js'
      // Include anything else that you like!
    ]
  };

Alternatively, web-component-tester can pass the entire value of the WCT block object here straight from the clientOptions key in wct.conf.json.

A reference of the default configuration can be found at browser/config.ts.

Use any web server

If you prefer not to use WCT's command line tool, you can also run WCT tests directly in a browser via a web server of your choosing and still make use of all of its features.

Make sure that the wct-mocha/wct-mocha.js script is accessible by your web server, and have your tests load it after loading mocha/mocha.js.