-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add edge-runtime environment https://github.com/vercel/edge-runtime * chore: apply code review * chore: ci * chore: ci * chore: add @edge-runtime/vm to peerDependencies * chore: lint Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com> Co-authored-by: Vladimir Sheremet <sheremet.va@icloud.com>
- Loading branch information
1 parent
4a95177
commit b947be4
Showing
13 changed files
with
122 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,24 @@ | ||
import { importModule } from 'local-pkg' | ||
import type { Environment } from '../../types' | ||
import { populateGlobal } from './utils' | ||
|
||
export default <Environment>({ | ||
name: 'edge-runtime', | ||
async setup(global) { | ||
const { EdgeVM } = await importModule('@edge-runtime/vm') as typeof import('@edge-runtime/vm') | ||
const vm = new EdgeVM({ | ||
extend: (context) => { | ||
context.global = context | ||
context.Buffer = Buffer | ||
return context | ||
}, | ||
}) | ||
const { keys, originals } = populateGlobal(global, vm.context, { bindFunctions: true }) | ||
return { | ||
teardown(global) { | ||
keys.forEach(key => delete global[key]) | ||
originals.forEach((v, k) => global[k] = v) | ||
}, | ||
} | ||
}, | ||
}) |
This file contains 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 |
---|---|---|
@@ -1,9 +1,19 @@ | ||
import node from './node' | ||
import jsdom from './jsdom' | ||
import happy from './happy-dom' | ||
import edge from './edge-runtime' | ||
|
||
export const environments = { | ||
node, | ||
jsdom, | ||
'happy-dom': happy, | ||
'edge-runtime': edge, | ||
} | ||
|
||
export const envs = Object.keys(environments) | ||
|
||
export const envPackageNames: Record<Exclude<keyof typeof environments, 'node'>, string> = { | ||
'jsdom': 'jsdom', | ||
'happy-dom': 'happy-dom', | ||
'edge-runtime': '@edge-runtime/vm', | ||
} |
This file contains 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 @@ | ||
{ | ||
"name": "@vitest/test-edge-runtime", | ||
"type": "module", | ||
"private": true, | ||
"scripts": { | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage" | ||
}, | ||
"devDependencies": { | ||
"@edge-runtime/vm": "1.1.0-beta.10", | ||
"@vitest/web-worker": "workspace:*", | ||
"vitest": "workspace:*" | ||
} | ||
} |
This file contains 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,19 @@ | ||
/** | ||
* @vitest-environment edge-runtime | ||
*/ | ||
import { describe, expect, it } from 'vitest' | ||
describe('edge runtime api', () => { | ||
it('TextEncoder references the same global Uint8Array constructor', () => { | ||
expect(new TextEncoder().encode('abc')).toBeInstanceOf(Uint8Array) | ||
}) | ||
|
||
it('allows to run fetch', async () => { | ||
const response = await fetch('https://vitest.dev') | ||
expect(response.status).toEqual(200) | ||
}) | ||
|
||
it('allows to run crypto', async () => { | ||
const array = new Uint32Array(10) | ||
expect(crypto.getRandomValues(array)).toHaveLength(array.length) | ||
}) | ||
}) |
This file contains 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,5 @@ | ||
import { expect, test } from 'vitest' | ||
|
||
test('node env should not have crypto', () => { | ||
expect(global).not.toHaveProperty('crypto') | ||
}) |
This file contains 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,7 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
test: { | ||
environment: 'node', | ||
}, | ||
}) |