-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement fetch and add local-mode-tests
- Loading branch information
Showing
9 changed files
with
120 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
feat: implement fetch for wrangler's unstable_dev API, and write our first integration test. | ||
|
||
Prior to this PR, users of `unstable_dev` had to provide their own fetcher, and guess the address and port that the wrangler dev server was using. | ||
|
||
With this implementation, it's now possible to test wrangler, using just wrangler (and a test framework): | ||
|
||
```js | ||
describe("worker", async () => { | ||
const worker = await wrangler.unstable_dev("src/index.ts"); | ||
|
||
const resp = await worker.fetch(); | ||
|
||
expect(resp).not.toBe(undefined); | ||
if (resp) { | ||
const text = await resp.text(); | ||
expect(text).toMatchInlineSnapshot(`"Hello World!"`); | ||
} | ||
|
||
worker.stop(); | ||
} | ||
``` | ||
Closes #1383 | ||
Closes #1384 | ||
Closes #1385 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,34 @@ | ||
{ | ||
"name": "local-mode-tests", | ||
"version": "1.0.0", | ||
"private": true, | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"check:type": "tsc", | ||
"test": "npx jest --forceExit" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^3.2.0" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"jest": { | ||
"restoreMocks": true, | ||
"testTimeout": 30000, | ||
"testRegex": ".*.(test|spec)\\.[jt]sx?$", | ||
"transform": { | ||
"^.+\\.c?(t|j)sx?$": [ | ||
"esbuild-jest", | ||
{ | ||
"sourcemap": true | ||
} | ||
] | ||
} | ||
} | ||
"name": "local-mode-tests", | ||
"version": "1.0.0", | ||
"private": true, | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"check:type": "tsc", | ||
"test": "npx jest --forceExit" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^3.2.0" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"jest": { | ||
"restoreMocks": true, | ||
"testTimeout": 30000, | ||
"testRegex": ".*.(test|spec)\\.[jt]sx?$", | ||
"transformIgnorePatterns": [ | ||
"node_modules/(?!find-up|locate-path|p-locate|p-limit|p-timeout|p-queue|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream|get-port|supports-color|pretty-bytes)", | ||
"wrangler-dist/cli.js" | ||
], | ||
"transform": { | ||
"^.+\\.c?(t|j)sx?$": [ | ||
"esbuild-jest", | ||
{ | ||
"sourcemap": true | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default { | ||
async fetch() { | ||
return new Response("Hello World!"); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import wrangler from "wrangler"; | ||
|
||
describe("worker", () => { | ||
let worker: { | ||
fetch: (init?: RequestInit) => Promise<Response | undefined>; | ||
stop: () => void; | ||
}; | ||
|
||
beforeAll(async () => { | ||
//since the script is invoked from the directory above, need to specify index.js is in src/ | ||
worker = await wrangler.unstable_dev("src/basicModule.ts", { | ||
ip: "127.0.0.1", | ||
port: 1337, | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
worker.stop(); | ||
}); | ||
|
||
it("should invoke the worker and exit", async () => { | ||
const resp = await worker.fetch(); | ||
expect(resp).not.toBe(undefined); | ||
if (resp) { | ||
const text = await resp.text(); | ||
|
||
expect(text).toMatchInlineSnapshot(`"Hello World!"`); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module "wrangler"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters