Git Product home page Git Product logo

hexo-test-utils's Introduction

hexo-test-utils

Build Status

Hexo is quite easy to write tests for, but the needed boilerplate code can be a barrier. This packages provides a set of utils to simplify testing Hexo plugins.

Offered features:

  • Sandbox environment
  • high level functions for Hexo internals
  • ECMAScript5 compatibility

Installation

npm install hexo-test-utils

Sandbox

The main feature is a Sandbox. It allows to create an isolated environment of Hexo blog. Combined with a fixture, it's easy to prepare all need files, process the blog and assert the output.

To create a Sandbox instance, first you need to create a factory with createSandbox:

const {createSandbox} = require('hexo-test-utils')
const Hexo = require('hexo')
const path = require('path')

const sandbox = createSandbox(Hexo, {
  fixture_folder: path.join(__dirname, '..', 'fixtures'),
  plugins: [
    path.join(__dirname, '..', 'src')
  ]
})

Now, the sandbox is a function which returns a new context - an instance of Hexo blog which is ready for processing.

Using fixture folder

If fixture_folder is set, the factory function accepts one parameter, name of the fixture folder where live all fixture files.

To prepare a fixture with one post, create folder ./fixtures/one-post/source/_posts/ and put the markdown file there.

const context = sandbox('one-post')

At this moment, the Hexo blog is created, but no files are loaded. To start the loading process, call process method.

const {process} = require('hexo-test-utils/core')

process(context).then(() => {
  // Hexo processed the ./fixtures/one-post/ folder as a blog
})

Now you can start check the behavior of your plugin.

Note: no files were actually saved on the hard drive, all the process happened in memory.

Empty Sandbox

You can skip fixture_folder if you don't need any files to exists, for example when testing a view helper in isolation. The factory function will have no parameters then and the Sandbox will be an empty Hexo project - no posts, no theme, no assets.

You can still use Hexo API to provide fixtures programmatically.

Documentation

Read API documentation here

hexo-test-utils's People

Contributors

dependabot[bot] avatar ertrzyiks avatar segayuu avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hexo-test-utils's Issues

Add onInit option to sandboxFactoryFn

The current signature os the sandboxFactoryFn is just one String param with a fixture name

const sandbox = createSandbox()
sandbox('fixture_name')

support passing options and the new option onInit

const sandbox = createSandbox()
sandbox({ 
  fixture_name: 'fixture_name',
  onInit: [load, process]
})

It should be an array of function which should be reduced with the context.

need more example

need more documentation like example

hexo.extend.filter.register("after_render:html", sitemapPost);
  hexo.call("render", "test render body", { title: "test render" });
  hexo.call("generate", { generate: true });

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.