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

riot-jest-transformer

v6.1.0

Published

Jest transformer for testing riot tags

Downloads

812

Readme

Build Status Coverage Status

riot-jest-transformer

A Jest transformer for riot tags

This transformer helps you to use Jest testing tool for your Riot tags. With this transformer you can import your tags into your Jest tests.

Prerequisites

  • Nodejs >= 6.9
  • Installed Jest package (npm i --save-dev jest babel-jest)
  • Installed riot-jest-transformer npm package into your project: npm i --save-dev riot-jest-transformer
  • If you use Babel, set up .babelrc file correctly (for more see Jest docs). Don't forget setting presets for new javascript features.

Setting up Jest config file

riot-jest-transformer must be used in your Jest config file like this:

{
    "transform": {
        "^.+\\.jsx?$": "babel-jest",
        "^.+\\.tag$": "riot-jest-transformer"
    }
}

If you use Riot pre-processors, you can provide config options for riot-jest-transformer to register pre-processors befor compiling your tags for your tests. In this case you should use the following scheme:

{
    "transform": {
        "^.+\\.jsx?$": "babel-jest",
        "^.+\\.tag$": ["riot-jest-transformer", {
          registrations: [{
            type: "css" | "template" | "javascript",
            name: string,
            preprocessorModulePath: string
        }]
    }
}

preprocessorModulePath must be defined as a relative path (based on Jest rootDir configuration) to a module that exports your preprocessor function.

For example if you use scss, you can define a preprocessor function like this:

// riot-scss-preprocessor.js
const sass = require('node-sass');

module.exports = function riotScssPreprocessor(code, { options }) => {
    const { file } = options;
    console.log('Compile the sass code in', file);
    const { css } = sass.renderSync({
        data: code
    });
    return {
        code: css.toString(),
        map: null
    };
}

In the above case the jest config should be looked something like this:

// jest.config.js

module.exports = {
    transform: {
        "^.+\\.riot$": ["riot-jest-transformer", {
            registrations: [{
                type: 'css',
                name: 'scss',
                preprocessorModulePath: 'riot-scss-preprocessor'
            }]
        }],
        "^.+\\.jsx?$": "babel-jest"
    },
};

Usage

Just import your tag into the Jest test file. After that you can mount your tag to an html element. For example:

import * as riot from 'riot';
import hello from '../hello.tag'; // <hello><h1>{ opts.name }</h1></hello>

describe('hello', () => {
    beforeAll( () => {
        // create mounting point
        const elem = document.createElement('hello');

        elem.setAttribute('name', 'world');
        document.body.appendChild(elem)

        riot.register('hello', hello);
        riot.mount(elem, 'hello');
    });

    it('should mount the tag', () => {
        expect(document.querySelector('hello h1').textContent).toBe('world');
    });
});

Demo

You can play with importing and testing tags in the demo folder:

  • Clone project
  • Enter demo folder
  • Run npm i
  • Run npm test to run a simple jest test for an example Riot tag.

Development

Run tests with npm test or npm run test:watch.

The transformer is developed with tdd, so if you would like to contribute (you are really welcomed :), please write your tests for your new functionality, and send pull request to integrate your changes.