Skip to content

broccolijs/broccoli-fixture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Broccoli Fixture Helpers

Build Status

This package is useful for testing Broccoli plugins without interacting with the file system. It lets you turn fixturify objects into Broccoli nodes, and build Broccoli nodes back into fixturify objects.

Usage

Node: Fixture object to Broccoli node

Create a Broccoli source node whose contents are created by fixturify rather than coming from the file system:

var fixture = require('broccoli-fixture');

var inputFixture = {
  'foo.txt': 'foo.txt contents',
  'subdir': {
    'bar.txt': 'bar.txt contents'
  }
};

var node = new fixture.Node(inputFixture);

build: Broccoli node to fixture object

fixture.build(node)
  .then(function(outputFixture) {
    // outputFixture deep-equals
    // {
    //   'foo.txt': 'foo.txt contents',
    //   'subdir': {
    //     'bar.txt': 'bar.txt contents'
    //   }
    // }
  });

build creates a builder behind the scenes, and cleans up automatically. Do not call it more than once on the same node.

Chai example

If you use the Chai assertion library, your test code might look like this:

return fixture.build(node)
  .then(function(fixture) {
    expect(fixture).to.deep.equal({
      'foo.txt': 'foo.txt contents',
      'subdir': {
        'bar.txt': 'bar.txt contents'
      }
    })
  });

With chai-as-promised (eventually), it's even more concise:

return expect(fixture.build(node)).to.eventually.deep.equal({
  'foo.txt': 'foo.txt contents',
  'subdir': {
    'bar.txt': 'bar.txt contents'
  }
});

Builder: Building repeatedly

The build function builds only once. If you need to build repeatedly, for example to test caching logic, use the fixture.Builder class. It's used just like a regular Broccoli Builder, but the build method returns a promise to a fixturify object.

var fixtureBuilder = new fixture.Builder(node);

fixtureBuilder.build()
  .then(function(fixture) {
    // fixture is { 'foo.txt': ... }

    // Build again
    return fixtureBuilder.build();
  })
  .then(function(fixture) {
    // fixture is { 'foo.txt': ... }
  })
  .finally(function() {
    return fixtureBuilder.cleanup();
  });

See also

This package brings in Broccoli core as a dependency, so it's perhaps too heavy to use in "production" build code. If you only need fixture.Node, you can use the lightweight broccoli-fixturify. fixture.Node is simply an alias for broccoli-fixturify's Fixturify class.

About

Fixture testing helpers for Broccoli

Resources

License

Stars

Watchers

Forks

Packages

No packages published