Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

docs: migrate shared behaviours to docs-next (#5432)

Kelechi Ebiri1dc4aa98eb37
diff --git a/docs-next/astro.config.ts b/docs-next/astro.config.ts
index c14d8328..4416b5e3 100644
--- a/docs-next/astro.config.ts
+++ b/docs-next/astro.config.ts
@@ -102,6 +102,10 @@ export default defineConfig({
label: "Run cycle overview",
slug: "explainers/run-cycle-overview",
},
+ {
+ label: "Shared behaviours",
+ slug: "explainers/shared-behaviours",
+ },
{ label: "Test duration", slug: "explainers/test-duration" },
{
label: "Test fixture decision tree",
diff --git a/docs-next/src/content/docs/explainers/shared-behaviours.mdx b/docs-next/src/content/docs/explainers/shared-behaviours.mdx
new file mode 100644
index 00000000..fde18337
--- /dev/null
+++ b/docs-next/src/content/docs/explainers/shared-behaviours.mdx
@@ -0,0 +1,79 @@
+---
+description: Reuse tests across contexts with `this`
+title: Shared Behaviours
+---
+
+Mocha currently has no concept of a "shared behaviour" however the "contexts" facilitate this feature. For example suppose you have an `Admin` which inherits from `User`, you most likely will not want to duplicate the `User` tests for `Admin`. The "context" (`this`) is the same object within the "before each", "after each" hooks, and the test-case itself, allowing you to utilize this instead of closures to store data. The following is an example of how you can achieve this sort of functionality:
+
+shared.js:
+
+```js
+exports.shouldBehaveLikeAUser = function() {
+ it('should have .name.first', function() {
+ this.user.name.first.should.equal('tobi');
+ })
+
+ it('should have .name.last', function() {
+ this.user.name.last.should.equal('holowaychuk');
+ })
+
+ describe('.fullname()', function() {
+ it('should return the full name', function() {
+ this.user.fullname().should.equal('tobi holowaychuk');
+ })
+ })
+};
+```
+
+test.js:
+
+```js
+var User = require('./user').User
+ , Admin = require('./user').Admin
+ , shared = require('./shared');
+
+describe('User', function() {
+ beforeEach(function() {
+ this.user = new User('tobi', 'holowaychuk');
+ })
+
+ shared.shouldBehaveLikeAUser();
+})
+
+describe('Admin', function() {
+ beforeEach(function() {
+ this.user = new Admin('tobi', 'holowaychuk');
+ })
+
+ shared.shouldBehaveLikeAUser();
+
+ it('should be an .admin', function() {
+ this.user.admin.should.be.true;
+ })
+})
+```
+
+user.js:
+
+```js
+exports.User = User;
+exports.Admin = Admin;
+
+function User(first, last) {
+ this.name = {
+ first: first,
+ last: last
+ };
+}
+
+User.prototype.fullname = function() {
+ return this.name.first + ' ' + this.name.last;
+};
+
+function Admin(first, last) {
+ User.call(this, first, last);
+ this.admin = true;
+}
+
+Admin.prototype.__proto__ = User.prototype;
+```
diff --git a/docs/api-tutorials/jsdoc.tutorials.json b/docs/api-tutorials/jsdoc.tutorials.json
index ecb4cfa7..383d3daa 100644
--- a/docs/api-tutorials/jsdoc.tutorials.json
+++ b/docs/api-tutorials/jsdoc.tutorials.json
@@ -4,5 +4,8 @@
},
"spies": {
"title": "Spies"
+ },
+ "shared-behaviours": {
+ "title": "Shared behaviours"
}
}
diff --git a/docs/api-tutorials/shared-behaviours.md b/docs/api-tutorials/shared-behaviours.md
new file mode 100644
index 00000000..f2788bb5
--- /dev/null
+++ b/docs/api-tutorials/shared-behaviours.md
@@ -0,0 +1,74 @@
+Mocha currently has no concept of a "shared behaviour" however the "contexts" facilitate this feature. For example suppose you have an `Admin` which inherits from `User`, you most likely will not want to duplicate the `User` tests for `Admin`. The "context" (`this`) is the same object within the "before each", "after each" hooks, and the test-case itself, allowing you to utilize this instead of closures to store data. The following is an example of how you can achieve this sort of functionality:
+
+shared.js:
+
+```js
+exports.shouldBehaveLikeAUser = function() {
+ it('should have .name.first', function() {
+ this.user.name.first.should.equal('tobi');
+ })
+
+ it('should have .name.last', function() {
+ this.user.name.last.should.equal('holowaychuk');
+ })
+
+ describe('.fullname()', function() {
+ it('should return the full name', function() {
+ this.user.fullname().should.equal('tobi holowaychuk');
+ })
+ })
+};
+```
+
+test.js:
+
+```js
+var User = require('./user').User
+ , Admin = require('./user').Admin
+ , shared = require('./shared');
+
+describe('User', function() {
+ beforeEach(function() {
+ this.user = new User('tobi', 'holowaychuk');
+ })
+
+ shared.shouldBehaveLikeAUser();
+})
+
+describe('Admin', function() {
+ beforeEach(function() {
+ this.user = new Admin('tobi', 'holowaychuk');
+ })
+
+ shared.shouldBehaveLikeAUser();
+
+ it('should be an .admin', function() {
+ this.user.admin.should.be.true;
+ })
+})
+```
+
+user.js:
+
+```js
+exports.User = User;
+exports.Admin = Admin;
+
+function User(first, last) {
+ this.name = {
+ first: first,
+ last: last
+ };
+}
+
+User.prototype.fullname = function() {
+ return this.name.first + ' ' + this.name.last;
+};
+
+function Admin(first, last) {
+ User.call(this, first, last);
+ this.admin = true;
+}
+
+Admin.prototype.__proto__ = User.prototype;
+```