Skip to content

Commit dd9d0ee

Browse files
committed
test: add virtual cjs test
1 parent 57e8166 commit dd9d0ee

File tree

11 files changed

+105
-0
lines changed

11 files changed

+105
-0
lines changed

edge-runtime/lib/cjs.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { createRequire } from 'node:module'
2+
import { join, dirname, relative } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
5+
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'
6+
7+
import { registerCJSModules } from './cjs.ts'
8+
9+
Deno.test('Virtual CJS Module loader', async (t) => {
10+
const localRequire = createRequire(import.meta.url)
11+
const realRequireResult = localRequire('./fixture/cjs/entry.js') as Record<string, string>
12+
13+
const fixtureRoot = new URL('./fixture/cjs/', import.meta.url)
14+
const virtualRoot = new URL('file:///virtual-root/index.mjs')
15+
16+
const fixtureRootPath = fileURLToPath(fixtureRoot)
17+
const virtualRootPath = dirname(fileURLToPath(virtualRoot))
18+
19+
// load fixture into virtual CJS
20+
const virtualModules = new Map<string, string>()
21+
const decoder = new TextDecoder('utf-8')
22+
async function addVirtualModulesFromDir(dir: string) {
23+
const dirUrl = new URL('./' + dir, fixtureRoot)
24+
25+
for await (const dirEntry of Deno.readDir(dirUrl)) {
26+
const relPath = join(dir, dirEntry.name)
27+
if (dirEntry.isDirectory) {
28+
await addVirtualModulesFromDir(relPath + '/')
29+
} else if (dirEntry.isFile) {
30+
const fileURL = new URL('./' + dirEntry.name, dirUrl)
31+
virtualModules.set(relPath, decoder.decode(await Deno.readFile(fileURL)))
32+
}
33+
}
34+
}
35+
36+
await addVirtualModulesFromDir('')
37+
registerCJSModules(virtualRoot, virtualModules)
38+
39+
const virtualRequire = createRequire(virtualRoot)
40+
const virtualRequireResult = virtualRequire('./entry.js') as Record<string, string>
41+
42+
const expectedVirtualRequireResult = {
43+
entry: '/virtual-root/entry.js',
44+
packageRoot: '/virtual-root/node_modules/package/index.js',
45+
packageInternalModule: '/virtual-root/node_modules/package/internal-module.js',
46+
packageMainRoot: '/virtual-root/node_modules/package-main/not-index.js',
47+
packageMainInternalModule: '/virtual-root/node_modules/package-main/internal-module.js',
48+
} as Record<string, string>
49+
50+
// make sure we collect all the possible keys to spot any cases of potentially missing keys in one of the objects
51+
const allTheKeys = [
52+
...new Set([
53+
...Object.keys(expectedVirtualRequireResult),
54+
...Object.keys(realRequireResult),
55+
...Object.keys(virtualRequireResult),
56+
]),
57+
]
58+
59+
for (const key of allTheKeys) {
60+
const virtualValue = virtualRequireResult[key]
61+
const realValue = realRequireResult[key]
62+
63+
// values are filepaths, "real" require has actual file system paths, virtual ones has virtual paths starting with file:///virtual-root/
64+
// we compare remaining paths to ensure same relative paths are reported indicating that resolution works the same in
65+
// in real CommonJS and simulated one
66+
assertEquals(relative(fixtureRootPath, realValue), relative(virtualRootPath, virtualValue))
67+
}
68+
69+
// the main portion of testing functionality is in above assertions that compare real require and virtual one
70+
// below is additional explicit assertion mostly to make sure that test setup is correct
71+
assertEquals(virtualRequireResult, expectedVirtualRequireResult)
72+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!node_modules

edge-runtime/lib/fixture/cjs/entry.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// package with no `main` or `exports`
2+
const packageRoot = require('package') // should resolve to `index.js`
3+
const packageInternalModule = require('package/internal-module') // should resolve to `internal-module.js`
4+
5+
// package with `main`, but no `exports`
6+
const packageMainRoot = require('package-main') // should resolve to `not-index.js`
7+
const packageMainInternalModule = require('package-main/internal-module') // should resolve to `internal-module.js`
8+
9+
module.exports = {
10+
packageRoot,
11+
packageInternalModule,
12+
packageMainRoot,
13+
packageMainInternalModule,
14+
entry: __filename,
15+
}

edge-runtime/lib/fixture/cjs/node_modules/package-main/internal-module.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

edge-runtime/lib/fixture/cjs/node_modules/package-main/not-index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

edge-runtime/lib/fixture/cjs/node_modules/package-main/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

edge-runtime/lib/fixture/cjs/node_modules/package/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

edge-runtime/lib/fixture/cjs/node_modules/package/internal-module.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

edge-runtime/lib/fixture/cjs/node_modules/package/package.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "cjs-fixture",
3+
"private": true,
4+
"type": "commonjs"
5+
}

0 commit comments

Comments
 (0)