-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Docs: migrate programmatic usage to docs and development content to DEVELOPMENT.md #5464
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
Open
TG199
wants to merge
2
commits into
mochajs:main
Choose a base branch
from
TG199:migrate-wiki-developer-guides
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+190
−0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
docs-next/src/content/docs/explainers/developing-mocha.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| --- | ||
| description: Developing Mocha". | ||
| title: Developing Mocha | ||
| --- | ||
|
|
||
| This page contains info on developing Mocha itself. | ||
|
|
||
| ## Environment setup | ||
|
|
||
| When you contribute to mocha itself, you will probably want to try to run your changes on the test suite of another project. You can (and should) run the test suite of mocha itself before committing, but also confirming that your changes give the expected result on another project can be useful. | ||
|
|
||
| For example, [WebSocket.io](https://github.com/LearnBoost/websocket.io/): | ||
|
|
||
| $ git clone https://github.com/LearnBoost/websocket.io.git | ||
|
|
||
| Retreive websocket.io's dependencies, which will include the stable version of mocha: | ||
|
|
||
| $ cd websocket.io/ | ||
| $ npm install | ||
|
|
||
| Replace the mocha dependency by the current git repository: | ||
|
|
||
| $ cd node_modules/ | ||
| $ mv mocha/ mocha.save | ||
| $ git clone https://github.com/visionmedia/mocha.git | ||
|
||
|
|
||
| Install mocha's dependencies for the development version: | ||
|
|
||
| $ cd mocha | ||
| $ npm install | ||
|
|
||
| Run websocket.io's test suite using the development version you just installed: | ||
|
|
||
| $ cd ../.. | ||
| $ ./node_modules/.bin/mocha | ||
|
|
||
75 changes: 75 additions & 0 deletions
75
docs-next/src/content/docs/explainers/programmatic-usage.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| --- | ||
| description: Using Mocha programmatically | ||
| title: Programmatic usage | ||
| --- | ||
|
|
||
| There are a lot of reasons why you might want to automate running the tests using Mocha. Using the command-line can run into some problems if you want to load specific files, for example. | ||
|
|
||
| Here is an example of using Mocha programmatically: | ||
|
|
||
| ```javascript | ||
| var Mocha = require('mocha'), | ||
| fs = require('fs'), | ||
| path = require('path'); | ||
|
|
||
| // Instantiate a Mocha instance. | ||
| var mocha = new Mocha(); | ||
|
|
||
| var testDir = 'some/dir/test' | ||
|
|
||
| // Add each .js file to the mocha instance | ||
| fs.readdirSync(testDir).filter(function(file) { | ||
| // Only keep the .js files | ||
| return file.substr(-3) === '.js'; | ||
|
|
||
| }).forEach(function(file) { | ||
| mocha.addFile( | ||
| path.join(testDir, file) | ||
| ); | ||
| }); | ||
|
|
||
| // Run the tests. | ||
| mocha.run(function(failures) { | ||
| process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures | ||
| }); | ||
| ``` | ||
|
|
||
| `mocha.run()` returns a `Runner` instance which emits many [events](https://github.com/mochajs/mocha/blob/8cae7a34f0b6eafeb16567beb8852b827cc5956b/lib/runner.js#L47-L57) of interest. | ||
|
|
||
| Note that `run` (via `loadFiles`, which it calls) relies on Node's `require` to execute the test interface functions. Thus, files loaded by Mocha will be stored in Node's `require` cache and therefore tests in these files will not be re-run if `mocha.run()` is called again. If you want to run tests multiple times, you may need to clear Node's `require` cache before subsequent calls in whichever manner best suits your needs. The upcoming Mocha-6.0 release will provide `Mocha#unloadFiles`, which will remove all files loaded by `Mocha#loadFiles`. | ||
|
|
||
| Unfortunately, event listeners in multiple places are not yet configured for restartability; for now, we recommend recreating the `mocha` instance before rerunning to _ensure_ everything gets reset properly. | ||
|
|
||
| Find a fully [working example here](https://github.com/mochajs/mocha-examples/tree/master/packages/programmatic-usage). | ||
|
|
||
| ## Set options | ||
|
|
||
| There are two ways to set the options to run the tests. | ||
|
|
||
| Firstly, you can set these options in the constructor object: | ||
|
|
||
| ```javascript | ||
| var mocha = new Mocha({ | ||
| ui: 'tdd', | ||
| reporter: 'list' | ||
| }); | ||
| ``` | ||
|
|
||
| Please check our [API documentation](https://mochajs.org/api/mocha) for a complete list of these options. | ||
|
|
||
| Secondly, on the `mocha` object, there are some chainable methods allowing you to change some more options. | ||
|
|
||
| Here is an example: | ||
|
|
||
| ```javascript | ||
| // Change the reporter to "list" before running the tests | ||
| mocha.reporter('list').run(); | ||
|
|
||
| // Change the UI to "tdd" before running the tests | ||
| mocha.ui('tdd').run(); | ||
|
|
||
| // Or do both changes before running the tests | ||
| mocha.reporter('list').ui('tdd').run(); | ||
| ``` | ||
|
|
||
| Please check our [API documentation](https://mochajs.org/api/mocha) for more information. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Developing Mocha | ||
|
|
||
| This page contains info on developing Mocha itself. | ||
|
|
||
| ## Environment setup | ||
|
|
||
| When you contribute to mocha itself, you will probably want to try to run your changes on the test suite of another project. You can (and should) run the test suite of mocha itself before committing, but also confirming that your changes give the expected result on another project can be useful. | ||
|
|
||
| For example, [WebSocket.io](https://github.com/LearnBoost/websocket.io/): | ||
|
|
||
| $ git clone https://github.com/LearnBoost/websocket.io.git | ||
|
|
||
| Retreive websocket.io's dependencies, which will include the stable version of mocha: | ||
|
|
||
| $ cd websocket.io/ | ||
| $ npm install | ||
|
|
||
| Replace the mocha dependency by the current git repository: | ||
|
|
||
| $ cd node_modules/ | ||
| $ mv mocha/ mocha.save | ||
| $ git clone https://github.com/visionmedia/mocha.git | ||
|
|
||
| Install mocha's dependencies for the development version: | ||
|
|
||
| $ cd mocha | ||
| $ npm install | ||
|
|
||
| Run websocket.io's test suite using the development version you just installed: | ||
|
|
||
| $ cd ../.. | ||
| $ ./node_modules/.bin/mocha |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # using Mocha programmatically | ||
|
|
||
| There are a lot of reasons why you might want to automate running the tests using Mocha. Using the command-line can run into some problems if you want to load specific files, for example. | ||
|
|
||
| Here is an example of using Mocha programmatically: | ||
|
|
||
| ```javascript | ||
| var Mocha = require('mocha'), | ||
| fs = require('fs'), | ||
| path = require('path'); | ||
|
|
||
| // Instantiate a Mocha instance. | ||
| var mocha = new Mocha(); | ||
|
|
||
| var testDir = 'some/dir/test' | ||
|
|
||
| // Add each .js file to the mocha instance | ||
| fs.readdirSync(testDir).filter(function(file) { | ||
| // Only keep the .js files | ||
| return file.substr(-3) === '.js'; | ||
|
|
||
| }).forEach(function(file) { | ||
| mocha.addFile( | ||
| path.join(testDir, file) | ||
| ); | ||
| }); | ||
|
|
||
| // Run the tests. | ||
| mocha.run(function(failures) { | ||
| process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures | ||
| }); | ||
| ``` | ||
|
|
||
| `mocha.run()` returns a `Runner` instance which emits many [events](https://github.com/mochajs/mocha/blob/8cae7a34f0b6eafeb16567beb8852b827cc5956b/lib/runner.js#L47-L57) of interest. | ||
|
|
||
| Note that `run` (via `loadFiles`, which it calls) relies on Node's `require` to execute the test interface functions. Thus, files loaded by Mocha will be stored in Node's `require` cache and therefore tests in these files will not be re-run if `mocha.run()` is called again. If you want to run tests multiple times, you may need to clear Node's `require` cache before subsequent calls in whichever manner best suits your needs. The upcoming Mocha-6.0 release will provide `Mocha#unloadFiles`, which will remove all files loaded by `Mocha#loadFiles`. | ||
|
|
||
| Unfortunately, event listeners in multiple places are not yet configured for restartability; for now, we recommend recreating the `mocha` instance before rerunning to _ensure_ everything gets reset properly. | ||
|
|
||
| Find a fully [working example here](https://github.com/mochajs/mocha-examples/tree/master/packages/programmatic-usage). | ||
|
|
||
| ## Set options | ||
|
|
||
| There are two ways to set the options to run the tests. | ||
|
|
||
| Firstly, you can set these options in the constructor object: | ||
|
|
||
| ```javascript | ||
| var mocha = new Mocha({ | ||
| ui: 'tdd', | ||
| reporter: 'list' | ||
| }); | ||
| ``` | ||
|
|
||
| Please check our [API documentation](https://mochajs.org/api/mocha) for a complete list of these options. | ||
|
|
||
| Secondly, on the `mocha` object, there are some chainable methods allowing you to change some more options. | ||
|
|
||
| Here is an example: | ||
|
|
||
| ```javascript | ||
| // Change the reporter to "list" before running the tests | ||
| mocha.reporter('list').run(); | ||
|
|
||
| // Change the UI to "tdd" before running the tests | ||
| mocha.ui('tdd').run(); | ||
|
|
||
| // Or do both changes before running the tests | ||
| mocha.reporter('list').ui('tdd').run(); | ||
| ``` | ||
|
|
||
| Please check our [API documentation](https://mochajs.org/api/mocha) for more information. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 We already have a
.github/DEVELOPMENT.mdin the repository. I would think we can leave development stuff like this out of the public website.WDYT @mochajs/committers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's repurpose this PR to just edit that development.md file, we don't need any docs here on the main website, that should stay focused to user-facing content