Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

javascript - How can mocha recursively search my `src` folder for a specific filename pattern?

In my npm package, I would like to emulate the pattern Meteor follows: a source file (named client.js) has a test file (named client.tests.js) live in a src/ folder. Tests run with the npm test command.

I'm following the usage docs to the 't'. I do not want to use a find in my package test command.

  1. I understand that mocha can recursively execute tests:

    mocha --recursive

  2. I understand that mocha can execute tests in a specific subfolder using the --recursive flag:

    mocha src --recursive

  3. I also understand that I can specify a glob to filter files by passing *.tests.js:

    mocha *.tests.js

But, I want all three. I want mocha to test only files ending in tests.js in the src folder, recursively checking subdirectories.

mocha --recursive *.tests.js

// See the files?
$ > ll ./src/app/
total 168
-rw-r--r--  ... client.js
-rw-r--r--  ... client.tests.js

// Option A
$ > mocha --recursive *.tests.js
Warning: Could not find any test files matching pattern: *.tests.js
No test files found

// Option B
$ > mocha *.tests.js --recursive
Warning: Could not find any test files matching pattern: *.tests.js
No test files found.

// Option C
$ > mocha --recursive src/app/*.tests.js
3 passing (130ms)
3 failing

So...

  1. Why is mocha not picking up the *.tests.js files in the subfolders?
  2. Why DOES it work if I specify the full path to the file?
  3. How do I make it work as desired?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The --recursive flag is meant to operate on directories. If you were to pass a glob that matches directories, then these directories would be examined recursively but if you pass a glob that matches files, like you are doing, then --recursive is ineffective. I would suggest not using --recursive with a glob because globs already have the capability to look recursively in subdirectories. You could do:

mocha 'src/app/**/*.tests.js'

This would match all files that match *.tests.js recursively in src/app. Note how I'm using single quotes around the pattern. This is to quote the pattern so that it is passed as-is to Mocha's globbing code. Otherwise, your shell might interpret it. Some shells, depending on options, will translate ** into * and you won't get the results you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...