Skip to content

Commit

Permalink
Fix helper signatures (#2394)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi authored Mar 23, 2024
1 parent 9eb53d3 commit b6ee13b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 51 deletions.
7 changes: 7 additions & 0 deletions .changeset/breezy-goats-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@effect/vitest": minor
---

Fix helper signatures removing support for passing effects as discriminating between effects and functions is not safe

Re-export vitest with patched "it"
3 changes: 1 addition & 2 deletions packages/effect/test/Effect/async.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as it from "effect-test/utils/extend"
import { assert, describe, it } from "effect-test/utils/extend"
import * as Cause from "effect/Cause"
import * as Chunk from "effect/Chunk"
import * as Deferred from "effect/Deferred"
Expand All @@ -10,7 +10,6 @@ import { pipe } from "effect/Function"
import * as Option from "effect/Option"
import * as Ref from "effect/Ref"
import * as Runtime from "effect/Runtime"
import { assert, describe } from "vitest"

describe("Effect", () => {
it.effect("simple async must return", () =>
Expand Down
53 changes: 31 additions & 22 deletions packages/experimental/test/RequestResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ describe("RequestResolver", () => {
storeId: "memory" | "kvs" | "lmdb",
layer: Layer.Layer<Persistence.ResultPersistence, unknown>
) =>
it.effect(
storeId,
it.effect(storeId, () =>
Effect.gen(function*(_) {
let count = 0
const baseResolver = RequestResolver.makeBatched((reqs: Array<MyRequest | TTLRequest>) => {
Expand Down Expand Up @@ -68,43 +67,55 @@ describe("RequestResolver", () => {

// ttl
let results = yield* _(
Effect.forEach(ReadonlyArray.range(-1, 3), (id) =>
Effect.exit(Effect.request(new TTLRequest({ id }), persisted)), {
batching: true
})
Effect.forEach(
ReadonlyArray.range(-1, 3),
(id) => Effect.exit(Effect.request(new TTLRequest({ id }), persisted)),
{
batching: true
}
)
)
assert.strictEqual(count, 10)
assert.strictEqual(results.length, 5)
assert(Exit.isFailure(results[0]))
assert(Exit.isSuccess(results[1]))

results = yield* _(
Effect.forEach(ReadonlyArray.range(-1, 3), (id) =>
Effect.exit(Effect.request(new TTLRequest({ id }), persisted)), {
batching: true
})
Effect.forEach(
ReadonlyArray.range(-1, 3),
(id) => Effect.exit(Effect.request(new TTLRequest({ id }), persisted)),
{
batching: true
}
)
)
assert.strictEqual(count, 10)
assert.strictEqual(results.length, 5)

yield* _(TestClock.adjust(1))

results = yield* _(
Effect.forEach(ReadonlyArray.range(-1, 3), (id) =>
Effect.exit(Effect.request(new TTLRequest({ id }), persisted)), {
batching: true
})
Effect.forEach(
ReadonlyArray.range(-1, 3),
(id) => Effect.exit(Effect.request(new TTLRequest({ id }), persisted)),
{
batching: true
}
)
)
assert.strictEqual(count, 11)
assert.strictEqual(results.length, 5)

yield* _(TestClock.adjust(5000))

results = yield* _(
Effect.forEach(ReadonlyArray.range(-1, 3), (id) =>
Effect.exit(Effect.request(new TTLRequest({ id }), persisted)), {
batching: true
})
Effect.forEach(
ReadonlyArray.range(-1, 3),
(id) => Effect.exit(Effect.request(new TTLRequest({ id }), persisted)),
{
batching: true
}
)
)
assert.strictEqual(count, 16)
assert.strictEqual(results.length, 5)
Expand All @@ -115,15 +126,13 @@ describe("RequestResolver", () => {
yield* _(store.clear)

users = yield* _(
Effect.forEach(ReadonlyArray.range(1, 5), (id) =>
Effect.request(new MyRequest({ id }), persisted), {
Effect.forEach(ReadonlyArray.range(1, 5), (id) => Effect.request(new MyRequest({ id }), persisted), {
batching: true
})
)
assert.strictEqual(count, 21)
assert.strictEqual(users.length, 5)
}).pipe(Effect.scoped, Effect.provide(layer))
)
}).pipe(Effect.scoped, Effect.provide(layer)))

testsuite("memory", Persistence.layerResultMemory)
testsuite("kvs", Persistence.layerResultKeyValueStore.pipe(Layer.provide(KeyValueStore.layerMemory)))
Expand Down
62 changes: 35 additions & 27 deletions packages/vitest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ import * as V from "vitest"
*/
export type API = TestAPI<{}>

/**
* @since 1.0.0
*/
export const it: API = V.it

const TestEnv = TestEnvironment.TestContext.pipe(
Layer.provide(Logger.remove(Logger.defaultLogger))
)
Expand All @@ -33,14 +28,14 @@ const TestEnv = TestEnvironment.TestContext.pipe(
export const effect = (() => {
const f = <E, A>(
name: string,
self: Effect.Effect<A, E, TestServices.TestServices> | (() => Effect.Effect<A, E, TestServices.TestServices>),
self: (ctx: V.TaskContext<V.Test<{}>> & V.TestContext) => Effect.Effect<A, E, TestServices.TestServices>,
timeout: number | V.TestOptions = 5_000
) =>
it(
name,
() =>
(c) =>
pipe(
Effect.isEffect(self) ? self : Effect.suspend(self),
Effect.suspend(() => self(c)),
Effect.provide(TestEnv),
Effect.runPromise
),
Expand All @@ -49,29 +44,29 @@ export const effect = (() => {
return Object.assign(f, {
skip: <E, A>(
name: string,
self: Effect.Effect<A, E, TestServices.TestServices> | (() => Effect.Effect<A, E, TestServices.TestServices>),
self: (ctx: V.TaskContext<V.Test<{}>> & V.TestContext) => Effect.Effect<A, E, TestServices.TestServices>,
timeout = 5_000
) =>
it.skip(
name,
() =>
(c) =>
pipe(
Effect.isEffect(self) ? self : Effect.suspend(self),
Effect.suspend(() => self(c)),
Effect.provide(TestEnv),
Effect.runPromise
),
timeout
),
only: <E, A>(
name: string,
self: Effect.Effect<A, E, TestServices.TestServices> | (() => Effect.Effect<A, E, TestServices.TestServices>),
self: (ctx: V.TaskContext<V.Test<{}>> & V.TestContext) => Effect.Effect<A, E, TestServices.TestServices>,
timeout = 5_000
) =>
it.only(
name,
() =>
(c) =>
pipe(
Effect.isEffect(self) ? self : Effect.suspend(self),
Effect.suspend(() => self(c)),
Effect.provide(TestEnv),
Effect.runPromise
),
Expand All @@ -85,14 +80,14 @@ export const effect = (() => {
*/
export const live = <E, A>(
name: string,
self: Effect.Effect<A, E> | (() => Effect.Effect<A, E>),
self: (ctx: V.TaskContext<V.Test<{}>> & V.TestContext) => Effect.Effect<A, E>,
timeout = 5_000
) =>
it(
name,
() =>
(c) =>
pipe(
Effect.isEffect(self) ? self : Effect.suspend(self),
Effect.suspend(() => self(c)),
Effect.runPromise
),
timeout
Expand Down Expand Up @@ -122,16 +117,16 @@ export const flakyTest = <A, E, R>(
*/
export const scoped = <E, A>(
name: string,
self:
| Effect.Effect<A, E, Scope.Scope | TestServices.TestServices>
| (() => Effect.Effect<A, E, Scope.Scope | TestServices.TestServices>),
self: (
ctx: V.TaskContext<V.Test<{}>> & V.TestContext
) => Effect.Effect<A, E, Scope.Scope | TestServices.TestServices>,
timeout = 5_000
) =>
it(
name,
() =>
(c) =>
pipe(
Effect.isEffect(self) ? self : Effect.suspend(self),
Effect.suspend(() => self(c)),
Effect.scoped,
Effect.provide(TestEnv),
Effect.runPromise
Expand All @@ -144,18 +139,31 @@ export const scoped = <E, A>(
*/
export const scopedLive = <E, A>(
name: string,
self:
| Effect.Effect<A, E, Scope.Scope>
| (() => Effect.Effect<A, E, Scope.Scope>),
self: (ctx: V.TaskContext<V.Test<{}>> & V.TestContext) => Effect.Effect<A, E, Scope.Scope>,
timeout = 5_000
) =>
it(
name,
() =>
(c) =>
pipe(
Effect.isEffect(self) ? self : Effect.suspend(self),
Effect.suspend(() => self(c)),
Effect.scoped,
Effect.runPromise
),
timeout
)

const methods = { effect, live, flakyTest, scoped, scopedLive } as const

/**
* @since 1.0.0
*/
export const it: API & typeof methods = Object.assign(
V.it,
methods
)

/**
* @since 1.0.0
*/
export * from "vitest"

0 comments on commit b6ee13b

Please sign in to comment.