Code Monkey home page Code Monkey logo

grunt-simple-mocha's Introduction

grunt-simple-mocha

A simple wrapper for running tests with Mocha. It's pretty much a cleaned up and plugin-ized version of this helpful Gist by johnkpaul.

If you're looking to run client-side specs with PhantomJS, you might be interested in grunt-mocha.

Installation

  1. Install this grunt plugin next to your project's Gruntfile.js with:

    npm install grunt-simple-mocha --save-dev
  2. Then add this line to your project's grunt.js gruntfile:

    grunt.loadNpmTasks('grunt-simple-mocha');

Usage

In your Gruntfile.js:

grunt.initConfig({
  simplemocha: {
    all: {
      src: ['test/**/*.js', '**/*.spec.js']
    }
  }
});

// For this to work, you need to have run `npm install grunt-simple-mocha`
grunt.loadNpmTasks('grunt-simple-mocha');

// Add a default task. This is optional, of course :)
grunt.registerTask('default', 'simplemocha');

Now, you can just run grunt simplemocha in your shell to run the tests. That's it!

Advanced Mocha Options

All options parameters are passed directly to Mocha. Therefore, you can use these options to configure how Mocha runs.

  • ui name "bdd", "tdd", "exports" etc
  • reporter reporter instance, defaults to mocha.reporters.spec
  • globals array of accepted globals
  • timeout timeout in milliseconds
  • retries number of times to retry failed tests
  • bail bail on the first test failure
  • slow milliseconds to wait before considering a test slow
  • ignoreLeaks ignore global leaks
  • fullTrace display the full stack-trace on failing
  • grep string or regexp to filter tests with

The Gruntfile configuration would look like this:

grunt.initConfig({
  simplemocha: {
    //
    // These are passed directly to the mocha constructor. See:
    //   https://github.com/mochajs/mocha/blob/master/lib/mocha.js#L56-L74
    //
    options: {
      ui: 'bdd',
      reporter: 'tap',
      globals: ['window','document','$','should'],
      timeout: 3000,
      retries: 2,
      bail: false,
      slow: 2000,
      ignoreLeaks: false,
      fullTrace: true
      grep: 'users',
    },

    all: { src: ['test/**/*.js'] }
  }
});

Running tests

Run npm install && npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Migration Guides

Updating from 0.2.x to 0.3.x

This task now depends on grunt 0.4.x. Please see the grunt 0.3 to 0.4 migration guide for more details.

Updating from 0.1.x to 0.2.x

If you were using 0.1.x, the task name has changed from mocha to simplemocha to avoid confusion with grunt-mocha. Please make sure your grunt.js file is updated. See #3.

Release History

  • v0.1 - Woo!
  • v0.2 - Changed the task name from mocha to simplemocha. See #3.
  • v0.3 - Updated to support grunt 0.4.x.
  • v0.4 - Updated to support node 0.10.x.
  • v0.4.1 - Updated readme and added tests.

License

Copyright (c) 2012 Mukund Lakshman

Licensed under the MIT license.

grunt-simple-mocha's People

Contributors

bharrisau avatar c0bra avatar dfernandez79 avatar heldr avatar marco-faustinelli avatar ts-web avatar zappan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

grunt-simple-mocha's Issues

var paths = this.filesSrc.map(path.resolve) show the test src path

in your readme, it have not comments for this filesSrc, it let user confuse. it should be fixed.

before

grunt.initConfig({
  simplemocha: {
    options: {
      globals: ['should'],
      timeout: 3000,
      ignoreLeaks: false,
      grep: '*-test',
      ui: 'bdd',
      reporter: 'tap'
    },

    all: { src: 'test/**/*.js' }
  }
});

should be change to

grunt.initConfig({
  simplemocha: {
    options: {
      globals: ['should'],
      timeout: 3000,
      ignoreLeaks: false,
      grep: '*-test',
      ui: 'bdd',
      reporter: 'tap'
    },

    filesSrc: ['test/**/*.js' ]
  }
});

The `grep` option may cause confusion

For new users of mocha, they might not know what the grep option is for. For people familiar with jasmine, they might think that the value *-test pertains to the filename suffix, e.g. thing-test.js, since Jasmine by default looks for files like thing.spec.js. But that is not what grep does.

The README should mention that the grep option filters the tests and only runs those that match the pattern. It does not run those that don't.

Or else, the README needs to include a basic unit test that works with the configuration. Because most likely, the configuration (as is) won't run the tests that new users write.

Reporters

Hello,

What is the full list of supported reporters?
I currently tried to use XUnit but I got the following: Warning: invalid reporter "XUnit"

Thank you in advance.

Merge this plugin to grunt-mocha

Hello! I've opened a pull request into grunt-mocha plugin to merge the contents of this plugin there: kmiyashiro/grunt-mocha#17

There is some confusion and conflict between these two plugins, and I'd love to see these being resolved so that work can continue on a single, better plugin. Hopefully kmiyashiro will merge these changes in, and these plugins can be merged. I've tried to make the changes as non-invasive as possible, so that neither this or grunt-mocha -plugin users need to change their grunt configs.

