Mocha-compatible fixtures support
Asked Answered
I

2

10

I am looking to convert from Jasmine tests to Mocha tests because of its ability to do Before(all), its reporting capabilities, and its support for coffeescript.

One thing I haven't been able to find is whether Mocha (or in conjunction with a compatible assertion library) supports fixtures like jasmine-jquery does (https://github.com/velesin/jasmine-jquery). Does such functionality exist for Mocha?

I'm trying to test some in-browser code (so I will be running it with the html test runner) that manipulates the DOM. I don't wish to use zombiejs because I'd prefer to run it within the environment I'm testing in.

Edit: Just for reference, while searching for a solution, I came across using grunt + mocha with a phantomjs implementation. This also doesn't work for my case as I'd like to use a custom version of webkit.

Ihram answered 13/6, 2012 at 21:58 Comment(0)
I
11

From what I've been able to figure out, the answer is no. This is probably due to the fact that Mocha was first meant to support node.js. In node, the notion of fixtures in jasmine-jquery doesn't exist because a DOM/browser doesn't exist.

Looking at the fixtures support for the jasmine-jquery plugin, I realize now that is may not be so difficult to build this functionality myself. The basic mechanism is to use jQuery to add an external template to the DOM, while retaining the references to it so that cleanup and caching is possible.

I will update this answer if I decide to release something on github

Edit: here it is - https://github.com/badunk/js-fixtures. The code and specs were essentially copied from jasmine-jquery with the dependencies removed.

Ihram answered 24/6, 2012 at 4:16 Comment(3)
Does js-fixtures also replicate loading JSON, like 'getJSONFixture()' in jasmine-jquery?Pronuba
no, that isn't currently supported - I will look into it as an enhancement. PR's welcome: github.com/badunk/js-fixtures/issues/12Ihram
Is this still the case? There are setup and teardown functions in Mocha and they support running tests in the browser.Melodimelodia
B
0

Have you tried zombie.js? It installs with NPM and seems rather agnostic to test framework.

This gave me a fully-functioning window object that I could decorate with jQuery.

Browser = require 'zombie'

browser = new Browser()
global.window = browser.window
require '../src/js/vendor/jquery-1.7.1.min.js'

UPDATE:

I've revisited this problem and it looks like jquery + jsdom (via NPM) seem to be a more popular and lightweight solution. Example: http://www.netboy.pl/2012/10/testing-jquery-code-with-mocha-chai-and-jsdom/

Beforetime answered 9/7, 2012 at 16:39 Comment(1)
JSDOM and Zombie are a bit problematic. For example, no support for classList, dataset, and a bunch of other things I wanted to use. Seems I'm going back to phantomjs with this.Cuisse

© 2022 - 2024 — McMap. All rights reserved.