Skip to content

Commit

Permalink
fix: add browser e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Oct 23, 2023
1 parent 69ceed8 commit a1cde25
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"test:typecheck": "vitest typecheck --config ./vitest.type.config.ts --run",
"test:coverage": "npm test -- --reporter verbose --coverage",
"test:e2e": "run-s test:e2e:*",
"test:e2e:browser": "cd playground/browser && node --test",
"test:e2e:node": "cd playground/node && node --test",
"test:e2e:deno": "cd playground/deno && deno task test",
"test:e2e:bun": "cd playground/bun && npm run test",
Expand Down
39 changes: 39 additions & 0 deletions playground/browser/browser.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import assert from 'node:assert'
import test from 'node:test'
import { createServer } from 'node:http'
import playwright from 'playwright'
import handler from 'serve-handler'

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

async function getText(page, selector, options) {
return page.locator(selector, options).innerText()
}

test('browser integration test', async () => {
const server = createServer((request, response) => {
return handler(request, response)
})
server.listen(3000, () => {
console.log('Running at http://localhost:3000')
})

const cleanup = () => {
server.close()
}
process.on('SIGINT', cleanup)
process.on('SIGTERM', cleanup)
process.on('SIGQUIT', cleanup)
process.on('uncaughtException', cleanup)
await sleep(2000)

const browser = await playwright['chromium'].launch({ headless: true })
const page = await browser.newPage({})
await page.goto('http://localhost:3000')
const data = await getText(page, '#app')

assert(data === `isLocale('en-US'): true`)

browser.close()
server.close()
})
9 changes: 7 additions & 2 deletions playground/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "npx --no-install serve -l 3000"
"dev": "npx --no-install serve -l 3000",
"test": "node --test"
},
"devDependencies": {
"serve": "^14.2.1"
"serve": "^14.2.1",
"serve-handler": "^6.1.5"
},
"dependencies": {
"@intlify/utils": "npm:@intlify/utils-edge@latest"
}
}
2 changes: 1 addition & 1 deletion playground/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "node --test"
},
"dependencies": {
"@intlify/utils": "npm:@intlify/utils-edge@latest"
"@intlify/utils": "file:/Users/kazuya.kawaguchi/Projects/oss/intlify/utils/intlify-utils-0.10.0.tgz"
},
"peerDependencies": {
"typescript": "^5.2.2"
Expand Down
24 changes: 21 additions & 3 deletions scripts/replaceDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url))

type Platform = 'browser' | 'node' | 'deno' | 'bun'

async function replaceNodePlatform(platform: 'node' | 'bun', playgroundPath: string) {
async function replaceNodePlatform(platform: 'browser' | 'node' | 'bun', playgroundPath: string) {
const utilsPath = resolve(__dirname, '..')
const utilsPkg = await readPackageJSON(resolve(utilsPath, 'package.json'))
const utilsTgzPath = resolve(utilsPath, `intlify-utils-${utilsPkg.version}.tgz`)
Expand All @@ -22,6 +22,20 @@ async function replaceNodePlatform(platform: 'node' | 'bun', playgroundPath: str
return true
}

async function replaceBrowserPlatform(platform: 'browser', playgroundPath: string) {
if (!replaceNodePlatform(platform, playgroundPath)) {
return false
}
const codePath = resolve(playgroundPath, platform, 'index.js')
const code = await fs.readFile(codePath, 'utf-8')
const replacedCode = code.replace(
'https://esm.sh/@intlify/utils',
'./node_modules/@intlify/utils/dist/index.mjs',
)
await fs.writeFile(codePath, replacedCode, 'utf-8')
return true
}

async function replaceDenoPlatform(playgroundPath: string) {
const denoConfigPath = resolve(playgroundPath, 'deno', 'deno.jsonc')
const denoConfig = JSON.parse(await fs.readFile(denoConfigPath, 'utf-8')) as {
Expand All @@ -44,8 +58,12 @@ async function main() {
if (!await replaceDenoPlatform(playgroundPath)) {
console.error(`cannot replace '@intlify/utils' dependency on ${platform}`)
}
} else { // for browser
// TODO:
} else if (platform === 'browser') {
if (!await replaceBrowserPlatform(platform, playgroundPath)) {
console.error(`cannot replace '@intlify/utils' dependency on ${platform}`)
}
} else {
throw new Error(`no support '${platform}' platform`)
}
}
}
Expand Down

0 comments on commit a1cde25

Please sign in to comment.