prisma-labs / konn

MIT License
89 stars 4 forks source link

Allow easy context extension #31

Open jasonkuhrt opened 2 years ago

jasonkuhrt commented 2 years ago

Perceived Problem

Extending context means that a sub-group of tests want to have the parent group context plus some more. Currently achieved via cumbersome means:

const ctx = konn.beforeEach(() => ({ a: 1 }))

describe('sub', () => {
  const ctx2 = konn().beforeEach(() => ctx2)
})

I fact this probably won't even work, consider the mixing of beforeAll and beforeEach

const ctx = konn.beforeEach(() => ({ a: 1 }))

describe('sub', () => {
  const ctx2 = konn().beforeAll(() => ctx2).beforeAll((ctx) => ctx.a /* <-- bug! (undefined at runtime, statically appears there) */ )
})

That looks like trouble.

Ideas / Proposed Solution(s)

A first class way to extend context:

const ctx = konn.beforeEach(() => ({ a: 1 }))

describe('sub', () => {
  const ctx2 = konn(ctx).beforeAll((ctx) => ctx.a /* <-- statically undefined, good, agrees with runtime */ )
})