Socket
Socket
Sign inDemoInstall

karma-fixture

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    karma-fixture

A plugin for Karma test runner that enables loading fixture html and json files


Version published
Weekly downloads
9.8K
increased by5.06%
Maintainers
1
Install size
11.1 kB
Created
Weekly downloads
 

Changelog

Source

v0.2.1 (2014-05-23)

Full Changelog

Implemented enhancements:

  • Properly empty fixture.json array when loading JSON fixture with append=false

Readme

Source

karma-fixture Build Status

A plugin for the Karma test runner that loads .html and .json fixtures.

It provides the same API as the teaspoon fixture package.

Installation

Install the plugin from npm:

$ npm install karma-fixture --save-dev

Add fixture to the frameworks array in your Karma configuration:

module.exports = (config) ->
  config.set

    # frameworks to use
    frameworks: ['mocha', 'fixture']

    # ...

You also have to register any/all fixtures in your Karma configuration:

(defaults to spec/fixtures)

files: [
    {
        pattern: 'spec/fixtures/**/*',
    },

    # ...
],

Finally you have to add the html2js karma preprocessor:

$ npm install karma-html2js-preprocessor --save-dev

and then configure it Karma configuration to process all html and JSON files:

preprocessors: {
    '**/*.html'   : ['html2js'],
    '**/*.json'   : ['html2js']
}

Implementation details

All fixtures files are pre-loaded as strings as-well, and placed inside the Karma-created window.__html__ array.

The fixture plugin is exposed in the window.fixture object on every test run. It loads fixture files from that array and appends the created html inside the window.fixture.el element that gets created on start-up.

Usage

Lets say you have the following fixture files:

  • spec/fixtures/test1.html

    <p>p</p>
    <a href='#'>
        <span>link</span>
    </a>
    
  • spec/fixtures/json/test1.json

    "{"test":true}"
    

So you can use fixture inside your tests.

describe 'some test that needs a fixture', ->
  beforeEach ->
    @result = fixture.load('html_fixture', 'json_fixture')

  afterEach ->
    fixture.cleanup()

  it 'play with the html fixture', ->
    expect(fixture.el.firstChild).to.equal(@result[0][0])

API

  • fixture.el

    Reference to the container element. Inside this container element, all html fixture files get appended, after creation.

  • fixture.json

    An array of all json objects created from fixture templates.

  • fixture.load(files..., append = false)

    It takes multiple filenames as arguments and load them. It returns the loaded result, or an array of more than one loaded results

    It takes a boolean argument with default value false. If false, it empties the window.fixture.el container element and clears the window.fixture.json array.

    Scenarios:

    html fixture

    It returns an array of all the first-level nodes created by the fixture file:

    html_fixture = fixture.load('test1.html')
    
    expect(html_fixture[0].innerHTML).to.equal('<p>p</p>')
    
    expect(html_fixture[1].innerHTML).to.equal('<a href='#'><span>link</span></a>')
    

    JSON fixture

    It returns a valid object by JSON.parsing the passed json fixture file. Also all JSON files loaded get appended to the window.fixture.json array:

    json_fixture = fixture.load('json/test1.json')
    
    expect(json_fixture).to.eql({"test":true})
    
    expect(fixture.json[0]).to.eql({"test":true})
    

    Multiple files

    The result will be an array containing results of each loaded template:

    loaded_fixtures = fixture.load('test1.html', 'json/test1.json')
    
    expect(loaded_fixtures[0][0].innerHTML).to.equal('<p>p</p>')
    
    expect(loaded_fixtures[0][1].innerHTML).to.equal('<a href='_'><span>link</span></a>')
    
    expect(loaded_fixtures[1]).to.eql({"test":true})
    
    expect(fixture.json[0]).to.eql({"test":true})
    
  • fixture.set(html_strings, append=false)

    It takes multiple html_strings as arguments and load them. It returns the loaded result, or an array of more than one loaded results

    It takes a boolean argument with default value false. If false, it empties the window.fixture.el container element and clears the window.fixture.json array.

    result = fixture.set('<h1>test</h1>')
    # and
    expect(result[0].innerHTML).to.equal('<h1>test</h1>')
    
  • fixture.clear()

    It empties the window.fixture.el container element and clears the window.fixture.json array.

License

The MIT License (MIT)

Keywords

FAQs

Last updated on 23 May 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc