-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Docs: migrate HOW-TO wiki pages to main documentation #5463
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,36 @@ | ||||||||
--- | ||||||||
description: Learn how to use environment variables in Mocha tests | ||||||||
title: HOW-TO: Use environment variables | ||||||||
--- | ||||||||
|
||||||||
Sometimes you might want your test to make use of variables set on an environment-level. Common reasons for this include | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
- Flagging a run as CI | ||||||||
- Setting the domain of an application | ||||||||
- Pointing to a test database | ||||||||
- Setting a key which you want to remain secure | ||||||||
|
||||||||
The best way to do this is to use NodeJS's native environmental variables. | ||||||||
|
||||||||
## Example | ||||||||
|
||||||||
In your `package.json` or directly in a terminal set your variable name and value. | ||||||||
```bash | ||||||||
env CI=STAGE mocha | ||||||||
``` | ||||||||
|
||||||||
Then in your test or code reference it via `process.env.CI`. e.g. | ||||||||
```javascript | ||||||||
if (process.env.CI === "STAGE") // do something | ||||||||
``` | ||||||||
|
||||||||
The value is available from anywhere in a spec file allowing you to set values from it before the tests run. e.g. | ||||||||
|
||||||||
```javascript | ||||||||
const URL = `${process.env.APP_HOST}/${process.env.APP_URI}`; | ||||||||
describe('Test the page loads',() { | ||||||||
// tests ... | ||||||||
``` | ||||||||
|
||||||||
Any issues or questions please contact us on Gitter. | ||||||||
|
||||||||
Thanks | ||||||||
Comment on lines
+34
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too much copypasta 🍝
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,57 @@ | ||||||
--- | ||||||
description: How to work with global variables in Mocha tests | ||||||
title: Mess with globals | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Mess with" is fun phrasing, but these days we're more ... cleaned up.
Suggested change
|
||||||
--- | ||||||
Related issue [#1582](https://github.com/mochajs/mocha/issues/1582) | ||||||
|
||||||
If your test messes with globals, a reasonable expectation is that you will clean up after yourself. This is not impossible in the case of `process.stdout.write()` or `console.log()`. In fact, it's pretty easy. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is just moving code over, but we shouldn't say things are "easy".
Suggested change
|
||||||
|
||||||
```js | ||||||
var expect = require('chai').expect; | ||||||
|
||||||
describe('my nice test', function() { | ||||||
var write, log, output = ''; | ||||||
|
||||||
// restore process.stdout.write() and console.log() to their previous glory | ||||||
var cleanup = function() { | ||||||
process.stdout.write = write; | ||||||
console.log = log; | ||||||
output = ""; | ||||||
}; | ||||||
|
||||||
beforeEach(function() { | ||||||
// store these functions to restore later because we are messing with them | ||||||
write = process.stdout.write; | ||||||
log = console.log; | ||||||
|
||||||
// our stub will concatenate any output to a string | ||||||
process.stdout.write = console.log = function(s) { | ||||||
output += s; | ||||||
}; | ||||||
}); | ||||||
|
||||||
// restore after each test | ||||||
afterEach(cleanup); | ||||||
|
||||||
it('should suppress all output if a non-AssertionError was thrown', function() { | ||||||
process.stdout.write('foo'); | ||||||
console.log('bar'); | ||||||
// uncomment below line to suppress output, which is bad | ||||||
// expect(output).to.equal(foobar); | ||||||
expect(output).to.equal('foobar'); | ||||||
}); | ||||||
|
||||||
it('should not suppress any output', function() { | ||||||
try { | ||||||
process.stdout.write('foo'); | ||||||
console.log('bar'); | ||||||
// uncomment below line to just throw an AssertionError | ||||||
// expect(output).to.equal('barfoo'); | ||||||
expect(output).to.equal(foobar); // ReferenceError | ||||||
} catch (e) { | ||||||
cleanup(); | ||||||
throw e; | ||||||
} | ||||||
}); | ||||||
}); | ||||||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
description: Learn how to properly stub stdout for testing console output | ||
title: HOW-TO: Correctly stub stdout | ||
--- | ||
|
||
If you want to stub stdout inside your own code (via `process.stdout.write` or `console.log`) there is a potential to hinder Mochas reporters output. This is because they rely on the same mechanisms to print results of the tests. | ||
|
||
i.e. | ||
``` javascript | ||
it('should do, but it do not', function() { | ||
// user wants to hide output generated by console.log calls in fn 'foo' | ||
console.log = function() {}; | ||
foo(); | ||
|
||
expect(...) | ||
}); | ||
``` | ||
|
||
This will result in a faulty reporter output. | ||
|
||
The correct way to handle this is to stub before and restore after the function call. | ||
|
||
``` javascript | ||
it('will do', function() { | ||
var consoleLogStub = sinon.stub(console, 'log'); | ||
foo(); | ||
consoleLogStub.restore(); | ||
|
||
expect(...) | ||
}); | ||
``` | ||
|
||
Now the reporters can run and print the assertion without any interference, while stubbing stdout inside your function. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Environment variables | ||
|
||
Sometimes you might want your test to make use of variables set on an environment-level. Common reasons for this include | ||
|
||
- Flagging a run as CI | ||
- Setting the domain of an application | ||
- Pointing to a test database | ||
- Setting a key which you want to remain secure | ||
|
||
The best way to do this is to use NodeJS's native environmental variables. | ||
|
||
## Example | ||
|
||
In your `package.json` or directly in a terminal set your variable name and value. | ||
|
||
```bash | ||
env CI=STAGE mocha | ||
``` | ||
|
||
Then in your test or code reference it via `process.env.CI`. e.g. | ||
|
||
```javascript | ||
if (process.env.CI === "STAGE") // do something | ||
``` | ||
|
||
The value is available from anywhere in a spec file allowing you to set values from it before the tests run. e.g. | ||
|
||
```javascript | ||
const URL = `${process.env.APP_HOST}/${process.env.APP_URI}`; | ||
describe('Test the page loads',() { | ||
// tests ... | ||
``` | ||
|
||
Any issues or questions please contact us on Gitter. | ||
|
||
Thanks |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Mess with globals | ||
|
||
Related issue [#1582](https://github.com/mochajs/mocha/issues/1582) | ||
|
||
If your test messes with globals, a reasonable expectation is that you will clean up after yourself. This is not impossible in the case of `process.stdout.write()` or `console.log()`. In fact, it's pretty easy. | ||
|
||
```js | ||
var expect = require('chai').expect; | ||
|
||
describe('my nice test', function() { | ||
var write, log, output = ''; | ||
|
||
// restore process.stdout.write() and console.log() to their previous glory | ||
var cleanup = function() { | ||
process.stdout.write = write; | ||
console.log = log; | ||
output = ""; | ||
}; | ||
|
||
beforeEach(function() { | ||
// store these functions to restore later because we are messing with them | ||
write = process.stdout.write; | ||
log = console.log; | ||
|
||
// our stub will concatenate any output to a string | ||
process.stdout.write = console.log = function(s) { | ||
output += s; | ||
}; | ||
}); | ||
|
||
// restore after each test | ||
afterEach(cleanup); | ||
|
||
it('should suppress all output if a non-AssertionError was thrown', function() { | ||
process.stdout.write('foo'); | ||
console.log('bar'); | ||
// uncomment below line to suppress output, which is bad | ||
// expect(output).to.equal(foobar); | ||
expect(output).to.equal('foobar'); | ||
}); | ||
|
||
it('should not suppress any output', function() { | ||
try { | ||
process.stdout.write('foo'); | ||
console.log('bar'); | ||
// uncomment below line to just throw an AssertionError | ||
// expect(output).to.equal('barfoo'); | ||
expect(output).to.equal(foobar); // ReferenceError | ||
} catch (e) { | ||
cleanup(); | ||
throw e; | ||
} | ||
}); | ||
}); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Stub stdout | ||
|
||
If you want to stub stdout inside your own code (via `process.stdout.write` or `console.log`) there is a potential to hinder Mochas reporters output. This is because they rely on the same mechanisms to print results of the tests. | ||
|
||
i.e. | ||
|
||
``` javascript | ||
it('should do, but it do not', function() { | ||
// user wants to hide output generated by console.log calls in fn 'foo' | ||
console.log = function() {}; | ||
foo(); | ||
|
||
expect(...) | ||
}); | ||
``` | ||
|
||
This will result in a faulty reporter output. | ||
|
||
The correct way to handle this is to stub before and restore after the function call. | ||
|
||
``` javascript | ||
it('will do', function() { | ||
var consoleLogStub = sinon.stub(console, 'log'); | ||
foo(); | ||
consoleLogStub.restore(); | ||
|
||
expect(...) | ||
}); | ||
``` | ||
|
||
Now the reporters can run and print the assertion without any interference, while stubbing stdout inside your function. |
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.
I get a build error on this and the other
HOW-TO
file:The
title:
frontmatter is indeed invalid. You'll need to escape it with quotes. The:
is getting interpreted as invalid YML.That being said, I don't think there's any need to keep to the
HOW-TO
. All of these "Explainers" section pages are variants of "how to" guides. So, I would say it should match the existing titles convention: