Giter Site home page Giter Site logo

Comments (2)

yusukebe avatar yusukebe commented on July 28, 2024

Hi @metrue

As far as I know, there are two ways:

edge-mock is used in benchmark/handleEvent. miniflare Jest Environment is used in example/blog

I tried both to test with FormData. minflare is better than edge-mock. miniflare reproduces Cloudflare Workers API faithfully. This tests will be passed:

  describe('FormData', () => {
    it('formData - application/x-www-form-urlencoded', async () => {
      app.post('/', async (c) => {
        const form = await c.req.formData()
        // console.log(c.req.headers.get('Content-Type'))
        // => application/x-www-form-urlencoded;charset=UTF-
        return c.text((form.get('aaa') || '').toString())
      })
      const searchParams = new URLSearchParams()
      searchParams.append('aaa', 'bbb')
      const req = new Request('https://localhost/', {
        method: 'POST',
        body: searchParams,
      })
      const res = await app.dispatch(req)
      expect(res.status).toBe(200)
      expect(await res.text()).toBe('bbb')
    })

    it('formData - multipart/form-data', async () => {
      app.post('/', async (c) => {
        const form = await c.req.formData()
        // console.log(c.req.headers.get('Content-Type'))
        // multipart/form-data; boundary=----formdata-undici-0.12093121571495491
        return c.text((form.get('aaa') || '').toString())
      })
      const formData = new FormData()
      formData.append('aaa', 'bbb')
      const req = new Request('https://localhost/', {
        method: 'POST',
        body: formData,
      })
      const res = await app.dispatch(req)
      expect(res.status).toBe(200)
      expect(await res.text()).toBe('bbb')
    })
  })
})

And, miniflare Jest Environment provides Environment. No need to call a method like makeServiceWorkerEnv().

Unfortunately, miniflare dump warning messages, but it will be resolved in the near future.

(node:73360) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(node:73360) ExperimentalWarning: buffer.Blob is an experimental feature. This feature could change at any time

I think miniflare Jest Environment is best. How about you?

from hono.

metrue avatar metrue commented on July 28, 2024

Yes, miniflare Jest Environment is better, let's go with it.

from hono.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.