Giter VIP home page Giter VIP logo

browser-test-echo's Introduction

browser-test-echo

For some web projects it is desirable to run unit tests in a browser instead of an emulated environment like jsdom. Karma is the established utility for this, but Karma bundles its own web server. Using a different web server is possible (see the obsolete karma-esm) but not easy.

Why would anyone want to use a specific web server? One reason is that some web servers designed for development (e.g. Web Dev Server) allow "buildless" workflows, i.e. bundling is not required until deployment. There are pros and cons to this but many of the current cons are because ES6 modules aren't universally supported, issues which should dwindle over time.

This package provides two scripts that connect a standalone Jasmine test in the browser and the test runner Jest. It should be adaptable to most node test runners that support ES6 modules, Puppeteer, and Jasmine (or Jasmine-like) test specs.

Quick Start

  • Use a recent version of nodejs (successfully tried v14.15.4).
  • Clone the project and yarn install (npm may also work but this project was developed with yarn).
  • Follow the instructions for enabling ES6 modules in Jest which may involve setting NODE_OPTIONS in your environment or command line.
  • yarn test

The sample test should show two passing tests and one intentionally failing test. On a dumb terminal the results should look like this:

Web Dev Server started...

  Root dir: /home/dev/browser-test-echo
  Local:    http://localhost:8001/
  Network:  http://172.17.0.2:8001/

FAIL test/sample.test.js
  sample test suite
    ✓ should work
    nested test suite
      ✕ should show failed tests (like this one) (13 ms)
      ✓ should also work

  ● sample test suite › nested test suite › should show failed tests (like this one)

    expect(received).toBeFalsy()

    Received: "Error: Expected [ 1, 2, 3 ] to be 'a unicorn'.
        at <Jasmine>

      at UserContext.<anonymous> (http:/localhost:8001/test/sample.test.html:34:27)
          at <Jasmine>"
      at forEach (src/BrowserTestRunner.js:117:38)
          at Array.forEach (<anonymous>)
      at Object.<anonymous> (src/BrowserTestRunner.js:115:45)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 2 passed, 3 total
Snapshots:   0 total
Time:        2.664 s, estimated 5 s
Ran all test suites.

Usage

How to run browser tests from Jest in your project:

  • Install this package as a dev dependency (yarn add -D browser-test-echo@rhashimoto/browser-test-echo), plus:

    • jest
    • jest-environment-node
    • puppeteer
    • @web/dev-server
  • Copy the test scripts - jest-setup.cjs, jest-teardown.cjs, and jest-environment.cjs - into your own project and reference them in your jest.config.js.

  • Use the sample files as models to create your own test.

    • sample.test.js - Jest test that passes the Puppeteer browser and Jasmine Standalone test URL to this package.
    • sample.test.html - Standalone browser test. You will need to alter the Jasmine Standalone script paths appropriately for your project.

If you want to debug a browser test interactively in a browser, note that you can simply bypass Jest and load the HTML file directly. Use yarn web-dev-server --node-resolve --preserve-symlinks to launch the server manually (add --watch to trigger a browser reload when dependent files change).

Caveats

ES6 module support is not complete or stable in Jest and Nodejs, though steadily improving.

Alternatives

Web Dev Server does have a companion Test Runner. It is released and actively developed though it is not as mature as other test runners.

Eventually import maps may standardize browser support for buildless workflows.

browser-test-echo's People

Contributors

rhashimoto avatar shoestringresearch avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.