-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added global-fixture example (#101)
- Loading branch information
1 parent
042b886
commit ec08b35
Showing
5 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
}); | ||
}); |