Start-up / shut-down tasks? #104
-
Is there any way to perform some tasks before the test-suite starts, and after it ends? (I don't mean before/after each test, but before/after the whole test-suite.) I'm launching an I managed to "emulate" a start-up task this way: import express from "express";
import type { Server } from "http";
let testServer: Promise<Server>; // 👈 singleton
export async function getTestServer(): Promise<Server> {
return testServer || (testServer = createTestServer()); // 👈 initialize singleton
}
async function createTestServer(): Promise<Server> {
return new Promise(resolve => {
const app = express();
// ... configure express...
const server = app.listen(options.port, () => resolve(server));
});
} But how do I shut down? I need to somehow wait for zora's test-runner finish, so I know when to shut down the test-server - for as long as it's up, the application doesn't end, which means the test hangs after it finishes. I could start it up and shut it down for every individual test, but that will slow things down a lot - and it's a stateless server, so there's no practical reason to do that. I'd like to reuse it across all tests. (incidentally, this is a good example of why I argued here that being required to explicitly kick off the test-runner would be "a good thing" - an obvious guess would have been something Is there still a way to manually create your test-harness? That bit appears to have vanished from the README with version 5. The code of course is still there, but doesn't seem to have anything for this. I could add a decorator around the reporter, I guess - but that seems a bit hacky and probably not really what that's for? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 17 replies
-
My solution for now is, I take control of the test-harness myself - I added a import { createHarness } from "zora";
export const harness = createHarness({});
export const { test } = harness; Calling In all of my tests, I In the test entry-point script, I can then manually import { harness } from "./harness";
import { createJSONReporter } from "zora";
import { getTestServer } from "./test-server"; // 👆 see above
(async () => {
await harness.report({
reporter: createJSONReporter(),
});
const server = await getTestServer();
await new Promise(resolve => server.close(resolve));
})(); Gets the job done. 👍 As discussed here) though, this honestly still leaves me wishing that the default harness didn't even exist - global state, and things executing without your starting them, is pretty confusing, and it seems unnecessary. Creating the harness and starting the reporter saves you literally two lines of code. Just my opinion, but I'd really like to see |
Beta Was this translation helpful? Give feedback.
My solution for now is, I take control of the test-harness myself - I added a
test.ts
:Calling
createHarness
disables the default, global harness.In all of my tests, I
import { test } from "./harness"
instead offrom "zora"
.In the test entry-point script, I can then manually
await
the harness: