用法

优质
小牛编辑
131浏览
2023-12-01
  Usage: mocha [debug] [options] [files]
Options:
  -V, --version                           output the version number
  -A, --async-only                        force all tests to take a callback (async) or return a promise
  -c, --colors                            force enabling of colors
  -C, --no-colors                         force disabling of colors
  -G, --growl                             enable growl notification support
  -O, --reporter-options <k=v,k2=v2,...>  reporter-specific options
  -R, --reporter <name>                   specify the reporter to use
  -S, --sort                              sort test files
  -b, --bail                              bail after first test failure
  -d, --debug                             enable node's debugger, synonym for node --debug
  -g, --grep <pattern>                    only run tests matching <pattern>
  -f, --fgrep <string>                    only run tests containing <string>
  -gc, --expose-gc                        expose gc extension
  -i, --invert                            inverts --grep and --fgrep matches
  -r, --require <name>                    require the given module
  -s, --slow <ms>                         "slow" test threshold in milliseconds [75]
  -t, --timeout <ms>                      set test-case timeout in milliseconds [2000]
  -u, --ui <name>                         specify user-interface (bdd|tdd|qunit|exports)
  -w, --watch                             watch files for changes
  --check-leaks                           check for global variable leaks
  --full-trace                            display the full stack trace
  --compilers <ext>:<module>,...          use the given module(s) to compile files
  --debug-brk                             enable node's debugger breaking on the first line
  --globals <names>                       allow the given comma-delimited global [names]
  --es_staging                            enable all staged features
  --file <file>                           include a file to be ran during the suite [file]
  --harmony<_classes,_generators,...>     all node --harmony* flags are available
  --preserve-symlinks                     Instructs the module loader to preserve symbolic links when resolving and caching modules
  --icu-data-dir                          include ICU data
  --inline-diffs                          display actual/expected differences inline within each string
  --inspect                               activate devtools in chrome
  --inspect-brk                           activate devtools in chrome and break on the first line
  --interfaces                            display available interfaces
  --no-deprecation                        silence deprecation warnings
  --exit                                  force shutdown of the event loop after test run: mocha will call process.exit
  --no-timeouts                           disables timeouts, given implicitly with --debug
  --no-warnings                           silence all node process warnings
  --opts <path>                           specify opts path
  --perf-basic-prof                       enable perf linux profiler (basic support)
  --napi-modules                          enable experimental NAPI modules
  --prof                                  log statistical profiling information
  --log-timer-events                      Time events including external callbacks
  --recursive                             include sub directories
  --reporters                             display available reporters
  --retries <times>                       set numbers of time to retry a failed test case
  --throw-deprecation                     throw an exception anytime a deprecated function is used
  --trace                                 trace function calls
  --trace-deprecation                     show stack traces on deprecations
  --trace-warnings                        show stack traces on node process warnings
  --use_strict                            enforce strict mode
  --watch-extensions <ext>,...            additional extensions to monitor with --watch
  --delay                                 wait for async suite definition
  --allow-uncaught                        enable uncaught errors to propagate
  --forbid-only                           causes test marked with only to fail the suite
  --forbid-pending                        causes pending tests and test marked with skip to fail the suite
  -h, --help                              output usage information
Commands:
  init <path>  initialize a client-side mocha setup at <path>

-w, --watch

在CWD中执行对JavaScript更改的测试,最初一次。

--exit / --no-exit

在Mocha v4.0.0中更新

版本v4.0.0 之前默认情况下,一旦完成执行所有测试,Mocha将强制其自己的进程退出。这种行为会带来一系列潜在问题; 它表示测试(或固定装置,线束,被测代码等),这些测试不能在自身正常后进行清理。最终,“脏”测试可以(但不总是)导致假阳性假阴性结果。

如果服务器仍然在端口上监听,或者套接字仍处于打开状态等,“挂”最常表现为它。它也可能像失控setInterval(),甚至Promise是永不满足的错误。

