Skip to content
Merged
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
4 changes: 4 additions & 0 deletions docs-next/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
79 changes: 79 additions & 0 deletions docs-next/src/content/docs/explainers/shared-behaviours.mdx
Original file line number Diff line number Diff line change
@@ -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;
```
3 changes: 3 additions & 0 deletions docs/api-tutorials/jsdoc.tutorials.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
},
"spies": {
"title": "Spies"
},
"shared-behaviours": {
"title": "Shared behaviours"
}
}
74 changes: 74 additions & 0 deletions docs/api-tutorials/shared-behaviours.md
Original file line number Diff line number Diff line change
@@ -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;
```
Loading