Skip to content
Open
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
6 changes: 3 additions & 3 deletions docs-next/src/content/docs/declaring/dynamic-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ No special syntax is required--plain old JavaScript can be used to achieve funct
Take the following example:

```js
const assert = require("assert");
import assert from "node:assert";

function add(args) {
return args.reduce((prev, curr) => prev + curr, 0);
Expand Down Expand Up @@ -66,9 +66,9 @@ See also [`--delay`](/features/hooks#delayed-root-suite) for CommonJS modules wi

```js
// testfile.mjs
import assert from "assert";
import assert from "node:assert";

// top-level await: Node >= v14.8.0 with ESM test file
// top-level await: requires ESM test files (.mjs or "type": "module" in package.json)
const tests = await new Promise((resolve) => {
setTimeout(resolve, 5000, [
{ args: [1, 2], expected: 3 },
Expand Down
6 changes: 3 additions & 3 deletions docs-next/src/content/docs/declaring/inclusive-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ If a test needs an environment or configuration which cannot be detected beforeh
For example:

```js
it('should only test in the correct environment', function() {
it("should only test in the correct environment", function () {
if (/* check test environment */) {
// make assertions
} else {
Expand All @@ -66,7 +66,7 @@ To avoid confusion, do not execute further instructions in a test or hook after
Contrast the above test with the following code:

```js
it('should only test in the correct environment', function() {
it("should only test in the correct environment", function () {
if (/* check test environment */) {
// make assertions
} else {
Expand All @@ -85,7 +85,7 @@ A test should make an assertion or use `this.skip()`.
To skip _multiple_ tests in this manner, use `this.skip()` in a "before all" hook:

```js
before(function() {
before(function () {
if (/* check test environment */) {
// setup code
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ 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
import { equal } from "assert";
import { equal } from "node:assert";
import { loadPage } from "your-code";
const URL = `${process.env.APP_HOST}/${process.env.APP_URI}`;
describe('Test the page loads', () {
it('goes to URL', () => {
describe("Test the page loads", () => {
it("goes to URL", () => {
const page = loadPage(URL);
equal(page.url, URL);
});
Expand Down
6 changes: 3 additions & 3 deletions docs-next/src/content/docs/explainers/global-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Related issue [#1582](https://github.com/mochajs/mocha/issues/1582)
If your test manipulates global variables, a reasonable expectation is that you will clean up after yourself. This includes commonly called methods such as `process.stdout.write()` or `console.log()`.

```js
var expect = require("chai").expect;
import { expect } from "chai";

describe("my nice test", function () {
var write,
let write,
log,
output = "";

// restore process.stdout.write() and console.log() to their previous glory
var cleanup = function () {
const cleanup = function () {
process.stdout.write = write;
console.log = log;
output = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ For example:
```js
// test.mjs
import { add } from "./add.mjs";
import assert from "assert";
import assert from "node:assert";

it("should add to numbers from an es module", () => {
assert.equal(add(3, 5), 8);
Expand Down
14 changes: 7 additions & 7 deletions docs-next/src/content/docs/explainers/programmatic-usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ There are a lot of reasons why you might want to automate running the tests usin
Here is an example of using Mocha programmatically:

```javascript
var Mocha = require("mocha"),
fs = require("fs"),
path = require("path");
import Mocha from "mocha";
import fs from "node:fs";
import path from "node:path";

// Instantiate a Mocha instance.
var mocha = new Mocha();
const mocha = new Mocha();

var testDir = "some/dir/test";
const testDir = "some/dir/test";

// Add each .js file to the mocha instance
fs.readdirSync(testDir)
.filter(function (file) {
// Only keep the .js files
return file.substr(-3) === ".js";
return file.endsWith(".js");
})
.forEach(function (file) {
mocha.addFile(path.join(testDir, file));
Expand All @@ -48,7 +48,7 @@ There are two ways to set the options to run the tests.
Firstly, you can set these options in the constructor object:

```javascript
var mocha = new Mocha({
const mocha = new Mocha({
ui: "tdd",
reporter: "list",
});
Expand Down
46 changes: 22 additions & 24 deletions docs-next/src/content/docs/explainers/shared-behaviours.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Mocha currently has no concept of a "shared behaviour" however the "contexts" fa
shared.js:

```js
exports.shouldBehaveLikeAUser = function () {
export function shouldBehaveLikeAUser() {
it("should have .name.first", function () {
this.user.name.first.should.equal("tobi");
});
Expand All @@ -22,30 +22,29 @@ exports.shouldBehaveLikeAUser = function () {
this.user.fullname().should.equal("tobi holowaychuk");
});
});
};
}
```

test.js:

```js
var User = require("./user").User,
Admin = require("./user").Admin,
shared = require("./shared");
import { User, Admin } from "./user.js";
import { shouldBehaveLikeAUser } from "./shared.js";

describe("User", function () {
beforeEach(function () {
this.user = new User("tobi", "holowaychuk");
});

shared.shouldBehaveLikeAUser();
shouldBehaveLikeAUser();
});

describe("Admin", function () {
beforeEach(function () {
this.user = new Admin("tobi", "holowaychuk");
});

shared.shouldBehaveLikeAUser();
shouldBehaveLikeAUser();

it("should be an .admin", function () {
this.user.admin.should.be.true;
Expand All @@ -56,24 +55,23 @@ describe("Admin", function () {
user.js:

```js
exports.User = User;
exports.Admin = Admin;

function User(first, last) {
this.name = {
first: first,
last: last,
};
export class User {
constructor(first, last) {
this.name = {
first: first,
last: last,
};
}

fullname() {
return this.name.first + " " + this.name.last;
}
}

User.prototype.fullname = function () {
return this.name.first + " " + this.name.last;
};

function Admin(first, last) {
User.call(this, first, last);
this.admin = true;
export class Admin extends User {
constructor(first, last) {
super(first, last);
this.admin = true;
}
}

Admin.prototype.__proto__ = User.prototype;
```
12 changes: 6 additions & 6 deletions docs-next/src/content/docs/explainers/spies.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ title: Spies
Mocha does not come equipped with spies, though libraries like [Sinon](https://github.com/sinonjs/sinon) provide this behaviour if desired. The following is an example of Mocha utilizing Sinon and [Should.js](https://github.com/shouldjs/should.js) to test an EventEmitter:

```javascript
var sinon = require("sinon"),
EventEmitter = require("events").EventEmitter;
import sinon from "sinon";
import { EventEmitter } from "node:events";

describe("EventEmitter", function () {
describe("#emit()", function () {
it("should invoke the callback", function () {
var spy = sinon.spy(),
const spy = sinon.spy(),
emitter = new EventEmitter();

emitter.on("foo", spy);
Expand All @@ -21,7 +21,7 @@ describe("EventEmitter", function () {
});

it("should pass arguments to the callbacks", function () {
var spy = sinon.spy(),
const spy = sinon.spy(),
emitter = new EventEmitter();

emitter.on("foo", spy);
Expand All @@ -39,14 +39,14 @@ The following is the same test, performed without any special spy library, utili
describe("EventEmitter", function () {
describe("#emit()", function () {
it("should invoke the callback", function (done) {
var emitter = new EventEmitter();
const emitter = new EventEmitter();

emitter.on("foo", done);
emitter.emit("foo");
});

it("should pass arguments to the callbacks", function (done) {
var emitter = new EventEmitter();
const emitter = new EventEmitter();

emitter.on("foo", function (a, b) {
a.should.equal("bar");
Expand Down
12 changes: 6 additions & 6 deletions docs-next/src/content/docs/explainers/stub-stdout.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ If you want to stub `stdout` inside your own code (via `process.stdout.write` or
i.e.

```javascript
it('should do, but it do not', function() {
it("should do, but it do not", function () {
// user wants to hide output generated by console.log calls in fn 'foo'
console.log = function() {};
console.log = function () {};
foo();

expect(...)
expect(...);
});
```

Expand All @@ -22,12 +22,12 @@ 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');
it("will do", function () {
const consoleLogStub = sinon.stub(console, "log");
foo();
consoleLogStub.restore();

expect(...)
expect(...);
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ Here is a minimalistic sample reporter, which you can use by executing: `mocha -

```js
// my-reporter.js
var mocha = require("mocha");
module.exports = MyReporter;
import mocha from "mocha";

function MyReporter(runner) {
export default function MyReporter(runner) {
mocha.reporters.Base.call(this, runner);
var passes = 0;
var failures = 0;
let passes = 0;
let failures = 0;

runner.on("pass", function (test) {
passes++;
Expand Down
Loading