Skip to content
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

deprecate noHighlighting and replace it with reporterOptions.highlight #3701 #4049

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions browser-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ mocha.setup = function(opts) {
}
for (var opt in opts) {
if (opts.hasOwnProperty(opt)) {
this[opt](opts[opt]);
if (opt === 'reporterOptions') {
this.options.reporterOptions = opts[opt];
} else {
this[opt](opts[opt]);
}
}
}
return this;
Expand All @@ -157,14 +161,7 @@ mocha.run = function(fn) {

return Mocha.prototype.run.call(mocha, function(err) {
// The DOM Document is not available in Web Workers.
var document = global.document;
if (
document &&
document.getElementById('mocha') &&
options.noHighlighting !== true
) {
Mocha.utils.highlightTags('code');
}

if (fn) {
fn(err);
}
Expand Down
13 changes: 11 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1588,8 +1588,17 @@ You can pass a reporter's name or a custom reporter's constructor. You can find

#### Options that _only_ function in browser context:

`noHighlighting` _{boolean}_
If set to `true`, do not attempt to use syntax highlighting on output test code.
> _`BREAKING CHANGE in v7.0.0; noHighlighting` is DEPRECATED. We recommend using `reporterOptions.highlight` instead._

`reporterOptions.highlight` _{boolean}_

If set to `false`, do not attempt to use syntax highlighting on output test code.

```js
reporterOptions: {
highlight: false;
}
```

### Reporting

Expand Down
10 changes: 8 additions & 2 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,17 @@ Mocha.prototype.asyncOnly = function() {
* Disables syntax highlighting (in browser).
*
* @public
* @param {boolean} enableHighlight - Whether to enable highlight.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.noHighlighting = function() {
this.options.noHighlighting = true;
Mocha.prototype.noHighlighting = function(enableHighlight) {
utils.deprecate(
'noHighlighting is deprecated; provide {reporterOption: {highlight: false}} to mocha.setup().'
);
this.options.reporterOptions = this.options.reporterOptions || {};
this.options.reporterOptions.highlight =
enableHighlight !== undefined && enableHighlight !== true;
return this;
};

Expand Down
16 changes: 16 additions & 0 deletions lib/reporters/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN;
var EVENT_SUITE_END = constants.EVENT_SUITE_END;
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
var EVENT_RUN_END = constants.EVENT_RUN_END;
var escape = utils.escape;

/**
Expand Down Expand Up @@ -59,6 +60,15 @@ var playIcon = '‣';
function HTML(runner, options) {
Base.call(this, runner, options);

var enableHighlight = true;
if (
options.reporterOptions &&
options.reporterOptions.hasOwnProperty('highlight') &&
!options.reporterOptions.highlight
) {
enableHighlight = false;
}

var self = this;
var stats = this.stats;
var stat = fragment(statsTemplate);
Expand Down Expand Up @@ -224,6 +234,12 @@ function HTML(runner, options) {
updateStats();
});

runner.once(EVENT_RUN_END, function() {
if (enableHighlight) {
utils.highlightTags('code');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be enabled with noHighlighting before it removed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not here. In browser-entry.js

}
});

function appendToStack(el) {
// Don't call .appendChild if #mocha-report was already .shift()'ed off the stack.
if (stack[0]) {
Expand Down
31 changes: 29 additions & 2 deletions test/unit/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,37 @@ describe('Mocha', function() {

describe('#noHighlighting()', function() {
// :NOTE: Browser-only option...
it('should set the noHighlighting option to true', function() {
it('should set the reporterOptions.highlight to false', function() {
var mocha = new Mocha(opts);
mocha.noHighlighting();
expect(mocha.options, 'to have property', 'noHighlighting', true);
expect(
mocha.options.reporterOptions,
'to have property',
'highlight',
false
);
});

it('should set the reporterOptions.highlight to false', function() {
var mocha = new Mocha(opts);
mocha.noHighlighting(true);
expect(
mocha.options.reporterOptions,
'to have property',
'highlight',
false
);
});

it('should set the reporterOptions.highlight to true', function() {
var mocha = new Mocha(opts);
mocha.noHighlighting(false);
expect(
mocha.options.reporterOptions,
'to have property',
'highlight',
true
);
});

it('should be chainable', function() {
Expand Down