Giter VIP home page Giter VIP logo

jspm-test's Introduction

jspm test

A browser test runner for jspm projects. Not yet considered production ready.

The problem

I spend a lot of time working on React and jspm projects, but the testing story isn't perfect with these. If your app is managed through jspm, you'll likely have to reinstall all these dependencies again in Node land to be able to use a testing framework with them. Often this is fine, but sometimes you just want to easily run tests in the browser and reuse your initial dependencies. This is what jspm-test does.

How

Installing

Install jspm-test from GitHub:

jspm install jspm-test=github:jackfranklin/[email protected]

### Creating the test runner

Then, create test.html in the root of your app:

<!DOCTYPE html>
<html>
  <head>
    <script src="jspm_packages/system.js"></script>
    <script src="config.js"></script>
    <script>System.import('test-runner');</script>
  </head>
  <body>
  </body>
</html>

Then create test-runner.js:

import {
  runTestsOnFiles
} from 'jspm-test/runner';

const testFiles = [
  'test/my-test.js'
];

runTestsOnFiles(testFiles);

Simply import the runTestsOnFiles and pass it a list of test files that it should run.

### Writing a test

import { describe } from 'jspm-test/describe';

describe('Adding 2 + 2', (t) => {
  t.equal(2 + 2, 4);
});

The describe function is all you need to import. It passes in an object to the callback function that contains assertion methods on it. See below for the methods available to you, nested tests and more. The API is strongly influenced by tape, which is my go-to testing framework in Node land.

Running the tests

Run your application and visit /test.html that we created earlier. You should see a passing test!

Now, let's make that test fail:

import { describe } from 'jspm-test/describe';

describe('Adding 2 + 2', (t) => {
  t.equal(2 + 2, 3);
});

Refresh, and you'll see the test fail. Any errors are also logged to the console so you get clickable stack traces:

Available Assertions

t.deepEqual

Asserts that two objects are deeply equal:

describe('Foo', (t) => {
  t.deepEqual({ x: 1 }, { y: 1 }); // will fail
});

t.ok

Asserts that the given variable is truthy:

describe('Foo', (t) => {
  t.ok(true);
  t.ok({});
  t.ok(false); // fail
});

t.equal

Asserts that two variables are strictly equal (using ===).

describe('Foo', (t) => {
  t.equal(2, 2);
  t.equal(2, '2'); // fail
});

## Adding Custom Assertions

Rather than create yet another assertion library, jspm-test wraps some of Chai's assertions. By default only t.ok, t.deepEqual and t.equal are available but you can add others.

To do this, you need to import and call wrapAssertion. This function takes the name of the assertion and a function that will be called with the arguments. The function is expected to raise an error if it fails, and do nothing if it passes. For example, here's how t.equal is implemented in jspm-test:

wrapAssertion('equal', (x, y) => {
  expect(x).to.equal(y);
});

And here's how you might add your own custom assertion:

import { wrapAssertion } from 'jspm-test/describe';

wrapAssertion('isFour', (x) => {
  if (x !== 4) {
    throw new Error('Given value wasn\'t equal to 4');
  }
});

// then in a test

describe('Is 4', (t) => {
  t.isFour(5); // uh oh...
});

TODO / Coming Soon

  • Thorough documentation on other features of jspm-test, including async tests.
  • PhantomJS script / support for jspm run such that tests can run in a CI environment.
  • Support for jspm-hot-reloader, so tests runs are crazy quick.
  • Improve the CSS for the browser tests (any help more than welcome!)

jspm-test's People

Contributors

jackfranklin avatar

Stargazers

 avatar Beni Buess avatar Andrei Nitescu avatar Brad Pillow avatar

Watchers

 avatar  avatar Anthony Bowyer-Lowe avatar James Cloos avatar  avatar

jspm-test's Issues

Searching for a good way of testing with jspm

Hi Jack,

I have more or less the same problem that you had when you created this project, I am guessing if you found other ways to do these tests because it seems you haven't touched this project in 2 years....

before Systemjs/Jspm I was using a lot requirejs, for that time I remember a really nice test-framework testem , no idea how it is the status nowadays…

for now I have found
https://github.com/OrKoN/jspm-testem
https://medium.com/@capajj/why-it-is-foolish-to-use-karma-with-jspm-3af9ebd02af

I am trying to find more recent opinions about how to do test across different browsers smoothly with jspm,

Thanks in advance!

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.