Should not hide exceptions

directly with mocha:

$ mocha --compilers coffee:coffee-script test/mocha-common.js test/unit/*

  ․․․․․․․․․․․․  ✖ 1 of 12 tests failed:

  1) web-server testacular source handler should serve client.html:
     TypeError: null is not a spy or a call to a spy!
      at assertCanWorkWith (/Users/vojta/Code/testacular/node_modules/sinon-chai/lib/sinon-chai.js:35:19)
      at Assertion.alwaysSinonMethod (/Users/vojta/Code/testacular/node_modules/sinon-chai/lib/sinon-chai.js:58:13)
      at Assertion.Object.defineProperty.get (/Users/vojta/Code/testacular/node_modules/chai/lib/chai/utils/addProperty.js:35:29)
      at MockHTTPResponse.includedFiles.path (/Users/vojta/Code/testacular/test/unit/web-server.spec.coffee:164:43)
      at MockHTTPResponse.g (events.js:185:14)
      at MockHTTPResponse.EventEmitter.emit (events.js:85:17)
      at MockHTTPResponse.end (/Users/vojta/Code/testacular/node_modules/mock-http-response/src/index.coffee:80:19)
      at /Users/vojta/Code/testacular/lib/web-server.js:40:21
      at Array.Mock.readFile (/Users/vojta/Code/testacular/node_modules/mocks/lib/fs.js:195:7)
      at nextTickHandler (/Users/vojta/Code/testacular/node_modules/mocks/lib/util.js:29:31)
      at process.startup.processNextTick.process._tickCallback (node.js:244:9)

through simple-mocha:

grunt simplemocha:unit
Running "simplemocha:unit" (simplemocha) task

  ․․․․․․․․․․․Fatal error: null is not a spy or a call to a spy!

Make options work correctly

When adding grunt 0.4.x support, I didn't correctly update how grunt-simple-mocha does options. Working on a fix for 0.3.1.

Update readme and usage description on npmjs.com

The usage description on npmjs.com for this package still uses the mocha name for the task (0.1.x version). Consider updating to the latest usage to avoid confusion with users finding the package on npm.

Vows support

I quite like grunt-simple-mocha. Great work! However, I've been working on a migration over to Vows. I was a little disappointed with the test runners currently pairing Grunt and Vows. I couldn't find any which supported multitasks and the same form of pattern expansion. (i.e. test/**/*.js)

I found copying the basic structure of grunt-simple-mocha to be quite useful in trying to do something similar for Vows. I considered just releasing grunt-simple-vows, but I was curious to hear your thoughts on whether these might better exist as a single project.

reporter spec won't print stack trace when using simplemocha

I am using simplemocha with the spec reporter. My options are:

  options: {
    globals: ['expect'],
    timeout: 1000,
    ignoreLeaks: false,
    ui: 'bdd',
    reporter: 'spec'  // was 'tap'
  },

I'd like to see the whole stack trace in case of errors, as presented in https://mochajs.org/images/reporter-spec-fail.png, but I see on screen only the first line of the stack trace.

I want to see the whole stack trace.

I assume this is a simplemocha bug, but I may be wrong :-)
Thank you for your attention.

Setting working directory

My tests need to be run from a different working directory than the one where my Gruntfile resides. Is there a way to run mocha through this plugin with a different working directory?

FYI, it does look like selecting files with cwd works, but then when the plugin tries to run those test files, it does not use the cwd working directory.

Simplemocha broke on Grunt 0.4

Hi, I can run my tests via the mocha cmd, but when I run it through grunt-simply-mocha, I always get this error: Warning: object is not a function Use --force to continue.

Any ideas?

    // My configuration
    simplemocha: {
      options: {
        ui: "bdd",
        reporter: "spec"
      },
      all: "test/mocha/node.js"
    }

Feature request: specify destination for results

I'm trying to get my mocha tests integrated with Jenkins via the xUnit reporter in mocha. This works fine when using mocha without grunt as I can do this:

mocha -R xunit -r should $WORKSPACE/tests/mocha/*.js > $WORKSPACE/testresults.xml

With grunt, I'd like to do this:

grunt test
// runs simplemocha:production

simplemocha: {
        options: {
            globals: ['should'],
            reporter: 'spec',
            timeout: 5000
        },
        all: { 
            src: [
              // require should...
              'node_modules/should/lib/should.js',
              // This test can use it!
                'tests/mocha/*.js']
        },
      production: {
        options: {
          reporter: 'xunit'
        },
        src: [
          // require should...
          'node_modules/should/lib/should.js',
          // This test can use it!
            'tests/mocha/*.js'],
        dest: 'tests/mocha.xml'
      }
    }

I'm after just the XML output, not the "Running "simplemocha:production" (simplemocha) task" console message before it, otherwise Jenkins won't accept the file as valid xUnit.

--require option not working

Mocha allows you to pass a --require flag for things like should.js, and it will load it so you don't have to manually require('should') in every single test. Setting this option doesn't do anything with grunt-simple-mocha.

Using Assertion Libs

I'm attempting to add tests to my Grunt plugin. I want to use Mocha and Chai.

I've added simple mocha and installed chai from npm. initConfig is

simplemocha: {
        options: {
            globals: ['chai']
        },

        all: { 
            src: [
              'node_modules/chai/lib/chai.js',
              'test/**/*.js'
            ] 
        }
    }

