Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Last active February 13, 2024 16:50
Show Gist options
  • Save cowboyd/70e4596e61aadafd852f403dc6d8ec6f to your computer and use it in GitHub Desktop.
Save cowboyd/70e4596e61aadafd852f403dc6d8ec6f to your computer and use it in GitHub Desktop.
A testing scope for Effection to help writing tests
import { run, type Operation } from "effection";
export interface TestScope {
/**
* Call from your runner's "beforeEach" or equivalent
*/
addSetup(op: () => Operation<void>): void;
/**
* Call from runner's `it()` or equivalent;
*/
runTest(op: () => Operation<void>): Promise<void>;
}
export function createTestScope(): TestScope {
let setup = [] as Array<() => Operation<void>>;
return {
addSetup(op) {
setup.push(op);
},
runTest(op) {
return run(function*() {
for (let step of setup) {
yield* step();
}
yield* op();
})
},
};
}
import * as bdd from "https://deno.land/[email protected]/testing/bdd.ts";
import { TestScope, createTestScope } from "./test-scope.ts";
import type { Operation } from "effection";
let scope: TestScope | void = void(0);
// Integrate test scope with the Deno test runner by wrapping the `describe()`, `it()` and `beforeEach()` methods.
function describeWithScope<T>(...args: bdd.DescribeArgs<T>): bdd.TestSuite<T> {
let [name, def] = args;
return bdd.describe(name as string, () => {
bdd.beforeEach(() => {
if (!scope) {
scope = createTestScope();
}
});
if (def && typeof def === "function") {
def();
}
});
}
describeWithScope.only = bdd.describe.only;
describeWithScope.ignore = bdd.describe.ignore;
export const describe: typeof bdd.describe = describeWithScope;
export function beforeEach(op: () => Operation<void>): void {
bdd.beforeEach(() => scope!.addSetup(op));
}
export function it(desc: string, op?: () => Operation<void>): void {
if (op) {
return bdd.it(desc, () => scope!.runTest(op));
} else {
return bdd.it.ignore(desc, () => {});
}
}
it.only = function only(desc: string, op?: () => Operation<void>): void {
if (op) {
return bdd.it.only(desc, () => scope!.runTest(op));
} else {
return bdd.it.ignore(desc, () => {});
}
};
import { createContext } from "effection";
import { describe, it, beforeEach } from "./02-deno.bdd.ts";
const context = createContext<string>("some-string");
describe("scenario", () => {
beforeEach(function*() {
yield* context.set("Hello World");
});
it("does something with context", function*() {
expect(yield* context).toEqual("Hello World");
})
})
@cowboyd
Copy link
Author

cowboyd commented Feb 13, 2024

@taras thanks! Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment