Skip to content

Commit

Permalink
wip: vitest pool testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Cherry committed Feb 24, 2024
1 parent 957323e commit 87e96a5
Show file tree
Hide file tree
Showing 11 changed files with 833 additions and 579 deletions.
1,339 changes: 785 additions & 554 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"test:watch": "vitest watch"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7978720501/npm-package-cloudflare-vitest-pool-workers-5060",
"@adaptivelink/pops": "0.5.8",
"@cloudflare/kv-asset-handler": "0.3.1",
"@cloudflare/workers-types": "4.20240222.0",
Expand All @@ -43,7 +44,7 @@
"sanitize-html": "2.12.1",
"typescript": "5.3.3",
"validate-color": "2.2.4",
"vitest": "1.3.1",
"vitest": "1.3.0",
"wrangler": "3.29.0"
},
"engines": {
Expand Down
File renamed without changes.
33 changes: 11 additions & 22 deletions src/index.test.ts → test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createExecutionContext, env, waitOnExecutionContext } from 'cloudflare:test';

Check warning on line 1 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / test

'waitOnExecutionContext' is defined but never used
import {
afterAll,

Check warning on line 3 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / test

'afterAll' is defined but never used
beforeAll,

Check warning on line 4 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / test

'beforeAll' is defined but never used
Expand All @@ -6,30 +7,15 @@ import {
it,
test,
} from 'vitest';
import { unstable_dev } from 'wrangler';

import { getKeys } from './utils';

import type { UnstableDevWorker } from 'wrangler';
import worker from '../src/index';
import { getKeys } from '../src/utils';

describe('Worker', () => {
let worker: UnstableDevWorker;

beforeAll(async () => {
worker = await unstable_dev('src/index.ts', {
experimental: {
disableExperimentalWarning: true,
},
});
});

afterAll(async () => {
await worker.stop();
});

it('should return html landing page', async () => {
const req = new Request('https://example.com', { method: 'GET' });
const resp = await worker.fetch(req.url);
const ctx = createExecutionContext();
const resp = await worker.fetch(req, env, ctx);
expect(resp.status).toBe(200);

// check if html is returned
Expand All @@ -41,7 +27,8 @@ describe('Worker', () => {

it('should sanitize for XSS', async () => {
const req = new Request('https://example.com/api/?width=350&height=100&text=Hello%20World&bgColor=%22%3E%3Cscript%3Ealert(%22XSS%22);%3C/script%3E', { method: 'GET' });
const resp = await worker.fetch(req.url);
const ctx = createExecutionContext();
const resp = await worker.fetch(req, env, ctx);
expect(resp.status).toBe(200);

const text = await resp.text();
Expand All @@ -50,7 +37,8 @@ describe('Worker', () => {

it('should sanitize for CSS prop injection', async () => {
const req = new Request('https://example.com/api/?width=450&height=450&text=James&fontFamily=test;background:url(https://avatars.githubusercontent.com/u/856748?v=4)&textWrap=true', { method: 'GET' });
const resp = await worker.fetch(req.url);
const ctx = createExecutionContext();
const resp = await worker.fetch(req, env, ctx);
expect(resp.status).toBe(200);

const text = await resp.text();
Expand Down Expand Up @@ -122,7 +110,8 @@ describe('Worker', () => {
searchParams.set(key, String(query[key]));
}
const req = new Request(`https://example.com/api/?${searchParams.toString()}`, { method: 'GET' });
const resp = await worker.fetch(req.url);
const ctx = createExecutionContext();
const resp = await worker.fetch(req, env, ctx);
expect(resp.status).toBe(200);

const text = await resp.text();
Expand Down
2 changes: 1 addition & 1 deletion src/sanitizers.test.ts → test/sanitizers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
sanitizeNumber,
sanitizeString,
sanitizeStringForCss,
} from './sanitizers';
} from '../src/sanitizers';

describe('Sanitizers', () => {
it('number', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';

import { type Options, simpleSvgPlaceholder } from './simple-svg-placeholder';
import { type Options, simpleSvgPlaceholder } from '../src/simple-svg-placeholder';

describe('simpleSVGPlaceholder', () => {
const testOptions: Options[] = [
Expand Down
10 changes: 10 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": [
"@cloudflare/workers-types/experimental",
"@cloudflare/vitest-pool-workers"
]
},
"include": ["./**/*.ts"]
}
4 changes: 4 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Env } from '../src/types';
declare module 'cloudflare:test' {
interface ProvidedEnv extends Env {}
}
18 changes: 18 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineWorkersPoolOptions } from '@cloudflare/vitest-pool-workers/config';
import { defineConfig } from 'vitest/config';


export default defineConfig({
test: {
pool: '@cloudflare/vitest-pool-workers',
poolOptions: {
workers: defineWorkersPoolOptions({
isolatedStorage: true,
main: './src/index.ts',
wrangler: {
configPath: './wrangler.toml',
},
}),
},
},
});
1 change: 1 addition & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name = "placeholders-dev"
workers_dev = true
compatibility_date = "2023-12-09"
compatibility_flags = ["nodejs_compat"]
main = "./src/index.ts"

[site]
Expand Down

0 comments on commit 87e96a5

Please sign in to comment.