Skip to content

Commit

Permalink
feat: added global-fixture example (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg authored Jan 2, 2025
1 parent 042b886 commit ec08b35
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Additional examples include:
- [Babel application](packages/babel/)
- [Browser](packages/browser/)
- [Express REST API](packages/express-rest-api/)
- [Global Fixture](packages/global-fixture/)
- [Karma](packages/karma/)
- [Node Sqlite 3 example](packages/node-sqlite3/)
- [Playwright application](packages/playwright/)
Expand Down
6 changes: 6 additions & 0 deletions packages/global-fixture/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Global Fixture Example

A straightforward example of using Mocha with a global setup and teardown fixture.

1. `npm install`
2. `npm run test`
30 changes: 30 additions & 0 deletions packages/global-fixture/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Server {
#count = 0;

#timer = setInterval(() => {
this.#tick();
}, 100);

constructor() {
this.#tick();
}

#tick() {
globalThis.fixtureCount = this.#count += 1;
}

teardown() {
clearInterval(this.#timer);
}
}

let server;

exports.mochaGlobalSetup = () => {
server = new Server();
};

exports.mochaGlobalTeardown = () => {
server.teardown();
server = undefined;
};
15 changes: 15 additions & 0 deletions packages/global-fixture/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "global-fixture",
"version": "1.0.0",
"description": "Global fixture example",
"scripts": {
"test": "mocha test.spec.js --require fixture.js"
},
"engines": {
"node": ">=10.0.0"
},
"license": "ISC",
"devDependencies": {
"mocha": "latest"
}
}
7 changes: 7 additions & 0 deletions packages/global-fixture/test.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe("example", () => {
it("reads from the global count", () => {
if (typeof globalThis.fixtureCount !== "number") {
throw new Error("Expected a globalThis.fixtureCount.");
}
});
});

0 comments on commit ec08b35

Please sign in to comment.