New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive / Sub-Folders #106

Closed
quangv opened this Issue Dec 1, 2011 · 38 comments

Comments

Projects
None yet
@quangv
Copy link
Contributor

quangv commented Dec 1, 2011

So what's Mocha's philosophy on sub-folders ?

I notice right now that sub-folders for instance ./test/bugs/* would not be run with your standard ./mocha command...

Is it best practice not to use sub-folders? Or it just a feature that hasn't been implement yet?

Or is there a recommended work-around to make -r recursive support unnecessary ?

@tj

This comment has been minimized.

Copy link
Contributor

tj commented Dec 1, 2011

I prefer to keep things unixy, just passing files, so it's completely arbitrary and up to you, but you could do for example:

TESTS = $(shell find test -name "*.test.js")

test:
   mocha $(TESTS)
@quangv

This comment has been minimized.

Copy link
Contributor Author

quangv commented Dec 1, 2011

okay yeah that seems like it'll work... I was using a ./runTests script anyway, so i'll try this out. thanks.

yeh, keeping functionality modularize is a good idea...

Update: tried it, sweet TJ's command substitution worked like a charm

@tj tj closed this Dec 2, 2011

@tj tj referenced this issue Dec 2, 2011

Closed

Run tests recursively #108

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 3, 2011

Is that a bash script file?

I am aware of "find" but what does $() mean?

@tj

This comment has been minimized.

Copy link
Contributor

tj commented Dec 3, 2011

sub-shell

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 3, 2011

Where do I put all that code?

Inside a .sh file?

And what does test: means in

test:
    mocha ... 

However, I get:

./run_tests.sh: line 3: test:: command not found
./run_tests.sh: line 4: TESTS: command not found

@tj

This comment has been minimized.

Copy link
Contributor

tj commented Dec 3, 2011

that's a makefile, you could do whatever you want though, $() works in posix shells as well as makefiles

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 3, 2011

Ah .. no wonder I wasn't familiar with it.

I guess I can use Jake/Cake to get all files and then pass it to mocha.

@tj

This comment has been minimized.

Copy link
Contributor

tj commented Dec 3, 2011

why not use.. a makefile? haha or a simple shell script, both are easier and more common than jake/cake, developers that don't have make installed are just weird, jake and cake on the other hand are not common

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 3, 2011

But then you want to do something more complicated and you have to learn another scripting language. I prefer JS when you already have a lot of libs and code in JS.

If you find a developer you don't want to ask him if he knows bash and ask him to learn it even if it's just the basics.

JS all the way =)

@tj

This comment has been minimized.

Copy link
Contributor

tj commented Dec 3, 2011

js is terrible for things like this, and it's very basic bash lol anyone using a terminal should know these things

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 3, 2011

At least there is only one lang to learn if you are using JS as frontend, backend and shell lang.

Then for some reasons you wanna test your script files and you can test them with Mocha! :)

@tj

This comment has been minimized.

Copy link
Contributor

tj commented Dec 3, 2011

I'm just saying, there's no point duplicating functionality over and over and over when the shell takes care of these things for you, it's vital

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 3, 2011

I use the shell for one liners. Don't wanna create a script file with bash.

  • Use the libs you are using
  • Write CoffeeScript
  • Will work on all envs that have Node.js installed
  • No need for anyone to be gurus on bash

Also, I tend to like automation a lot. A lot of these one liners could be integrated into a CI system and it would be better to use JS for it.
Frontend <-> Backend <-> Jobs

And if the jobs are doing something that other processes wanna know I can now use Hook.io to alert other processes. Everything is wired together to me.

Thousands of simple tasks won't be easy to handle in the end.

@tj

This comment has been minimized.

Copy link
Contributor

tj commented Dec 3, 2011

mmk..

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 3, 2011

ok

@quangv

This comment has been minimized.

Copy link
Contributor Author

quangv commented Dec 3, 2011

@johnnywengluu

here's my runTest file...

#!/bin/bash

# Runs the mocha tests

mocha $(find test -name "*.coffee") -r should $@

to run this file just use the command ./runTest

make sure runTest has +x file permissions to run as executable chmod +x runTest

the .sh file-extension is optional, but it doesn't hurt to have i guess...

Also $@ is to pipe through other arguments so you can do something like ./runTest -R progress to use a different reporter

Be sure to change mocha $(find test -name "*.coffee") -r should $@ to mocha $(find test -name "*.js") -r should $@ for javascript.


Also I agree with visionmedia, keeping functionality separate is the way to go... using mocha has been awesome so far, this is my workflow

I put all my tests/spec in a sub-directory ./test/* so for example /test/app.coffee /test/index.coffee /test/user.coffee also I have a sub-directory /test/bugs/ with file /test/bugs/index.coffee ...

so while working I just run the current test file that I'm working on, like mocha test/index.coffee (be sure to put your configurations in test/mocha.opts)

this way your tests runs quicker cause it only runs what you need, what you are working on... so then before I commit I do a full test ./runTest

But yeah I think the best way to be a more productive developer IS to learn bash/shell stuff... don't fight linux... it is everywhere :)

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 3, 2011

@quangv: I'll test it. Thanks!

@criso

This comment has been minimized.

Copy link

criso commented Dec 3, 2011

You guys are missing the awesome that a make file brings
Check mocha's makefile https://github.com/visionmedia/mocha/blob/master/Makefile

This means that you can do

make test-unit
make test-bbd
make test-tdd
etc....

Flexibility is king. This wins over a ./runTest
As far as bash is concerned...
if you're gonna write web apps, you're gonna host it on a *nix system. You should know how to use its tools.

just sayin'

@quangv

This comment has been minimized.

Copy link
Contributor Author

quangv commented Dec 4, 2011

@criso Makefile cool beans, yeah I agree, better than a ./runTest, thanks :)

@Enome

This comment has been minimized.

Copy link

Enome commented Jan 19, 2012

Never knew what makefiles were used for until I read this. Thought it was rather cool so I copy paste the example but couldn't get it to work. After looking through some examples I noticed some wrote $(shell somecommand). So I updated the example and it worked.

TESTS = $(shell find test -name "*.test.js")

test:
   mocha $(TESTS)

If anybody could explain why I had to use $(shell) that would be great.

@quangv

This comment has been minimized.

Copy link
Contributor Author

quangv commented Jan 25, 2012

@Enome i believe $() is called a subshell ...

@joeandaverde

This comment has been minimized.

Copy link

joeandaverde commented Feb 26, 2012

I finally got this to work as expected when I changed $(find test -name ".test.js") to $(shell find test -name ".test.js"). If this doesn't hurt anything it might be worth updating the documentation so others don't search for half an hour for a solution.

@quangv

This comment has been minimized.

Copy link
Contributor Author

quangv commented Feb 26, 2012

for me $(find test -name ".test.js") works fine. Maybe it depends on what shell you are using? I'm using /bin/bash

in the Makefile $(find test -name ".test.js") has to be written $$(find test -name ".test.js") with two $$

@Nothus

This comment has been minimized.

Copy link

Nothus commented May 24, 2012

Why not use directory globs?

mocha test/**/*.js
@printercu

