Skip to content

armgit / mocha

Public Imported from GitHub
0

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

docs: migrate count assertions wiki page to docs (#5438)

Kelechi Ebiri02a306c6cbf3
diff --git a/docs-next/astro.config.ts b/docs-next/astro.config.ts
index 81ade308..146b587c 100644
--- a/docs-next/astro.config.ts
+++ b/docs-next/astro.config.ts
@@ -94,6 +94,10 @@ export default defineConfig({
label: "Detecting multiple calls to done()",
slug: "explainers/detecting-multiple-calls-to-done",
},
+ {
+ label: "Counting assertions",
+ slug: "explainers/count-assertions",
+ },
{
label: "Find global leaks",
slug: "explainers/find-global-leaks",
diff --git a/docs-next/src/content/docs/explainers/count-assertions.mdx b/docs-next/src/content/docs/explainers/count-assertions.mdx
new file mode 100644
index 00000000..638be4e6
--- /dev/null
+++ b/docs-next/src/content/docs/explainers/count-assertions.mdx
@@ -0,0 +1,52 @@
+---
+description: How to count assertions.
+title: Count assertions
+---
+
+While Mocha itself does not provide an assertion layer and cannot provide assertion counting, it's relatively easy to integrate this behavior using hooks. The following is a simplified version of an assertion counter:
+
+```js
+var expected = 0;
+var actual = 0;
+
+function assert(expr, msg) {
+ if (!expr) throw new Error(msg || 'assertion failed');
+ actual++;
+}
+
+function expect(n) {
+ expected = n;
+}
+
+function reset() {
+ expected = 0;
+ actual = 0;
+}
+
+function check() {
+ if (!expected || expected == actual) return;
+ var err = new Error('expected ' + expected + ' assertions, got ' + actual);
+ this.currentTest.emit('error', err);
+}
+
+beforeEach(reset);
+afterEach(check);
+
+describe('something', function () {
+ it('should work', function (done) {
+ expect(2);
+
+ setTimeout(function () {
+ assert('wahoo');
+ }, 50);
+
+ setTimeout(function () {
+ assert('hey');
+ }, 50);
+
+ setTimeout(function () {
+ done();
+ }, 100);
+ });
+});
+```
diff --git a/docs-next/src/content/docs/explainers/find-global-leak.mdx b/docs-next/src/content/docs/explainers/find-global-leak.mdx
new file mode 100644
index 00000000..6b910e6f
--- /dev/null
+++ b/docs-next/src/content/docs/explainers/find-global-leak.mdx
@@ -0,0 +1,16 @@
+---
+description: How to find a global leak.
+title: global link
+---
+
+Detecting which module is causing a global leak can be tedious. Add these lines at the very beginning of your tests, or in a file which is included via `--require`:
+
+```javascript
+Object.defineProperty(global, "name_of_leaking_property", {
+ set: function(value) {
+ throw new Error("Found the leak!");
+ }
+});
+```
+
+This will print a stacktrace that shows which module caused the leak. And if it's not yours - go open a pull request! :)
diff --git a/docs/api-tutorials/count-assertions.md b/docs/api-tutorials/count-assertions.md
new file mode 100644
index 00000000..78851cf4
--- /dev/null
+++ b/docs/api-tutorials/count-assertions.md
@@ -0,0 +1,47 @@
+While Mocha itself does not provide an assertion layer and cannot provide assertion counting, it's relatively easy to integrate this behavior using hooks. The following is a simplified version of an assertion counter:
+
+```js
+var expected = 0;
+var actual = 0;
+
+function assert(expr, msg) {
+ if (!expr) throw new Error(msg || 'assertion failed');
+ actual++;
+}
+
+function expect(n) {
+ expected = n;
+}
+
+function reset() {
+ expected = 0;
+ actual = 0;
+}
+
+function check() {
+ if (!expected || expected == actual) return;
+ var err = new Error('expected ' + expected + ' assertions, got ' + actual);
+ this.currentTest.emit('error', err);
+}
+
+beforeEach(reset);
+afterEach(check);
+
+describe('something', function () {
+ it('should work', function (done) {
+ expect(2);
+
+ setTimeout(function () {
+ assert('wahoo');
+ }, 50);
+
+ setTimeout(function () {
+ assert('hey');
+ }, 50);
+
+ setTimeout(function () {
+ done();
+ }, 100);
+ });
+});
+```
diff --git a/docs/api-tutorials/find-global-leak.md b/docs/api-tutorials/find-global-leak.md
new file mode 100644
index 00000000..2a61ea18
--- /dev/null
+++ b/docs/api-tutorials/find-global-leak.md
@@ -0,0 +1,11 @@
+Detecting which module is causing a global leak can be tedious. Add these lines at the very beginning of your tests, or in a file which is included via `--require`:
+
+```javascript
+Object.defineProperty(global, "name_of_leaking_property", {
+ set: function(value) {
+ throw new Error("Found the leak!");
+ }
+});
+```
+
+This will print a stacktrace that shows which module caused the leak. And if it's not yours - go open a pull request! :)
diff --git a/docs/api-tutorials/jsdoc.tutorials.json b/docs/api-tutorials/jsdoc.tutorials.json
index 7d630cc9..11e5ac63 100644
--- a/docs/api-tutorials/jsdoc.tutorials.json
+++ b/docs/api-tutorials/jsdoc.tutorials.json
@@ -1,4 +1,7 @@
{
+ "count-assertions": {
+ "title": "Count assertions"
+ },
"custom-reporter": {
"title": "Create a Custom Reporter"
},