Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added global-fixture example #101

Merged
merged 1 commit into from
Jan 2, 2025
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
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.");
}
});
});
Loading