I am really struggling to correctly setup a Miniflare Enviroment with a Cloudflare Worker on GitHub Actions so I can use Miniflare Jest to perform Jest testing.
So far, I have followed the guides completely and created the deploy scrip below:
name: CI/CD Workflow
on:
push:
branches:
- main
jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 14
- name: Install dependencies
run: npm ci
- name: Run Jest tests with Miniflare
run: |
npm run miniflare &
sleep 5
npm test
env:
MINIFLARE_SCRIPT: ../src/index.js
MINIFLARE_ADDRESS: http://127.0.0.1:8787
production:
needs: tests
runs-on: ubuntu-latest
if: needs.tests.result == 'success'
steps:
- uses: actions/checkout@v3
- name: Publish
uses: cloudflare/wrangler-action@2.0.0
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{ secrets.CF_ACCOUNT_ID }}
I have also correctly setup Jest config with a test directory with a simple fetch test:
import { handleRequest } from "../src/index.js";
test("responds with url", async () => {
const req = new Request("http://localhost/");
const res = await handleRequest(req);
expect(await res.text()).toBe("URL: http://localhost/ KEY: value");
});
This is my Jest config adding the Miniflare enviroment:
export default {
testEnvironment: "miniflare",
testEnvironmentOptions: {
bindings: { KEY: "value" }
},
testRegex: [
"**/tests/**/*.test.js"
],
};
However, when the actions run, under the testing stage, I receive this error:
Run npm run miniflare &
> worker-typescript-template@1.0.0 miniflare /home/runner/work/edge/edge
> npx miniflare ../src/index.ts
> worker-typescript-template@1.0.0 test /home/runner/work/edge/edge
> echo 'Error: no test specified'
Error: no test specified
My directory has:
tests/index.test.js as the example above shows.
I have no idea why Jest isn't finding the tests I have defined.
Below is my package.json:
{
"name": "worker-typescript-template",
"version": "1.0.0",
"description": "Cloudflare worker TypeScript template",
"main": "dist/worker.js",
"scripts": {
"dev": "wrangler dev --local",
"build": "node build.js",
"miniflare": "npx miniflare ../src/index.ts"
},
"author": "author",
"license": "MIT OR Apache-2.0",
"eslintConfig": {
"root": true,
"extends": [
"typescript"
]
},
"devDependencies": {
"@cloudflare/workers-types": "latest",
"@types/negotiator": "^0.6.1",
"@types/node": "^18.6.4",
"@typescript-eslint/eslint-plugin": "^4.16.1",
"@typescript-eslint/parser": "^4.16.1",
"eslint": "^7.21.0",
"eslint-config-prettier": "^8.1.0",
"eslint-config-typescript": "^3.0.0",
"jest": "^29.5.0",
"jest-environment-miniflare": "^2.14.0",
"miniflare": "^3.0.2",
"service-worker-mock": "^2.0.5",
"typescript": "^4.9.5",
"wrangler": "^3.1.1"
},
"dependencies": {
"@types/html-minifier": "^4.0.2",
"@types/service-worker-mock": "^2.0.1",
"esbuild": "^0.17.11",
"html-minifier": "^4.0.0",
"tslib": "^2.4.0"
}
}