Abadaba
Abadaba

Reputation: 1466

How to unit test with jasmine and browserify?

Any best way to run the jasmine HTML reporter with browserify styled code? I also want to be able to run this headless with phantomjs, thus the need for the HTML reporter.

Upvotes: 9

Views: 8090

Answers (5)

przemcio
przemcio

Reputation: 397

As all above answers are little outdated (of course it doesn't mean that they are not working any more etc.) I would like to point to https://github.com/nikku/karma-browserify this is a preprocessor for karma runner. It combines test files with all required dependencies. Such created browserify bundle is passed to karma which base on configuration runs it. Please be aware that you can choose any modern test framework (jasmin,mocha...) and browsers (phantom, chrome ..) Probably this is exactly what you need :)

Upvotes: 1

Randy
Randy

Reputation: 1548

You may also want to look into Karma. It really simple to setup and it will watch for changes and rerun your test. Check out this sample project that uses Karma to test a browserify/react project. You just need to add a few dependancies and create a karma.conf.js file.

https://github.com/TYRONEMICHAEL/react-component-boilerplate

Upvotes: 0

Sava Jcript
Sava Jcript

Reputation: 394

If you use grunt-watchify, no need to create spec_entry.js. Just use require in your specs, and then bundle your specs with grunt-watchify:

    watchify: {
        test: {
            src: './spec/**/*Spec.js',
            dest: 'spec/spec-bundle.js'
        }
    },
    jasmine: {
        test: {
            options: {
                specs: 'spec/spec-bundle.js'
            }
        }
    },

Then run your tests with

grunt.registerTask('test', ['watchify:test','jasmine:test']);

Upvotes: 4

Amitay Dobo
Amitay Dobo

Reputation: 1388

I've created a detailed example project which addresses the jasmine testing (and others) - see https://github.com/amitayd/grunt-browserify-jasmine-node-example. Discussion at my blog post

The approach in this aspect was to create a Browserify bundle for the main source code (where all the modules are exposed), and one for tests which relies on external for the main source code. Then tests can be run both in PhantomJS or a real browser.

Upvotes: 8

btown
btown

Reputation: 2281

I don't think there's a jasmine-browserify package yet, and it doesn't really match Browserify/NPM's way of doing things (avoid global exports).

For now, I just include /node_modules/jasmine-reporters/ext/jasmine.js and jasmine-html.js at the top of my <head>, and require all my specs in a top-level spec_entry.js that I then use as the entry point for a Browserify bundle that I put right afterwards in the <head>. (Note that if the entry point is not top-level, you'll have a bad time due to a long-lasting, gnarly bug in Browserify).

This plays nicely with jasmine-node as long as you don't assume the presence of a global document or window. However, you do have to remember to register your specs in that spec_entry.js, unless you want to hack Browserify to get it to crawl your directories for .spec.js files.

I'd be very interested in a more elegant solution, though, that would transparently work with jasmine-node and browserify.

Upvotes: 5

Related Questions