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

Commit 3afed4f

Browse files
committed
test: test in worker env
1 parent 173ac4e commit 3afed4f

File tree

11 files changed

+886
-18
lines changed

11 files changed

+886
-18
lines changed

.moon/workspace.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ projects:
2121
karbon-utils: packages/karbon-utils
2222
playground: packages/playground
2323
typesense-xior: packages/typesense-xior
24+
worker-playground: packages/worker-playground
2425

2526
# Configures the version control system to utilize within the workspace. A VCS
2627
# is required for determining touched (added, modified, etc) files, calculating file hashes,

moon.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ tasks:
1313
- vitest.workspace.ts
1414
deps:
1515
- ~:build
16+
- worker-playground:test-run # It require a different vitest version
1617
test-coverage:
1718
extends: test
1819
outputs:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# worker-playground
2+
3+
This is a playground to confirm some function implement are safe in worker
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
dependsOn:
2+
- karbon-utils
3+
4+
tasks:
5+
test:
6+
command: vitest
7+
inputs:
8+
- tests/**/*
9+
- vitest.config.ts
10+
- wrangler.toml
11+
deps:
12+
- ^:build
13+
test-run:
14+
extends: test
15+
args: run
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "worker-playground",
3+
"private": true,
4+
"packageManager": "yarn@4.1.1",
5+
"scripts": {
6+
"test": "moon run test --"
7+
},
8+
"dependencies": {
9+
"@storipress/karbon-utils": "workspace:^"
10+
},
11+
"devDependencies": {
12+
"@cloudflare/vitest-pool-workers": "0.2.6",
13+
"@cloudflare/workers-types": "^4.20240502.0",
14+
"@tsconfig/node18": "^18.2.4",
15+
"vitest": "1.3.0"
16+
}
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Buffer } from 'node:buffer'
2+
import { describe, expect } from 'vitest'
3+
import { fc, it } from '@fast-check/vitest'
4+
import { base64ToUint8Array, textToUint8Array, uint8ArrayToBase64, uint8ArrayToText } from '@storipress/karbon-utils'
5+
6+
describe('textToUint8Array & uint8ArrayToText', () => {
7+
it.prop({ text: fc.string() })('can convert text to and from uint8array', ({ text }) => {
8+
const array = textToUint8Array(text)
9+
const result = uint8ArrayToText(array)
10+
expect(result).toBe(text)
11+
})
12+
})
13+
14+
describe('base64ToUint8Array', () => {
15+
it.prop({ s: fc.string() })('can convert base64 to Uint8Array', ({ s }) => {
16+
const base64 = Buffer.from(s).toString('base64')
17+
expect(Buffer.from(base64ToUint8Array(base64)).toString()).toEqual(s)
18+
})
19+
})
20+
21+
describe('base64ToUint8Array & uint8ArrayToBase64', () => {
22+
it.prop({ s: fc.string() })('can convert base64 string from and to uint8array', ({ s }) => {
23+
const base64 = Buffer.from(s).toString('base64')
24+
expect(uint8ArrayToBase64(base64ToUint8Array(base64))).toEqual(base64)
25+
})
26+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "@tsconfig/node18",
3+
"compilerOptions": {
4+
"lib": ["esnext"],
5+
"module": "esnext",
6+
"moduleResolution": "bundler",
7+
"types": ["@cloudflare/workers-types/experimental", "@cloudflare/vitest-pool-workers"]
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'
2+
3+
export default defineWorkersConfig({
4+
test: {
5+
poolOptions: {
6+
workers: {
7+
wrangler: { configPath: './wrangler.toml' },
8+
},
9+
},
10+
},
11+
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
compatibility_date = '2024-01-01'
2+
compatibility_flags = ['nodejs_compat']

vitest.workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { defineWorkspace } from 'vitest/config'
22

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

0 commit comments

Comments
 (0)