-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: replace jest with vitest #24
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e57c9e
refactor: replace jest with vitest
jonathannorris 601b110
test: add workerd integration tests using @cloudflare/vitest-pool-wor…
jonathannorris 33fac0f
ci: add worker integration tests to CI pipeline
jonathannorris d9be6a5
fix: add @vitest/coverage-v8 dep and restructure test helper to asser…
jonathannorris 2d73903
fix: add pretest build step for worker integration tests
jonathannorris d485f1e
chore: use workspace name for test:worker and wildcard test include p…
jonathannorris 42c1560
chore: include test.ts files in vitest include pattern
jonathannorris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| declare module 'cloudflare:workers' { | ||
| interface ProvidedEnv { | ||
| FLAGS_R2_BUCKET?: R2Bucket; | ||
| FLAG_SOURCE?: string; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "extends": "../tsconfig.json", | ||
| "compilerOptions": { | ||
| "moduleResolution": "bundler", | ||
| "types": ["@cloudflare/vitest-pool-workers", "@cloudflare/workers-types"] | ||
| }, | ||
| "include": ["./**/*.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { exports } from 'cloudflare:workers'; | ||
|
|
||
| function postJson(path: string, body: unknown): Request { | ||
| return new Request(`http://localhost${path}`, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify(body), | ||
| }); | ||
| } | ||
|
|
||
| describe('static flag worker', () => { | ||
| describe('service endpoints', () => { | ||
| it('returns service info at root', async () => { | ||
| const response = await exports.default.fetch('http://localhost/'); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const body = await response.json(); | ||
| expect(body.status).toBe('ok'); | ||
| expect(body.service).toBe('flagd-ofrep-js-worker'); | ||
| expect(body.flagSource).toBe('static'); | ||
| }); | ||
|
|
||
| it('returns health check', async () => { | ||
| const response = await exports.default.fetch('http://localhost/health'); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const body = await response.json(); | ||
| expect(body.status).toBe('ok'); | ||
| }); | ||
|
|
||
| it('returns 404 for unknown paths', async () => { | ||
| const response = await exports.default.fetch('http://localhost/unknown'); | ||
| expect(response.status).toBe(404); | ||
| }); | ||
| }); | ||
|
|
||
| describe('single flag evaluation', () => { | ||
| it('evaluates a boolean flag', async () => { | ||
| const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags/simple-boolean', {})); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const body = await response.json(); | ||
| expect(body.key).toBe('simple-boolean'); | ||
| expect(body.value).toBe(false); | ||
| expect(body.variant).toBe('off'); | ||
| expect(body.reason).toBeDefined(); | ||
| }); | ||
|
|
||
| it('evaluates a string flag', async () => { | ||
| const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags/simple-string', {})); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const body = await response.json(); | ||
| expect(body.key).toBe('simple-string'); | ||
| expect(body.value).toBe('default-value'); | ||
| }); | ||
|
|
||
| it('evaluates with targeting context', async () => { | ||
| const response = await exports.default.fetch( | ||
| postJson('/ofrep/v1/evaluate/flags/targeted-boolean', { | ||
| context: { email: 'user@openfeature.dev' }, | ||
| }), | ||
| ); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const body = await response.json(); | ||
| expect(body.value).toBe(true); | ||
| expect(body.reason).toBe('TARGETING_MATCH'); | ||
| }); | ||
|
|
||
| it('returns 404 for non-existent flag', async () => { | ||
| const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags/does-not-exist', {})); | ||
| expect(response.status).toBe(404); | ||
|
|
||
| const body = await response.json(); | ||
| expect(body.errorCode).toBe('FLAG_NOT_FOUND'); | ||
| }); | ||
|
|
||
| it('defers disabled flags to code defaults', async () => { | ||
| const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags/disabled-flag', {})); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const body = await response.json(); | ||
| expect(body.key).toBe('disabled-flag'); | ||
| expect(body.reason).toBe('DISABLED'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('bulk evaluation', () => { | ||
| it('evaluates all flags', async () => { | ||
| const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags', {})); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const body = await response.json(); | ||
| expect(body.flags).toBeDefined(); | ||
| expect(Array.isArray(body.flags)).toBe(true); | ||
| expect(body.flags.length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it('includes metadata in bulk response', async () => { | ||
| const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags', {})); | ||
| const body = await response.json(); | ||
| expect(body.metadata).toBeDefined(); | ||
| expect(body.metadata.flagSetId).toBe('js-worker-example'); | ||
| }); | ||
|
|
||
| it('passes context to bulk evaluation', async () => { | ||
| const response = await exports.default.fetch( | ||
| postJson('/ofrep/v1/evaluate/flags', { | ||
| context: { email: 'user@openfeature.dev' }, | ||
| }), | ||
| ); | ||
| const body = await response.json(); | ||
|
|
||
| const targeted = body.flags.find((f: { key: string }) => f.key === 'targeted-boolean'); | ||
| expect(targeted).toBeDefined(); | ||
| expect(targeted.value).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('CORS', () => { | ||
| it('handles OPTIONS preflight on OFREP paths', async () => { | ||
| const response = await exports.default.fetch( | ||
| new Request('http://localhost/ofrep/v1/evaluate/flags/simple-boolean', { method: 'OPTIONS' }), | ||
| ); | ||
| expect(response.status).toBe(204); | ||
| expect(response.headers.get('Access-Control-Allow-Origin')).toBe('*'); | ||
| }); | ||
|
|
||
| it('includes CORS headers on evaluation responses', async () => { | ||
| const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags/simple-boolean', {})); | ||
| expect(response.headers.get('Access-Control-Allow-Origin')).toBe('*'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { cloudflareTest } from '@cloudflare/vitest-pool-workers'; | ||
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| cloudflareTest({ | ||
| wrangler: { configPath: './wrangler.toml' }, | ||
| }), | ||
| ], | ||
| test: { | ||
| globals: true, | ||
| include: ['test/**/*.spec.ts'], | ||
| }, | ||
| }); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.