Skip to content

Commit 1378fd8

Browse files
chore: switch Runnable, Hook, and Test from util.inherits to ES2015 classes (#5178)
Co-authored-by: Mark Wiemer <markwiemer@outlook.com>
1 parent 5574738 commit 1378fd8

File tree

3 files changed

+551
-564
lines changed

3 files changed

+551
-564
lines changed

lib/hook.js

Lines changed: 70 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,81 @@
11
"use strict";
22

33
var Runnable = require("./runnable");
4-
const { inherits, constants } = require("./utils");
4+
const { constants } = require("./utils");
55
const { MOCHA_ID_PROP_NAME } = constants;
66

7-
/**
8-
* Expose `Hook`.
9-
*/
10-
11-
module.exports = Hook;
12-
13-
/**
14-
* Initialize a new `Hook` with the given `title` and callback `fn`
15-
*
16-
* @class
17-
* @extends Runnable
18-
* @param {String} title
19-
* @param {Function} fn
20-
*/
21-
function Hook(title, fn) {
22-
Runnable.call(this, title, fn);
23-
this.type = "hook";
24-
}
7+
class Hook extends Runnable {
8+
/**
9+
* Initialize a new `Hook` with the given `title` and callback `fn`
10+
*
11+
* @extends Runnable
12+
* @param {String} title
13+
* @param {Function} fn
14+
*/
15+
constructor(title, fn) {
16+
super(title, fn);
17+
this.type = "hook";
18+
}
2519

26-
/**
27-
* Inherit from `Runnable.prototype`.
28-
*/
29-
inherits(Hook, Runnable);
20+
/**
21+
* Resets the state for a next run.
22+
*/
23+
reset() {
24+
super.reset(this);
25+
delete this._error;
26+
}
3027

31-
/**
32-
* Resets the state for a next run.
33-
*/
34-
Hook.prototype.reset = function () {
35-
Runnable.prototype.reset.call(this);
36-
delete this._error;
37-
};
28+
/**
29+
* Get or set the test `err`.
30+
*
31+
* @memberof Hook
32+
* @public
33+
* @param {Error} err
34+
* @return {Error}
35+
*/
36+
error(err) {
37+
if (!arguments.length) {
38+
err = this._error;
39+
this._error = null;
40+
return err;
41+
}
3842

39-
/**
40-
* Get or set the test `err`.
41-
*
42-
* @memberof Hook
43-
* @public
44-
* @param {Error} err
45-
* @return {Error}
46-
*/
47-
Hook.prototype.error = function (err) {
48-
if (!arguments.length) {
49-
err = this._error;
50-
this._error = null;
51-
return err;
43+
this._error = err;
5244
}
5345

54-
this._error = err;
55-
};
46+
/**
47+
* Returns an object suitable for IPC.
48+
* Functions are represented by keys beginning with `$$`.
49+
* @private
50+
* @returns {Object}
51+
*/
52+
serialize() {
53+
return {
54+
$$currentRetry: this.currentRetry(),
55+
$$fullTitle: this.fullTitle(),
56+
$$isPending: Boolean(this.isPending()),
57+
$$titlePath: this.titlePath(),
58+
ctx:
59+
this.ctx && this.ctx.currentTest
60+
? {
61+
currentTest: {
62+
title: this.ctx.currentTest.title,
63+
[MOCHA_ID_PROP_NAME]: this.ctx.currentTest.id,
64+
},
65+
}
66+
: {},
67+
duration: this.duration,
68+
file: this.file,
69+
parent: {
70+
$$fullTitle: this.parent.fullTitle(),
71+
[MOCHA_ID_PROP_NAME]: this.parent.id,
72+
},
73+
state: this.state,
74+
title: this.title,
75+
type: this.type,
76+
[MOCHA_ID_PROP_NAME]: this.id,
77+
};
78+
}
79+
}
5680

57-
/**
58-
* Returns an object suitable for IPC.
59-
* Functions are represented by keys beginning with `$$`.
60-
* @private
61-
* @returns {Object}
62-
*/
63-
Hook.prototype.serialize = function serialize() {
64-
return {
65-
$$currentRetry: this.currentRetry(),
66-
$$fullTitle: this.fullTitle(),
67-
$$isPending: Boolean(this.isPending()),
68-
$$titlePath: this.titlePath(),
69-
ctx:
70-
this.ctx && this.ctx.currentTest
71-
? {
72-
currentTest: {
73-
title: this.ctx.currentTest.title,
74-
[MOCHA_ID_PROP_NAME]: this.ctx.currentTest.id,
75-
},
76-
}
77-
: {},
78-
duration: this.duration,
79-
file: this.file,
80-
parent: {
81-
$$fullTitle: this.parent.fullTitle(),
82-
[MOCHA_ID_PROP_NAME]: this.parent.id,
83-
},
84-
state: this.state,
85-
title: this.title,
86-
type: this.type,
87-
[MOCHA_ID_PROP_NAME]: this.id,
88-
};
89-
};
81+
module.exports = Hook;

0 commit comments

Comments
 (0)