Giter Club home page Giter Club logo

miniflare-typescript-esbuild-jest's Introduction

Miniflare Example Project

This is an example Cloudflare Workers project that uses Miniflare for local development, TypeScript, esbuild for bundling, and Jest for testing, with Miniflare's custom Jest environment.

# Install dependencies
$ npm install
# Start local development server with live reload
$ npm run dev
# Start remote development server using wrangler
$ npm run dev:remote
# Run tests
$ npm test
# Run type checking
$ npm run types:check
# Deploy using wrangler
$ npm run deploy

miniflare-typescript-esbuild-jest's People

Contributors

lauragift21 avatar mrbbot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

miniflare-typescript-esbuild-jest's Issues

Jest: "Matcher error: received value must be a mock or spy function"

Hi! Thanks again for miniflare and this repo πŸ™

I'm unable to use jest mocks, at present (unrelated to #2 - I can import { jest } from '@jest/globals' fine πŸ‘ ).

When I add mock, or spy on a function, I get the error:

/**
  ● should spy on buildResponse

  expect(received).toHaveBeenCalledTimes(expected)

  Matcher error: received value must be a mock or spy function

  Received has type:  function
  Received has value: [Function buildResponse]

    17 |
    18 | test("should spy on buildResponse", async () => {
  > 19 |   expect(buildResponse).toHaveBeenCalledTimes(0);
       |                         ^
    20 |
    21 |   const res = buildResponse("not found", 404);
    22 |   expect(res.status).toBe(404);

    at ensureMockOrSpy (node_modules/expect/build/spyMatchers.js:1294:11)
    at Object.<anonymous> (test/response.spec.ts:19:25)
 */

I've got a repro for you here: https://github.com/ptim/miniflare-typescript-esbuild-jest/blob/repro%2Fjest--received-value-must-be-a-mock-or-spy-function/test/response.spec.ts#L23

It is:

  • based off master HEAD: fbf3fcb
  • running under node 17
  • running tests in the terminal, no vs-code extension trickery

I updated deps, confirmed that tests pass, then added the failing case, then updated to miniflare rc4, but the issue persists…

Cross referencing related miniflare release notes:

Switched back to CommonJS exports. A primary goal of Miniflare is to provide a fun developer experience. Currently, this is not the case when using Miniflare with a certain popular testing framework (cough, Jest, cough), because it and its ecosystem's support for ESM isn't great yet. This also makes it easier to upgrade from Miniflare 1. Node lets you import CommonJS modules in ES modules so this won't break existing code. Thanks @Kikobeats.
https://github.com/cloudflare/miniflare/releases/tag/v2.0.0-rc.4

AFAICT, this is related to jest vs ESM / esbuild config / ts-jest config, as the project continues to work beautifully in miniflare.

If I come up with anything, I'll report back πŸ‘ Thanks!

Snapshots in dist

With this approach snapshots are written to dist it could benefit from a snapshotResolver in jest.config.js

npm warns of vulnerabilities when using this template

The packages for this template are starting to get a little out-of-date, and causing npm to highlight some high-risk vulnerabilities when installing the dependencies.

Expected Behaviour

Using this template for starting a new project should bring latest versions of packages available.

Actual Behaviour

Using this template for starting a new project causes npm to immediately highlight vulnerabilities with the dependencies.

Steps to Reproduce the Problem

  1. Generate a new project using this template and point the terminal at the new project:
    npx wrangler generate new-project https://github.com/cloudflare/miniflare-typescript-esbuild-jest
    
    # then
    cd new-project
  2. Run npm install

Output

npm WARN deprecated [email protected]: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.

added 400 packages, and audited 401 packages in 7s

30 packages are looking for funding
  run `npm fund` for details

15 vulnerabilities (9 low, 6 high)

To address all issues, run:
  npm audit fix

Run `npm audit` for details.

add minify & compression

Just by mixing this template with graphql one, my worker size is already 0.98Mo minified.
since worker size limit is 1MB, I feel like this is needed.

Jest `--watch` doesn't pick up changes to DO definitions

Hey, this project was a huge help in understanding how to get set up with a miniflare Jest setup with TS and DOs and everything, thanks for that!

One thing I hit was that I can't live-reload the DO definitions. I use that a lot for development, and it works for the other pieces of the worker, just not the DO.

To reproduce:

git clone [email protected]:mrbbot/miniflare-typescript-esbuild-jest.git
cd miniflare-typescript-esbuild-jest
npm install
npm run test -- --watchAll

To see it working, go change a line like https://github.com/mrbbot/miniflare-typescript-esbuild-jest/blob/fcd5f82ed7a864650902adb490d6d5b8b8f6e33f/src/index.ts#L7 and see the tests immediately fail.

For the DO, changing a line like https://github.com/mrbbot/miniflare-typescript-esbuild-jest/blob/fcd5f82ed7a864650902adb490d6d5b8b8f6e33f/src/counter.ts#L12 does break the test, but requires you to restart npm run test -- --watchAll to see the failure happen.

My workaround is to rerun the whole suite on each change using chokidar-cli, which works but will be slow once I have a decent-sized app:

  "scripts": {
    "build": "node build.js",
    "dev": "miniflare --live-reload --debug",
    "test": "npm run build && node --experimental-vm-modules node_modules/jest/bin/jest.js",
    "types:check": "tsc && tsc -p test/tsconfig.json",
+   "test:watch": "chokidar 'src/**' 'test/**' -c 'npm run test' --initial"
  },

Tests fail with "Class Counter for Durable Object COUNTER not found"

Hi! Thanks for the miniflare and this setup πŸ™

Just looking to provide a minimal repro for another issue, and ran into this first… will investigate and offer a PR if I figure it out πŸ‘

Running on a clean install at fbf3fcb and node v16.13.0:

rm -rf node_modules && npm install && npm test
 FAIL  test/response.spec.ts
  ● Test suite failed to run

    DurableObjectError [ERR_CLASS_NOT_FOUND]: Class Counter for Durable Object COUNTER not found

      at DurableObjectsPlugin.reload (file:/Users/ptim/Documents/Projects/New-Atlas/source/miniflare-typescript-esbuild-jest/node_modules/@miniflare/durable-objects/src/plugin.ts:202:15)

 FAIL  test/index.spec.ts
  ● Test suite failed to run

    DurableObjectError [ERR_CLASS_NOT_FOUND]: Class Counter for Durable Object COUNTER not found

      at DurableObjectsPlugin.reload (file:/Users/ptim/Documents/Projects/New-Atlas/source/miniflare-typescript-esbuild-jest/node_modules/@miniflare/durable-objects/src/plugin.ts:202:15)

 FAIL  test/counter.spec.ts
  ● Test suite failed to run

    DurableObjectError [ERR_CLASS_NOT_FOUND]: Class Counter for Durable Object COUNTER not found

      at DurableObjectsPlugin.reload (file:/Users/ptim/Documents/Projects/New-Atlas/source/miniflare-typescript-esbuild-jest/node_modules/@miniflare/durable-objects/src/plugin.ts:202:15)

Using TOML with wrangler publish

Hi there, I realise this may be using pre-release code but I've started a project with this setup.

The issue is I can publish to dev with wrangler2 as such:
npx wrangler@beta publish {directory} --name {name}

But I can't publish to different environments and routes using the the TOML: npx wrangler@beta publish
The error I'm getting is TypeError: Cannot read properties of undefined (reading '1')

If I don't use the wrangler beta and run publish I get "unexpected token 'export'"

Should wrangler1 work with this project?
How have you been deploying this? As you have a TOML

Thanks for any help you can offer.

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.