forked from cloudflare/templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate Testing into Templates (cloudflare#132)
* integrate testing * integrate testing with vitest * add tests for worker-router template
- Loading branch information
1 parent
4ef1c46
commit 7e5f0cb
Showing
9 changed files
with
110 additions
and
48 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
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,53 @@ | ||
import { unstable_dev } from 'wrangler'; | ||
import { describe, expect, it, beforeAll, afterAll } from 'vitest'; | ||
|
||
describe('Worker', () => { | ||
let worker; | ||
|
||
beforeAll(async () => { | ||
worker = await unstable_dev('index.js', {}, { disableExperimentalWarning: true }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await worker.stop(); | ||
}); | ||
|
||
it('should return index route response', async () => { | ||
const resp = await worker.fetch('/'); | ||
if (resp) { | ||
const text = await resp.text(); | ||
expect(text).toMatchInlineSnapshot( | ||
`"Hello, world! This is the root page of your Worker template."` | ||
); | ||
} | ||
}); | ||
|
||
it('should return status 200 and encoded response', async () => { | ||
const resp = await worker.fetch('/example/hello'); | ||
if (resp) { | ||
const text = await resp.text(); | ||
expect(text).toContain('aGVsbG8'); | ||
expect(resp.status).toBe(200); | ||
} | ||
}); | ||
|
||
it('should return 200 and reponse object', async () => { | ||
const init = { | ||
body: { abc: 'def' }, | ||
method: 'POST', | ||
}; | ||
const resp = await worker.fetch('/post', init); | ||
const json = await resp.json(); | ||
if (resp) { | ||
expect(json).toEqual({ asn: 395747, colo: 'DFW' }); | ||
expect(resp.status).toBe(200); | ||
} | ||
}); | ||
|
||
it('should return 404 for undefined routes', async () => { | ||
const resp = await worker.fetch('/foobar'); | ||
if (resp) { | ||
expect(resp.status).toBe(404); | ||
} | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { unstable_dev } from 'wrangler'; | ||
import type { UnstableDevWorker } from 'wrangler'; | ||
import { describe, expect, it, beforeAll, afterAll } from 'vitest'; | ||
|
||
describe('Worker', () => { | ||
let worker: UnstableDevWorker; | ||
|
||
beforeAll(async () => { | ||
worker = await unstable_dev('src/index.ts', {}, { disableExperimentalWarning: true }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await worker.stop(); | ||
}); | ||
|
||
it('should return 200 response', async () => { | ||
const req = new Request('http://falcon', { method: 'GET' }); | ||
const resp = await worker.fetch(req); | ||
expect(resp.status).toBe(200); | ||
|
||
const text = await resp.text(); | ||
expect(text).toBe('request method: GET'); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,22 @@ | ||
import { unstable_dev } from 'wrangler'; | ||
import { describe, expect, it, beforeAll, afterAll } from 'vitest'; | ||
|
||
describe('Worker', () => { | ||
let worker; | ||
|
||
beforeAll(async () => { | ||
worker = await unstable_dev('index.js', {}, { disableExperimentalWarning: true }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await worker.stop(); | ||
}); | ||
|
||
it('should return Hello worker!', async () => { | ||
const resp = await worker.fetch(); | ||
if (resp) { | ||
const text = await resp.text(); | ||
expect(text).toMatchInlineSnapshot(`"Hello worker!"`); | ||
} | ||
}); | ||
}); |
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