如何使用wrangler v3测试来自Cloudflare服务的HTTP响应?

huangapple go评论81阅读模式
英文:

how should I test the http response from a Cloudflare service using wrangler v3?

问题

Q1: 我应该如何在一个 workerd nanoservice 中运行服务并在其他 nanoservices 中运行测试?如果是这样的话,我该如何从 config.capnp 中启动服务?

Q2: 在 unit-tests 示例中的 config.capnp 顶部暗示它可能会在某个时候自动生成。目前是否已经可以或者建议这样做?

Q3: 我应该使用服务绑定来从一个测试 nanoservice 向正在测试的服务发送 fetch 请求吗?

Q4: 在 https://blog.cloudflare.com/wrangler3/ 中提到 vitest 集成正在进行中。是否有一个练习/测试服务的示例?

在这个层面上进行测试非常常见,测试驱动开发强调预防性测试,因此我预计会有示例和文档可用。不幸的是,我的搜索结果中找到了很多关于 Miniflare v2 的信息,但没有找到关于最新 Wrangler 版本和 workerd 运行时的信息。

英文:

I have a Cloudflare service and would like to test its http responses. Best I can tell, I should be using wrangler v3's local workerd runtime. There is a sample but it is quite minimal so I have a few sub-questions:

Q1: Do I, somehow, run the service in one workerd nanoservice and run tests in other nanoservices? If so, how do I launch the service from the config.capnp?

Q2: The top of the config.capnp in the unit-tests sample suggests that it may be auto-generated at some point. Is this currently possible or advisable?

Q3: Should I use a service binding to send a fetch request from a test nanoservice to the service it is testing?

Q4: There is mention at https://blog.cloudflare.com/wrangler3/ that vitest integration is progressing. Is there an example of a test which exercises/exorcises a service?

Testing at this level is quite common and test-driven development places a focus on pre-emptive tests so I anticipate that examples and documentation are available. Regrettably, my search found a lot of information on Miniflare v2 but not for the latest Wrangler release and the workerd runtime.

答案1

得分: 1

Wrangler 提供了一个 unstable_dev() API,允许您以编程方式启动 Workers 并向它们发送 fetch 请求,以便您可以使用您喜欢的测试框架(如 Vitest)来断言响应是否符合您的预期。

有关可以传递给 unstable_dev() 的可用选项,请参阅 UnstableDevOptions

您所提到的 Vitest 集成是 Miniflare 环境,您可以在其中访问 KV/R2/等绑定,以确保您的 Worker 与其正常交互。在撰写本文时,这还没有在 Wrangler 3 中实现,但已列入计划,希望在未来几个月内推出。

英文:

Wrangler exposes an unstable_dev() API which lets you programmatically spin-up Workers and send fetch requests to them, so you can assert the responses are as you expect with your preference of testing framework such as Vitest.

https://developers.cloudflare.com/workers/wrangler/api/

For the available options you can pass to unstable_dev(), refer to UnstableDevOptions.

The Vitest integration you are referring to is the Miniflare environment, where you can access the KV/R2/etc bindings to assert that your Worker is interacting with those properly. That isn't back in Wrangler 3 at the time of writing, but is on the roadmap and will hopefully land in the coming months.

huangapple
  • 本文由 发表于 2023年7月20日 10:46:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定