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

Adding a polyfill for Suite#total() #114

Merged
merged 2 commits into from
Mar 27, 2024
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
22 changes: 18 additions & 4 deletions packages/client/src/serialization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,26 @@ describe("Client serialization", () => {
});

it("can serialize a Test", () => {
const suite = new Suite("root suite");
const test = new Test("test title");
test.parent = new Suite("root suite");
suite.addTest(test);

const result = serialize(test);
const parsed = parse(result);
expect(parsed).contain.keys("type", "__mocha_id__");
expect(parsed).contain.keys("type", "__mocha_id__", "$$fullTitle");
expect(parsed.$$fullTitle).equals("root suite test title");
});

it("can serialize a Suite", () => {
const suite = new Suite("root suite");
const test = new Test("test title");
suite.addTest(test);

const result = serialize(test.parent);
const parsed = parse(result);
expect(parsed).contain.keys("type", "__mocha_id__", "$$fullTitle", "$$total");
expect(parsed.$$fullTitle).equals("root suite");
expect(parsed.$$total).equals(1);
});

it("can serialize a Test with a parent Suite", () => {
Expand All @@ -29,8 +44,7 @@ describe("Client serialization", () => {
// eslint-disable-next-line no-console
console.log("Before hook ran");
});
test.parent = suite;
suite.tests = [ test ];
suite.addTest(test);
{
// Serializing the first time should return an object with $type and $properties
const result = serialize(test);
Expand Down
11 changes: 10 additions & 1 deletion packages/client/src/serialization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as flatted from "flatted";
import { Suite } from "mocha-remote-mocha";

import { extend } from "./debug";
const debug = extend("serialization");
Expand All @@ -17,7 +18,15 @@ function toJSON(value: Record<string, unknown>): Record<string, unknown> {
stack: stack ? filterStack(stack) : stack,
};
} else if (typeof value === "object" && typeof value.serialize === "function") {
return value.serialize();
const result = value.serialize();
if (value instanceof Suite) {
// Polyfill missing methods
Object.assign(result, {
"type": "suite",
"$$total": value.total(),
});
}
return result;
} else {
return value;
}
Expand Down
25 changes: 25 additions & 0 deletions packages/integration-tests/src/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,29 @@ describe("basic", () => {
});
});
});

it("expose the total number of tests on a Suite", async () => {
// Create a server - which is supposed to run in Node
server = new Server({ port: 0, autoRun: true });
await server.start();
// Create a client - which is supposed to run where the tests are running
client = new Client({
autoConnect: false,
url: server.url,
tests() {
it("tests one thing", () => {});
it("tests another thing", () => {});
}
});

const totalCount = new Promise<number>(resolve => {
const runner = server.run(() => {});
runner.once("suite", (suite) => {
resolve(suite.total());
});
});

await client.connect();
expect(await totalCount).equals(2);
});
});
Loading