Git Product home page Git Product logo

cloudflare-workers-testing's Introduction

Cloudflare Workers Testing

Test your Cloudflare Worker from the command line.

The Workers Testing package allows you to upload your script to the cloudflareworkers.com playground and query it from the command line, allowing you to test a worker before pushing it to production.

Install

npm install --save-dev workers-testing

Usage

Uploading

Edit your package.json to pipe the file you want to preview into the upload-worker cli, uploading it to the Workers Playground and saving a .cf-worker-id file to your home directory. The workers-testing function will read this file when it makes testing calls.

{
  "scripts": {
    "upload-worker": "upload-worker < my-script.js"
  },
  "dependencies": {
    "workers-testing": "^1.0.0"
  }
}
npm run upload-worker

Testing

Use a testing framework of your choice and import workers-testing. The function takes an object for an argument with the following keys:

{
  testUrl: /// The url with protocal that you'd like to run the test against
  method: /// The request method
  data: /// The data to be sent
}
var testWorker = require("workers-testing")
var expect = require("chai").expect

describe("Cloudflare Worker", function() {
  describe("GET request", function() {
    it("returns hello", async function() {
      const call = await testWorker({
        testUrl: "https://mydomain.com",
        method: "GET"
      })
      expect(call.data).to.equal("hello")
    })
  })

  describe("POST request", function() {
    it("returns whatever is posted to it", function(done) {
      testWorker({
        testUrl: "https://mydomain.com",
        method: "POST",
        data: "TESTING"
      }).then(res => {
        expect(res.data).to.equal("TESTING")
        done()
      })
    })
  })
})

You can also look at a worker in the browser by going to https://cloudflareworkers.com/#<YOUR WORKER ID HERE>:<WEBSITE TO TEST AGAINST HERE>. Be careful sharing this link with anyone, as they'll be able to see the contents of your worker.

cloudflare-workers-testing's People

Contributors

conzorkingkong avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.