Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »


1.4.3 • Public • Published

Gitbook integration tests framework

Build Status Coverage Status npm version Dependencies Status DevDependencies Status

No more mocking of gitbook build! Verify your gitbook-plugin against a real, up-to-date version of gitbook. This integration framework creates a temporary book, attaches your local gitbook plugin, runs gitbook build and returns the parsed pages content.

All the book resources are generated and executed in a temporary directory (exact location depends on your operating system). Resources are cleaned up upon test phase.


var tester = require('gitbook-tester');
  .withContent('This text is {% em %}highlighted!{% endem %}')
  .withBookJson({"plugins": ["emphasize"]})
  .then(function(result) {
    // do something with results!

Expected output is then:

<p>This text is <span class="pg-emphasize pg-emphasize-yellow" style="">highlighted !</span></p>

Only the <section> content of generated pages is currently returned. Do you need to test navigation, the header of page, etc.? Let me know or send me a pull request.

Gitbook-tester package provides a single entry point:


On the builder, the following methods can be called:


Put some Markdown content into the generated book's (initial/intro page).

.withPage(pageName, pageContent[, level])

Add another book page. For example:

  .withPage('second', 'Second page content')

There is no need for specifying extensions, .md will be automatically added. The rendered page can be accessed later in tests. For example:

it('should add second book page', function(testDone) {
    .withContent('First page content')
    .withPage('second', 'Second page content')
    .then(function(result) {
      expect(result.get('second.html').content).toEqual('<p>Second page content</p>');

Level: how nested should be this page, optional parameter. 0 for top level page, 1 for second, 2 for third...


Put your own book.json content as a JS object. May contain plugins, the plugin configuration or anything valid as described in the official documentation. Can also be omitted.


Attach currently tested or developed plugin to the generated gitbook. All locally attached plugins will be automatically added to book.json in the plugins section.

Should be called using the following format:


If you run your tests from the dir spec of your plugin, you should provide the path to the root of your plugin module. For example:

.withLocalPlugin(require('path').join(__dirname, '..'))

.withFile(path, content)

Allows you to create a file inside the book directory. Just provide the path for the file and string content. For example:

.withFile('includes/', 'included from an external file!')

Then you can use the file however you would like in your plugin or simply include its content in a page. For example:

'This text is {% include "./includes/" %}'


Start a build of the book. Generates all the book resources, installs required plugins, attaches the provided local modules. Returns promise.

Working with results

.then(function(result) {
  var index = result.get('index.html');

should output JavaScript object like

{ path: 'index.html',
  '$': [cheerio representation of the page]
  content: '<h1 id="test-me">test me</h1>' }

Force a specific gitbook version

You can test your plugin against a specific gitbook version by providing an ENV variable like GITBOOK_VERSION=2.6.7. This could be used, for example, in Travis-CI build matrix.


If you wish to see detailed output of the build and the gitbook logs, provide the ENV variable DEBUG=true.

Complete test example

How to write a simple test, using node-jasmine.

var tester = require('gitbook-tester');
// set timeout of jasmine async test. Default is 5000ms. That can
// be too low for a complete test (install, build, expects)
jasmine.getEnv().defaultTimeoutInterval = 20000;
describe("my first gitbook integration test", function() {
  it('should create book and parse content', function(testDone) {
    .withContent('#test me \n\n![preview](preview.jpg)')
    .then(function(result) {
      expect(result[0].content).toEqual('<h1 id="test-me">test me</h1>\n<p><img src="preview.jpg" alt="preview"></p>');


This project is available under Apache-2.0 license.


npm i gitbook-tester

Downloadsweekly downloads









last publish


  • avatar
Report a vulnerability