diff --git a/.gitignore b/.gitignore index 464b428..97aed75 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ dist node_modules coverage -deno.lock .DS_Store .idea diff --git a/bun.lockb b/bun.lockb index ca944ef..e177726 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 39efcdd..5b1ccf9 100644 --- a/package.json +++ b/package.json @@ -69,12 +69,17 @@ "prepare": "git config --local core.hooksPath .githooks", "changelog": "gh-changelogen --repo=intlify/utils", "release": "bumpp --commit \"release: v%s\" --push --tag", + "fix": "run-p lint format", "lint": "deno lint", "format": "deno fmt", "build": "unbuild", "test": "npm run test:typecheck && npm run test:unit", - "test:unit": "vitest run", - "test:typecheck": "NODE_OPTIONS=--experimental-vm-modules vitest typecheck --run", + "test:unit": "NODE_OPTIONS=--experimental-vm-modules vitest run ./src", + "test:typecheck": "vitest typecheck --config ./vitest.type.config.ts --run", + "test:e2e": "run-s test:e2e:*", + "test:e2e:node": "npm run -w example-node test", + "test:e2e:deno": "cd playground/deno && deno task test", + "test:e2e:bun": "npm run -w example-bun test", "test:coverage": "npm test -- --reporter verbose --coverage", "play:browser": "npm run -w example-browser dev", "play:node": "npm run -w example-node dev", @@ -101,6 +106,7 @@ "h3": "^1.8.1", "hono": "^3.8.1", "lint-staged": "^15.0.0", + "npm-run-all": "^4.1.5", "miniflare": "^3.20231016.0", "supertest": "^6.3.3", "typescript": "^5.2.2", diff --git a/playground/bun/bun.spec.ts b/playground/bun/bun.spec.ts new file mode 100644 index 0000000..6f7f993 --- /dev/null +++ b/playground/bun/bun.spec.ts @@ -0,0 +1,17 @@ +import { expect, test } from 'bun:test' + +test('bun integration test', async () => { + const process = Bun.spawn(['bun', 'run', './index.ts'], { + stdin: 'inherit', + cwd: import.meta.dir, + }) + await Bun.sleep(1000) + + const req = new Request('http://localhost:8124') + req.headers.set('accept-language', 'en-US,en;q=0.9,ja;q=0.8') + const res = await fetch(req) + await Bun.sleep(1000) + + expect(await res.text()).toBe('detect locale: en-US') + process.kill() +}) diff --git a/playground/bun/index.ts b/playground/bun/index.ts index d8f4638..f942106 100644 --- a/playground/bun/index.ts +++ b/playground/bun/index.ts @@ -1,7 +1,6 @@ import { getHeaderLocale } from '@intlify/utils' const port = 8124 -// @ts-ignore: this is example Bun.serve({ port, fetch(req: Request) { diff --git a/playground/bun/package.json b/playground/bun/package.json index 2806fdb..c1e2f86 100644 --- a/playground/bun/package.json +++ b/playground/bun/package.json @@ -4,7 +4,8 @@ "module": "index.ts", "type": "module", "scripts": { - "dev": "bun run index.ts" + "dev": "bun run index.ts", + "test": "bun test" }, "devDependencies": { "bun-types": "latest" diff --git a/playground/deno/deno.jsonc b/playground/deno/deno.jsonc index 3c5130f..bcd1551 100644 --- a/playground/deno/deno.jsonc +++ b/playground/deno/deno.jsonc @@ -1,5 +1,6 @@ { "tasks": { - "dev": "deno run --watch main.ts" + "dev": "deno run --watch main.ts", + "test": "deno test --allow-read=$(which deno) --allow-run --allow-net ./deno.spec.ts" } } diff --git a/playground/deno/deno.lock b/playground/deno/deno.lock new file mode 100644 index 0000000..41ad105 --- /dev/null +++ b/playground/deno/deno.lock @@ -0,0 +1,42 @@ +{ + "version": "3", + "redirects": { + "https://esm.sh/@intlify/utils": "https://esm.sh/@intlify/utils@0.10.0" + }, + "remote": { + "https://deno.land/std@0.204.0/assert/_constants.ts": "8a9da298c26750b28b326b297316cdde860bc237533b07e1337c021379e6b2a9", + "https://deno.land/std@0.204.0/assert/_diff.ts": "58e1461cc61d8eb1eacbf2a010932bf6a05b79344b02ca38095f9b805795dc48", + "https://deno.land/std@0.204.0/assert/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7", + "https://deno.land/std@0.204.0/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee", + "https://deno.land/std@0.204.0/assert/assert_almost_equals.ts": "e15ca1f34d0d5e0afae63b3f5d975cbd18335a132e42b0c747d282f62ad2cd6c", + "https://deno.land/std@0.204.0/assert/assert_array_includes.ts": "6856d7f2c3544bc6e62fb4646dfefa3d1df5ff14744d1bca19f0cbaf3b0d66c9", + "https://deno.land/std@0.204.0/assert/assert_equals.ts": "d8ec8a22447fbaf2fc9d7c3ed2e66790fdb74beae3e482855d75782218d68227", + "https://deno.land/std@0.204.0/assert/assert_exists.ts": "407cb6b9fb23a835cd8d5ad804e2e2edbbbf3870e322d53f79e1c7a512e2efd7", + "https://deno.land/std@0.204.0/assert/assert_false.ts": "0ccbcaae910f52c857192ff16ea08bda40fdc79de80846c206bfc061e8c851c6", + "https://deno.land/std@0.204.0/assert/assert_greater.ts": "ae2158a2d19313bf675bf7251d31c6dc52973edb12ac64ac8fc7064152af3e63", + "https://deno.land/std@0.204.0/assert/assert_greater_or_equal.ts": "1439da5ebbe20855446cac50097ac78b9742abe8e9a43e7de1ce1426d556e89c", + "https://deno.land/std@0.204.0/assert/assert_instance_of.ts": "3aedb3d8186e120812d2b3a5dea66a6e42bf8c57a8bd927645770bd21eea554c", + "https://deno.land/std@0.204.0/assert/assert_is_error.ts": "c21113094a51a296ffaf036767d616a78a2ae5f9f7bbd464cd0197476498b94b", + "https://deno.land/std@0.204.0/assert/assert_less.ts": "aec695db57db42ec3e2b62e97e1e93db0063f5a6ec133326cc290ff4b71b47e4", + "https://deno.land/std@0.204.0/assert/assert_less_or_equal.ts": "5fa8b6a3ffa20fd0a05032fe7257bf985d207b85685fdbcd23651b70f928c848", + "https://deno.land/std@0.204.0/assert/assert_match.ts": "c4083f80600bc190309903c95e397a7c9257ff8b5ae5c7ef91e834704e672e9b", + "https://deno.land/std@0.204.0/assert/assert_not_equals.ts": "9f1acab95bd1f5fc9a1b17b8027d894509a745d91bac1718fdab51dc76831754", + "https://deno.land/std@0.204.0/assert/assert_not_instance_of.ts": "0c14d3dfd9ab7a5276ed8ed0b18c703d79a3d106102077ec437bfe7ed912bd22", + "https://deno.land/std@0.204.0/assert/assert_not_match.ts": "3796a5b0c57a1ce6c1c57883dd4286be13a26f715ea662318ab43a8491a13ab0", + "https://deno.land/std@0.204.0/assert/assert_not_strict_equals.ts": "ca6c6d645e95fbc873d25320efeb8c4c6089a9a5e09f92d7c1c4b6e935c2a6ad", + "https://deno.land/std@0.204.0/assert/assert_object_match.ts": "d8fc2867cfd92eeacf9cea621e10336b666de1874a6767b5ec48988838370b54", + "https://deno.land/std@0.204.0/assert/assert_rejects.ts": "45c59724de2701e3b1f67c391d6c71c392363635aad3f68a1b3408f9efca0057", + "https://deno.land/std@0.204.0/assert/assert_strict_equals.ts": "b1f538a7ea5f8348aeca261d4f9ca603127c665e0f2bbfeb91fa272787c87265", + "https://deno.land/std@0.204.0/assert/assert_string_includes.ts": "b821d39ebf5cb0200a348863c86d8c4c4b398e02012ce74ad15666fc4b631b0c", + "https://deno.land/std@0.204.0/assert/assert_throws.ts": "63784e951475cb7bdfd59878cd25a0931e18f6dc32a6077c454b2cd94f4f4bcd", + "https://deno.land/std@0.204.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", + "https://deno.land/std@0.204.0/assert/equal.ts": "9f1a46d5993966d2596c44e5858eec821859b45f783a5ee2f7a695dfc12d8ece", + "https://deno.land/std@0.204.0/assert/fail.ts": "c36353d7ae6e1f7933d45f8ea51e358c8c4b67d7e7502028598fe1fea062e278", + "https://deno.land/std@0.204.0/assert/mod.ts": "37c49a26aae2b254bbe25723434dc28cd7532e444cf0b481a97c045d110ec085", + "https://deno.land/std@0.204.0/assert/unimplemented.ts": "d56fbeecb1f108331a380f72e3e010a1f161baa6956fd0f7cf3e095ae1a4c75a", + "https://deno.land/std@0.204.0/assert/unreachable.ts": "4600dc0baf7d9c15a7f7d234f00c23bca8f3eba8b140286aaca7aa998cf9a536", + "https://deno.land/std@0.204.0/fmt/colors.ts": "c51c4642678eb690dcf5ffee5918b675bf01a33fba82acf303701ae1a4f8c8d9", + "https://esm.sh/@intlify/utils@0.10.0": "0fe85fa01c25d7db7f11169ff8988961fcdd7e336b40456647495cfcc3923837", + "https://esm.sh/v133/@intlify/utils@0.10.0/denonext/utils.mjs": "302b975d6ff52aa5bc2932463f1bf65e54b18d26d3ca486f13813bcd13baae31" + } +} diff --git a/playground/deno/deno.spec.ts b/playground/deno/deno.spec.ts new file mode 100644 index 0000000..096c4be --- /dev/null +++ b/playground/deno/deno.spec.ts @@ -0,0 +1,26 @@ +import { assertEquals } from 'https://deno.land/std@0.204.0/assert/mod.ts' + +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) + +Deno.test('deno integration test', async () => { + const command = new Deno.Command(Deno.execPath(), { + stdout: 'null', + args: [ + 'run', + '--allow-net', + './main.ts', + ], + }) + const process = command.spawn() + await sleep(1000) + + const req = new Request('http://localhost:8125') + req.headers.set('accept-language', 'en-US,en;q=0.9,ja;q=0.8') + const res = await fetch(req) + await sleep(1000) + + assertEquals('detect accpect-language: en-US,en,ja', await res.text()) + + process.kill('SIGINT') + await process.status +}) diff --git a/playground/deno/main.ts b/playground/deno/main.ts index a3b3c24..227b0ff 100644 --- a/playground/deno/main.ts +++ b/playground/deno/main.ts @@ -2,7 +2,6 @@ import { getHeaderLanguages } from 'https://esm.sh/@intlify/utils' const port = 8125 -// @ts-ignore: this is example Deno.serve({ port, }, (req: Request) => { diff --git a/playground/node/node.test.mjs b/playground/node/node.test.mjs new file mode 100644 index 0000000..d108533 --- /dev/null +++ b/playground/node/node.test.mjs @@ -0,0 +1,19 @@ +import assert from 'node:assert' +import test from 'node:test' +import { spawn } from 'node:child_process' +import { fetch } from 'ofetch' + +const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) + +test('node integration test', async () => { + const child = spawn('npx', ['tsx', './index.ts'], { cwd: process.cwd(), stdio: 'inherit' }) + await sleep(1000) + + const req = new Request('http://localhost:8123') + req.headers.set('accept-language', 'en-US,en;q=0.9,ja;q=0.8') + const res = await fetch(req) + await sleep(1000) + + assert.deepEqual('detect accpect-language: en-US,en,ja', await res.text()) + child.kill('SIGINT') +}) diff --git a/playground/node/package.json b/playground/node/package.json index a45c225..9f5e43e 100644 --- a/playground/node/package.json +++ b/playground/node/package.json @@ -1,13 +1,16 @@ { "name": "example-node", "private": true, + "type": "module", "scripts": { - "dev": "npx tsx index.ts" + "dev": "npx tsx index.ts", + "test": "node --test" }, "dependencies": { "@intlify/utils": "npm:@intlify/utils-edge@latest" }, "devDependencies": { + "ofetch": "^1.3.3", "@types/node": "^20.6.0", "typescript": "^5.2.2" } diff --git a/tsconfig.vitest.json b/tsconfig.vitest.json new file mode 100644 index 0000000..7c4dd51 --- /dev/null +++ b/tsconfig.vitest.json @@ -0,0 +1,4 @@ +{ + "extends": ["./tsconfig.json"], + "exclude": ["./playground"] +} diff --git a/vitest.config.ts b/vitest.config.ts index c7d146f..a93210a 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -3,8 +3,5 @@ import { defineConfig } from 'vitest/config' export default defineConfig({ test: { includeSource: ['src/**/*.{js,ts}'], - // typecheck: { - // ignoreSourceErrors: true, - // }, }, }) diff --git a/vitest.type.config.ts b/vitest.type.config.ts new file mode 100644 index 0000000..784fee5 --- /dev/null +++ b/vitest.type.config.ts @@ -0,0 +1,12 @@ +import { defineConfig } from 'vitest/config' +import config from './vitest.config.ts' + +export default defineConfig({ + ...config, + test: { + ...config.test, + typecheck: { + tsconfig: './tsconfig.vitest.json', + }, + }, +})