v4.0.0中的默认行为--no-exit以前的行为--exit

“解决”问题的最简单方法是简单地传递--exit给Mocha进程。可以是耗时的调试,因为它是哪里的问题并不总是显而易见的,但它建议这样做。

为了确保您的测试不会让人感到困惑,这里有一些入门的想法:

--compilers

在Mocha v4.0.0中更新

--compilers自Mocha v4.0.0起已弃用。请参阅进一步的解释和解决方法

CoffeeScript is no longer supported out of the box. CS and similar transpilers may be used by mapping the file extensions (for use with --watch) and the module name. For example --compilers coffee:coffee-script with CoffeeScript 1.6- or --compilers coffee:coffee-script/register with CoffeeScript 1.7+.

About Babel

If your ES6 modules have extension .js, you can npm install --save-dev babel-register and use mocha --require babel-register; --compilers is only necessary if you need to specify a file extension.

-b, --bail

Only interested in the first exception? use --bail!

-d, --debug

Enables node’s debugger support, this executes your script(s) with node debug <file ...> allowing you to step through code and break with the debugger statement. Note the difference between mocha debug and mocha --debug: mocha debug will fire up node’s built-in debug client, mocha --debug will allow you to use a different interface — such as the Blink Developer Tools. Implies --no-timeouts.

--globals <names>

Accepts a comma-delimited list of accepted global variable names. For example, suppose your app deliberately exposes a global named app and YUI, you may want to add --globals app,YUI. It also accepts wildcards. You could do --globals '*bar' and it would match foobar, barbar, etc. You can also simply pass in '*' to ignore all globals.

By using this option in conjunction with --check-leaks, you can specify a whitelist of known global variables that you would expect to leak into global scope.

--check-leaks

Use this option to have Mocha check for global variables that are leaked while running tests. Specify globals that are acceptable via the --globals option (for example: --check-leaks --globals jQuery,MyLib).

-r, --require <module-name>

The --require option is useful for libraries such as should.js, so you may simply --require should instead of manually invoking require('should') within each test file. Note that this works well for should as it augments Object.prototype, however if you wish to access a module’s exports you will have to require them, for example var should = require('should'). Furthermore, it can be used with relative paths, e.g. --require ./test/helper.js

-u, --ui <name>

--ui选项允许您指定要使用的接口,默认为“bdd”。

-R, --reporter <name>

--reporter选项允许您指定将使用的报告器,默认为“spec”。该标志也可用于利用第三方记者。例如,如果你npm install mocha-lcov-reporter可以这样做的话--reporter mocha-lcov-reporter

-t, --timeout <ms>

指定测试用例超时,默认为2秒。要覆盖,您可以以毫秒为单位传递超时,或者使用s后缀ex:--timeout 2s--timeout 2000等效的值传递。

--no-timeouts

禁用超时。相当于--timeout 0

-s, --slow <ms>

指定“慢”测试阈值,默认为75毫秒。Mocha使用它来突出显示花费太长时间的测试用例。

--file <file>

首先在测试套件中添加要包含的文件。如果您有一些必须包含在测试套件中的通用设置代码,这将非常有用。传递的文件不受任何其他标志的影响(--recursive--sort无效)。接受多个--file标志以包含多个文件,标志的给定顺序是文件包含在测试套件中的顺序。也可以用于mocha.opts

-g, --grep <pattern>

--grep指定的选项将触发mocha仅运行与pattern内部编译为a 的给定匹配的测试RegExp

例如,假设您有“api”相关测试以及“app”相关测试,如下面的代码段所示; 人们可以使用--grep api--grep app运行其中一个或另一个。套件或测试用例标题的任何其他部分也是如此,--grep users也是有效的,甚至是有效的--grep GET

describe('api', function() {
describe('GET /api/users', function() {
  it('respond with an array of users', function() {
    // ...
  });
});
});
describe('app', function() {
describe('GET /users', function() {
  it('respond with an array of users', function() {
    // ...
  });
});
});