utaina

Slzii.com Saili

https://mochajs.org

Mocha - the fun, simple, flexible JavaScript test framework
Mocha - the fun, simple, flexible JavaScript test framework simple, flexible, fun Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub. # Sponsors Use Mocha at Work? Ask your manager or marketing team if they’d help support our project. Your company’s logo will also be displayed on npmjs.com and our GitHub repository. # Backers Find Mocha helpful? Become a backer and support Mocha with a monthly donation. # Features browser support simple async support, including promises run Node.js tests in parallel test coverage reporting string diff support JavaScript API for running tests auto-detects and disables coloring for non-TTYs async test timeout support test retry support test-specific timeouts reports test durations highlights slow tests file watcher support global variable leak detection optionally run tests that match a regexp auto-exit to prevent “hanging” with an active loop easily meta-generate suites & test-cases config file support node debugger support node native ES modules support source-map support detects multiple calls to done() use any assertion library you want extensible reporting, bundled with 9+ reporters extensible test DSLs or “interfaces” before, after, before each, after each hooks arbitrary transpiler support (coffee-script etc) TextMate bundle # Table of Contents Installation Getting Started Run Cycle Overview Detects Multiple Calls to done() Assertions Asynchronous Code Synchronous Code Arrow Functions Hooks Pending Tests Exclusive Tests Inclusive Tests Retry Tests Dynamically Generating Tests Timeouts Diffs Command-Line Usage Parallel Tests Root Hook Plugins Global Fixtures Test Fixture Decision-Tree Wizard Thing Interfaces Reporters Node.JS native ESM support Running Mocha in the Browser Configuring Mocha (Node.js) The test/ Directory Error Codes Editor Plugins Examples Testing Mocha More Information # Installation Install with npm globally: $ npm install --global mocha or as a development dependency for your project: $ npm install --save-dev mocha As of v10.0.0, Mocha requires Node.js v14.0.0 or newer. # Getting Started $ npm install mocha $ mkdir test $ $EDITOR test/test.js # or open with your favorite editor In your editor: var assert = require('assert'); describe('Array', function () { describe('#indexOf()', function () { it('should return -1 when the value is not present', function () { assert.equal([1, 2, 3].indexOf(4), -1); }); }); }); Back in the terminal: $ ./node_modules/mocha/bin/mocha.js Array #indexOf() ✓ should return -1 when the value is not present 1 passing (9ms) Set up a test script in package.json: "scripts": { "test": "mocha" } Then run tests with: $ npm test # Run Cycle Overview Updated for v8.0.0. The following is a mid-level outline of Mocha’s “flow of execution” when run in Node.js; the “less important” details have been omitted. In a browser, test files are loaded by # Grep The browser may use the --grep as functionality. Append a query-string to your URL: ?grep=api. # Browser Configuration Mocha options can be set via mocha.setup(). Examples: // Use "tdd" interface. This is a shortcut to setting the interface; // any other options must be passed via an object. mocha.setup('tdd'); // This is equivalent to the above. mocha.setup({ ui: 'tdd' }); // Examples of options: mocha.setup({ allowUncaught: true, asyncOnly: true, bail: true, checkLeaks: true, dryRun: true, failZero: true, forbidOnly: true, forbidPending: true, global: ['MyLib'], retries: 3, rootHooks: { beforeEach(done) { ... done();} }, slow: '100', timeout: '2000', ui: 'bdd' }); # Browser-specific Option(s) Browser Mocha supports many, but not all cli options. To use a cli option that contains a “-”, please convert the option to camel-case, (eg. check-leaks to checkLeaks). # Options that differ slightly from cli options: reporter {string|constructor} You can pass a reporter’s name or a custom reporter’s constructor. You can find recommended reporters for the browser here. It is possible to use built-in reporters as well. Their employment in browsers is neither recommended nor supported, open the console to see the test results. # Options that only function in browser context: noHighlighting {boolean} If set to true, do not attempt to use syntax highlighting on output test code. # Reporting The HTML reporter is the default reporter when running Mocha in the browser. It looks like this: Mochawesome is a great alternative to the default HTML reporter. # Configuring Mocha (Node.js) New in v6.0.0 Mocha supports configuration files, typical of modern command-line tools, in several formats: JavaScript: Create a .mocharc.js (or .mocharc.cjs when using "type"="module" in your package.json) in your project’s root directory, and export an object (module.exports = {/* ... */}) containing your configuration. YAML: Create a .mocharc.yaml (or .mocharc.yml) in your project’s root directory. JSON: Create a .mocharc.json (or .mocharc.jsonc) in your project’s root directory. Comments — while not valid JSON — are allowed in this file, and will be ignored by Mocha. package.json: Create a mocha property in your project’s package.json. # Custom Locations You can specify a custom location for your configuration file with the --config option. Mocha will use the file’s extension to determine how to parse the file, and will assume JSON if unknown. You can specify a custom package.json location as well, using the --package option. # Ignoring Config Files To skip looking for config files, use --no-config. Likewise, use --no-package to stop Mocha from looking for configuration in a package.json. # Priorities If no custom path was given, and if there are multiple configuration files in the same directory, Mocha will search for — and use — only one. The priority is: .mocharc.js .mocharc.yaml .mocharc.yml .mocharc.jsonc .mocharc.json # Environment Variables The MOCHA_OPTIONS environment variable may be used to specify command line arguments. These arguments take priority over those found in configuration files. For example, setting the bail and retries options: $ MOCHA_OPTIONS="--bail --retries 3" mocha # Merging Mocha will also merge any options found in package.json into its run-time configuration. In case of conflict, the priority is: Arguments specified on command-line Arguments specified in MOCHA_OPTIONS environment variable. Configuration file (.mocharc.js, .mocharc.yml, etc.) mocha property of package.json Options which can safely be repeated (e.g., --require) will be concatenated, with higher-priority configuration sources appearing earlier in the list. For example, a .mocharc.json containing "require": "bar", coupled with execution of mocha --require foo, would cause Mocha to require foo, then bar, in that order. # Extending Configuration Configurations can inherit from other modules using the extends keyword. See here for more information. # Configuration Format Any “boolean” flag (which doesn’t require a parameter, such as --bail), can be specified using a boolean value, e.g.: "bail": true. Any “array”-type option (see mocha --help for a list) can be a single string value. For options containing a dash (-), the option name can be specified using camelCase. Aliases are valid names, e.g., R instead of reporter. Test files can be specified using spec, e.g., "spec": "test/**/*.spec.js". Flags to node are also supported in configuration files. Use caution, as these can vary between versions of Node.js! For more configuration examples, see the example/config directory on GitHub. # The test/ Directory By default, mocha looks for the glob "./test/*.{js,cjs,mjs}", so you may want to put your tests in test/ folder. If you want to include subdirectories, pass the --recursive option. To configure where mocha looks for tests, you may pass your own glob: $ mocha --recursive "./spec/*.js" Some shells support recursive matching by using the globstar (**) wildcard. Bash >= 4.3 supports this with the globstar option which must be enabled to get the same results as passing the --recursive option (ZSH and Fish support this by default). With recursive matching enabled, the following is the same as passing --recursive: $ mocha "./spec/**/*.js" You should always quote your globs in npm scripts. If you use quotes, the node-glob module will handle its expansion. For maximum compatibility, surround the entire expression with double quotes and refrain from $, ", ^, and \ within your expression. See this tutorial on using globs. Note: Double quotes around the glob are recommended for portability. # Error Codes New in v6.0.0 When Mocha itself throws exception, the associated Error will have a code property. Where applicable, consumers should check the code property instead of string-matching against the message property. The following table describes these error codes: Code Description ERR_MOCHA_INVALID_ARG_TYPE wrong type was passed for a given argument ERR_MOCHA_INVALID_ARG_VALUE invalid or unsupported value was passed for a given argument ERR_MOCHA_INVALID_EXCEPTION a falsy or otherwise underspecified exception was thrown ERR_MOCHA_INVALID_INTERFACE interface specified in options not found ERR_MOCHA_INVALID_REPORTER reporter specified in options not found ERR_MOCHA_NO_FILES_MATCH_PATTERN test file(s) could not be found ERR_MOCHA_UNSUPPORTED requested behavior, option, or parameter is unsupported # Editor Plugins The following editor-related packages are available: # TextMate The Mocha TextMate bundle includes snippets to make writing tests quicker and more enjoyable. # JetBrains JetBrains provides a NodeJS plugin for its suite of IDEs (IntelliJ IDEA, WebStorm, etc.), which contains a Mocha test runner, among other things. The plugin is titled NodeJS, and can be installed via Preferences > Plugins, assuming your license allows it. # Wallaby.js Wallaby.js is a continuous testing tool that enables real-time code coverage for Mocha with any assertion library in VS Code, Atom, JetBrains IDEs (IntelliJ IDEA, WebStorm, etc.), Sublime Text and Visual Studio for both browser and node.js projects. # Emacs Emacs support for running Mocha tests is available via a 3rd party package mocha.el. The package is available on MELPA, and can be installed via M-x package-install mocha. # Mocha Sidebar (VS Code) Mocha sidebar is the most complete mocha extension for vs code. # Features see all tests in VS Code sidebar menu run & debug tests for each level hierarchy from all tests to a single test (and each suite) auto run tests on file save see tests results directly in the code editor # Examples Real live example code: Mocha examples Express Connect SuperAgent WebSocket.io Mocha tests # Testing Mocha To run Mocha’s tests, you will need GNU Make or compatible; Cygwin should work. $ cd /path/to/mocha $ npm install $ npm test # More Information In addition to chatting with us on our Discord, for additional information such as using spies, mocking, and shared behaviours be sure to check out the Mocha Wiki on GitHub. For a running example of Mocha, view example/tests.html. For the JavaScript API, view the API documentation or the source. mochajs.org is licensed under a Creative Commons Attribution 4.0 International License. Copyright OpenJS Foundation and Mocha contributors. All rights reserved. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation, please see our Trademark Policy and Trademark List. Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. The OpenJS Foundation Terms of Use Privacy Policy OpenJS Foundation Bylaws Trademark Policy Trademark List Cookie Policy Last updated Sun Nov 24 00:28:25 2024
en
en
1732470061
https://mochajs.org

Fa'atonu lau saite?

O au mea na e fai?

0.0049009323120117


Webdirectory
Webdirectory

Webdirectory
Mocha - the fun, simple, flexible JavaScript test framework
Webdirectory