I have test.js with content as per

'use strict';

var assert = require("chai");

describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return -1 when the value is not present', function(){
      assert.equal(-1, [1,2,3].indexOf(5));
      assert.equal(-1, [1,2,3].indexOf(0));
    })
  })
});

...which is an example provided on the Chai website. I get the error

TypeError: Object #<Object> has no method 'equal'

Am I configuring things incorrectly?

Expose a way to emulate `-r` option

When using mocha via the command line you can specify files with -rwhich will cause them to be required before your tests are. This is useful for doing setup, but it seems there is no way to do that with simplemocha.

How do you feel about a setup option that receives the Mocha instance and can execute arbitrary operations before mocha is actually run?

How to run mocha in verbose mode?

My simplemocha run is failing with

Warning: undefined is not a function Use --force to continue.

How can I run in verbose mode, so to know at which line of which file this undefined popped up?.

Grunt 0.4 Release

I'm posting this issue to let you know that we will be publishing Grunt 0.4 on Monday, February 18th.

If your plugin is not already Grunt 0.4 compatible, would you please consider updating it? For an overview of what's changed, please see our migration guide.

If you'd like to develop against the final version of Grunt before Monday, please specify "grunt": "0.4.0rc8" as a devDependency in your project. After Monday's release, you'll be able to use "grunt": "~0.4.0" to actually publish your plugin. If you depend on any plugins from the grunt-contrib series, please see our list of release candidates for compatible versions. All of these will be updated to final status when Grunt 0.4 is published.

Also, in an effort to reduce duplication of effort and fragmentation in the developer community, could you review the grunt-contrib series of plugins to see if any of your functionality overlaps significantly with them? Grunt-contrib is community maintained with 40+ contributors—we'd love to discuss any additions you'd like to make.

Finally, we're working on a new task format that doesn't depend on Grunt: it's called node-task. Once this is complete, there will be one more conversion, and then we'll never ask you to upgrade your plugins to support our changes again. Until that happens, thanks for bearing with us!

If you have any questions about how to proceed, please respond here, or join us in #grunt on irc.freenode.net.

Thanks, we really appreciate your work!

Calling path.resolve from tests with "./" relative notation resolves it starting from project's root

app/
├── tests/
│ ├── data
│ ├──── data.json
│ ├── sample-test.js
└── Gruntfile.js
└── package.json

Gruntfile:
 grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    ...
    simplemocha: {
        all: { src: 'test/**/*-test.js' }
    },
    ...
 });

Issue:

  • calling path.resolve with "./data/data.json" from sample-test.js, results in path relative to the root dir of the project, e.g. could be "/Users/tsenkov/repos/app/data/data.json".

Expected:

  • "/Users/tsenkov/repos/app/tests/data/data.json"

Running from a grunt-regarde task only runs tests on first pass

  1. Grunt file that looks something like this:

        regarde: {
          all: {
            files: '<%= jshint.all %>'
            , tasks: ['simplemocha', 'mocha']
          }
        }
         , simplemocha: {
          options: {
            globals: ['should']
            , timeout: 1000
            , ignoreLeaks: true
            , ui: 'bdd'
          }
          , all: {
            src: 'test/**/*.js'
          }
        }
  2. run grunt regarde

  3. Results look something like:

    grunt regarde
    Running "regarde" task
    Watching Gruntfile.js,test/**/*.js
    
    Running "simplemocha:all" (simplemocha) task
    
      ․․․․․․․
    
      7 tests complete (7 ms)
    
    
    Running "regarde" task
    
    Running "simplemocha:all" (simplemocha) task
    
    
    
      0 tests complete (0 ms)
    
    
    Running "regarde" task

expected:

With nothing changing, the tests should run again.

Please update npm dependency: mocha

warning grunt-simple-mocha > mocha > glob > [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
warning grunt-simple-mocha > mocha > [email protected]: to-iso-string has been deprecated, use @segment/to-iso-string instead.
warning grunt-simple-mocha > mocha > [email protected]: Jade has been renamed to pug, please install the latest version of pug instead of jade

"mocha": "^2.3.4"

The latest mocha version is 4.0.1 now.

Taks cannot be proceeded after upgrading Node to v0.10.x

After I update Node to v0.10.x, simplemocha task cannot be proceeded with this warning:

$ grunt simplemocha
Running "simplemocha:test" (simplemocha) task
Warning: Arguments to path.resolve must be strings Use --force to continue.

Aborted due to warnings.

My task definition is here:

path:
  test: 'test'

simplemocha:
  options:
    compilers: 'coffee:coffee-script'
    globals: []
    ignoreLeaks: false
    ui: 'bdd'
    reporter: 'spec'
  test:
    src: '<%= path.test %>/**/*.coffee'

Any idea? Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.