How to test RPC Workers?

Is my approach wrong here?
// test/index.test.ts
import { env, createExecutionContext } from "cloudflare:test"
import { describe, it, expect } from "vitest"
import ShareBucket from "../src/index"

describe("R2 Bucket", () => {
let worker: ShareBucket | null = null

it("creates a worker", async () => {
const ctx = createExecutionContext()
worker = new ShareBucket(ctx, env)
expect(worker).toBeDefined()
})
})
// test/index.test.ts
import { env, createExecutionContext } from "cloudflare:test"
import { describe, it, expect } from "vitest"
import ShareBucket from "../src/index"

describe("R2 Bucket", () => {
let worker: ShareBucket | null = null

it("creates a worker", async () => {
const ctx = createExecutionContext()
worker = new ShareBucket(ctx, env)
expect(worker).toBeDefined()
})
})
// src/index.ts
import { api } from "./api"
import { WorkerEntrypoint } from "cloudflare:workers"

export default class ShareBucket extends WorkerEntrypoint<Env> {
async putObject(object: unknown) {
// ...
}

async getObject(key: string) {
// ...
}

async fetch(request: Request): Promise<Response> {
return api.state("env", this.env).state("ctx", this.ctx).fetch(request)
}
}
// src/index.ts
import { api } from "./api"
import { WorkerEntrypoint } from "cloudflare:workers"

export default class ShareBucket extends WorkerEntrypoint<Env> {
async putObject(object: unknown) {
// ...
}

async getObject(key: string) {
// ...
}

async fetch(request: Request): Promise<Response> {
return api.state("env", this.env).state("ctx", this.ctx).fetch(request)
}
}
With this setup, I receive vitest error:
TypeError: Failed to construct 'WorkerEntrypoint': constructor parameter 1 is not of type 'ExecutionContext'.
No description
2 Replies
Ian Schenck
Ian Schenck4mo ago
I am also running into this now. Same exact error and situation.
Sertan
Sertan2mo ago
Having the same issue. @Varna did you find a solution for this issue?
Want results from more Discord servers?
Add your server