This comment has been minimized.

Copy link

printercu commented Jun 24, 2013

@visionmedia Processing directories recursively would be great with package.json file. Now to get it working i have

  "scripts": {
    "test": "mocha test/*.coffee test/**/*.coffee"
  }

But it runs test in other order. And extension is explicitly set. Though it's not a big problem, I think it's just the way one expects to run all tests (including subdirs) when specifies mocha ./test/some_dir.

@eckyputrady

This comment has been minimized.

Copy link

eckyputrady commented Aug 6, 2013

Guys, how about mocha --recursive?

@RobinHoody

This comment has been minimized.

Copy link

RobinHoody commented Feb 21, 2014

+1 @Nothus

@jackniu81

This comment has been minimized.

Copy link

jackniu81 commented Jun 17, 2014

+1 @Nothus
works for me!

@advait

This comment has been minimized.

Copy link

advait commented Jun 25, 2014

Thanks @eckyputrady
Can we make yours the top comment?

@xpepermint

This comment has been minimized.

Copy link

xpepermint commented Jul 20, 2014

--recursive ignores *.coffee files? Setting --compilers coffee:coffee-script ignores .coffee files.

@rdsubhas

This comment has been minimized.

Copy link

rdsubhas commented Oct 12, 2015

@xpepermint yes --recursive ignores *.coffee files. A temporary fix is to not use globbing when running mocha. e.g. Instead of mocha ./test/**/*.coffee just run mocha ./test and it works with recursive flag.

@coryhouse

This comment has been minimized.

Copy link

coryhouse commented Apr 26, 2016

I'm trying to create a cross-platform compatible command line call that runs all tests within a given directory. The problem is, --recursive only applies to the /test folder. I'm placing tests alongside the file under test.

This glob approach works, but only to one level of depth:

mocha src/**/*.test.js

I can list the directories that contain tests which are deeper, but that's obviously not very scalable.

mocha src/**/*.test.js mocha src/dir1/dir2/*.test.js

It would be great if Mocha's recursive flag would apply to folders outside of /test.

@szkrd

This comment has been minimized.

Copy link

szkrd commented Jul 18, 2016

Can this be done in a cross platform fashion at all? I too would love to keep things "unixy", but that's hardly possible in a cross platform environment.

@LukasBombach

This comment has been minimized.

Copy link

LukasBombach commented Aug 10, 2016

Not sure if --recursive is broken when used with a file pattern. If I do this

mocha --recursive --compilers js:babel-core/register --colors ./server/test/*.spec.js

it will only check files that are direct children of the test folder. Same behavior as without --recursive. Doing

mocha --compilers js:babel-core/register --colors ./server/test/*.spec.js ./server/test/**/*.spec.js

works though

@coryhouse

This comment has been minimized.

Copy link

coryhouse commented Aug 10, 2016

@LukasBombach Two tweaks:

  1. Add a wildcard under the base dir.
  2. Wrap your path in double quotes. You'll likely need to escape the double quotes if you're running this in an npm script.

Final suggestion:

mocha --recursive --compilers js:babel-core/register --colors \"./server/test/**/*.spec.js\"

@boneskull

This comment has been minimized.

Copy link
Member

boneskull commented Aug 10, 2016

@LukasBombach @coryhouse

You want --recursive or a glob, generally. Depending on whether your shell expands globs, you may want to put the globspec in double quotes.

# this should work fine in bash
$ mocha --require babel-register ./server/test/**/*.spec.js
# windows; this causes the globspec to be interpreted by the "glob" module
mocha --require babel-register "./server/test/**/*.spec.js"

or

$ mocha --recursive --require babel-register ./server/test
@boneskull

This comment has been minimized.

Copy link
Member

boneskull commented Aug 10, 2016

(--compiler is generally only necessary if you're working with files that don't have the .js extension; it was written with .coffee in mind.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment