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 ?
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)
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
Is that a bash script file?
I am aware of "find" but what does $() mean?
sub-shell
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
that's a makefile, you could do whatever you want though, $() works in posix shells as well as makefiles
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.
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
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 =)
js is terrible for things like this, and it's very basic bash lol anyone using a terminal should know these things
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! :)
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
I use the shell for one liners. Don't wanna create a script file with 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.
mmk..
ok
@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 :)
@quangv: I'll test it. Thanks!
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'
@criso Makefile cool beans, yeah I agree, better than a ./runTest, thanks :)
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.
@Enome i believe $() is called a _subshell_ ...
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.
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 $$
Why not use directory globs?
mocha test/**/*.js
@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.
Guys, how about mocha --recursive?
+1 @Nothus
+1 @Nothus
works for me!
Thanks @eckyputrady
Can we make yours the top comment?
--recursive ignores *.coffee files? Setting --compilers coffee:coffee-script ignores .coffee files.
@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.
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.
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.
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
@LukasBombach Two tweaks:
Final suggestion:
mocha --recursive --compilers js:babel-core/register --colors \"./server/test/**/*.spec.js\"
@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
(--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.)
Most helpful comment
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.jsI 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.jsIt would be great if Mocha's recursive flag would apply to folders outside of /test.