Skip to content

Commit

Permalink
Integrate Testing into Templates (cloudflare#132)
Browse files Browse the repository at this point in the history
* integrate testing

* integrate testing with vitest

* add tests for worker-router template
  • Loading branch information
lauragift21 authored Nov 15, 2022
1 parent 4ef1c46 commit 7e5f0cb
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 48 deletions.
5 changes: 2 additions & 3 deletions worker-aws/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name = "" # todo
workers_dev = true

name = "worker-aws"
main="index.js"
compatibility_date = "2022-05-03"

[build]
Expand Down
53 changes: 53 additions & 0 deletions worker-router/index.test.js
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);
}
});
});
4 changes: 3 additions & 1 deletion worker-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
"scripts": {
"deploy": "wrangler publish index.js",
"dev": "wrangler dev index.js --local",
"test": "vitest",
"start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev index.js --local"
},
"dependencies": {
"itty-router": "^2.6.1"
},
"devDependencies": {
"wrangler": "2.0.23"
"vitest": "^0.25.2",
"wrangler": "^2.2.2"
}
}
19 changes: 0 additions & 19 deletions worker-typescript/jest.config.json

This file was deleted.

17 changes: 3 additions & 14 deletions worker-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,12 @@
"scripts": {
"deploy": "wrangler publish src/index.ts",
"dev": "wrangler dev src/index.ts --local",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test": "vitest",
"start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev src/index.ts --local"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.0.0",
"@types/jest": "^28.1.6",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-typescript": "^3.0.0",
"jest": "^28.1.3",
"jest-environment-miniflare": "^2.6.0",
"miniflare": "^2.6.0",
"prettier": "^2.7.1",
"ts-jest": "^28.0.7",
"typescript": "^4.7.4",
"wrangler": "2.0.23"
"vitest": "^0.24.5",
"wrangler": "^2.1.14"
}
}
24 changes: 24 additions & 0 deletions worker-typescript/src/index.test.ts
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');
});
});
10 changes: 0 additions & 10 deletions worker-typescript/test/index.test.ts

This file was deleted.

22 changes: 22 additions & 0 deletions worker/index.test.js
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!"`);
}
});
});
4 changes: 3 additions & 1 deletion worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"version": "0.0.0",
"scripts": {
"deploy": "wrangler publish index.js",
"dev": "wrangler dev index.js --local"
"dev": "wrangler dev index.js --local",
"test": "vitest"
},
"devDependencies": {
"vitest": "^0.24.5",
"wrangler": "^2.0.0"
}
}

0 comments on commit 7e5f0cb

Please sign in to comment.