Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
test: test in worker env
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSnow committed May 3, 2024
1 parent 173ac4e commit 3afed4f
Show file tree
Hide file tree
Showing 11 changed files with 886 additions and 18 deletions.
1 change: 1 addition & 0 deletions .moon/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ projects:
karbon-utils: packages/karbon-utils
playground: packages/playground
typesense-xior: packages/typesense-xior
worker-playground: packages/worker-playground

# Configures the version control system to utilize within the workspace. A VCS
# is required for determining touched (added, modified, etc) files, calculating file hashes,
Expand Down
1 change: 1 addition & 0 deletions moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ tasks:
- vitest.workspace.ts
deps:
- ~:build
- worker-playground:test-run # It require a different vitest version
test-coverage:
extends: test
outputs:
Expand Down
3 changes: 3 additions & 0 deletions packages/worker-playground/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# worker-playground

This is a playground to confirm some function implement are safe in worker
15 changes: 15 additions & 0 deletions packages/worker-playground/moon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
dependsOn:
- karbon-utils

tasks:
test:
command: vitest
inputs:
- tests/**/*
- vitest.config.ts
- wrangler.toml
deps:
- ^:build
test-run:
extends: test
args: run
17 changes: 17 additions & 0 deletions packages/worker-playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "worker-playground",
"private": true,
"packageManager": "[email protected]",
"scripts": {
"test": "moon run test --"
},
"dependencies": {
"@storipress/karbon-utils": "workspace:^"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "0.2.6",
"@cloudflare/workers-types": "^4.20240502.0",
"@tsconfig/node18": "^18.2.4",
"vitest": "1.3.0"
}
}
26 changes: 26 additions & 0 deletions packages/worker-playground/tests/encoding.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Buffer } from 'node:buffer'
import { describe, expect } from 'vitest'
import { fc, it } from '@fast-check/vitest'
import { base64ToUint8Array, textToUint8Array, uint8ArrayToBase64, uint8ArrayToText } from '@storipress/karbon-utils'

describe('textToUint8Array & uint8ArrayToText', () => {
it.prop({ text: fc.string() })('can convert text to and from uint8array', ({ text }) => {
const array = textToUint8Array(text)
const result = uint8ArrayToText(array)
expect(result).toBe(text)
})
})

describe('base64ToUint8Array', () => {
it.prop({ s: fc.string() })('can convert base64 to Uint8Array', ({ s }) => {
const base64 = Buffer.from(s).toString('base64')
expect(Buffer.from(base64ToUint8Array(base64)).toString()).toEqual(s)
})
})

describe('base64ToUint8Array & uint8ArrayToBase64', () => {
it.prop({ s: fc.string() })('can convert base64 string from and to uint8array', ({ s }) => {
const base64 = Buffer.from(s).toString('base64')
expect(uint8ArrayToBase64(base64ToUint8Array(base64))).toEqual(base64)
})
})
9 changes: 9 additions & 0 deletions packages/worker-playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@tsconfig/node18",
"compilerOptions": {
"lib": ["esnext"],
"module": "esnext",
"moduleResolution": "bundler",
"types": ["@cloudflare/workers-types/experimental", "@cloudflare/vitest-pool-workers"]
}
}
11 changes: 11 additions & 0 deletions packages/worker-playground/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'

export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: { configPath: './wrangler.toml' },
},
},
},
})
2 changes: 2 additions & 0 deletions packages/worker-playground/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
compatibility_date = '2024-01-01'
compatibility_flags = ['nodejs_compat']
2 changes: 1 addition & 1 deletion vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { defineWorkspace } from 'vitest/config'

export default defineWorkspace(['packages/*/vitest.config.ts'])
export default defineWorkspace(['packages/*/vitest.config.ts', '!packages/worker-playground/vitest.config.ts'])
Loading

0 comments on commit 3afed4f

Please sign in to comment.