Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use @cloudflare/vitest-pool-workers #32

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,554 changes: 798 additions & 756 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@
"devDependencies": {
"@adaptivelink/pops": "0.5.8",
"@cloudflare/kv-asset-handler": "0.3.1",
"@cloudflare/vitest-pool-workers": "0.1.2",
"@cloudflare/workers-types": "4.20240222.0",
"@nodecraft/eslint-config": "34.0.1",
"@types/sanitize-html": "2.11.0",
"@typescript-eslint/eslint-plugin": "7.0.2",
"@typescript-eslint/eslint-plugin": "7.2.0",
"eslint": "8.57.0",
"eslint-plugin-json": "3.1.0",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-unicorn": "51.0.1",
"sanitize-html": "2.12.1",
"typescript": "5.3.3",
"typescript": "5.4.2",
"validate-color": "2.2.4",
"vitest": "1.3.1",
"wrangler": "3.29.0"
"vitest": "1.3.0",
"wrangler": "3.34.2"
},
"engines": {
"node": ">=18"
Expand Down
35 changes: 11 additions & 24 deletions src/index.test.ts → test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
import { createExecutionContext, env } from 'cloudflare:test';
import {
afterAll,
beforeAll,
describe,
expect,
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 +25,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 +35,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 +108,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 {}
}
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": [
"src"
"src",
"**/*.config.ts"
],
"exclude": [
"node_modules"
]
}
16 changes: 16 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config';


export default defineWorkersConfig({
test: {
pool: '@cloudflare/vitest-pool-workers',
poolOptions: {
workers: {
isolatedStorage: true,
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
Loading