index.spec.ts 1.1 KB

123456789101112131415161718192021222324
  1. import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
  2. import { describe, it, expect } from 'vitest';
  3. import worker from '../src/index';
  4. // For now, you'll need to do something like this to get a correctly-typed
  5. // `Request` to pass to `worker.fetch()`.
  6. const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;
  7. describe('Hello World worker', () => {
  8. it('responds with Hello World! (unit style)', async () => {
  9. const request = new IncomingRequest('http://example.com');
  10. // Create an empty context to pass to `worker.fetch()`.
  11. const ctx = createExecutionContext();
  12. const response = await worker.fetch(request, env, ctx);
  13. // Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
  14. await waitOnExecutionContext(ctx);
  15. expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
  16. });
  17. it('responds with Hello World! (integration style)', async () => {
  18. const response = await SELF.fetch('https://example.com');
  19. expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
  20. });
  21. });