Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions packages/edge-bundler/node/bundler.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Buffer } from 'buffer'
import { execSync } from 'node:child_process'
import { access, readdir, readFile, rm, writeFile } from 'fs/promises'
import { join, resolve } from 'path'
import process from 'process'
import { pathToFileURL } from 'url'

import { lt } from 'semver'
import { gte, lt } from 'semver'
import tmp from 'tmp-promise'
import { test, expect, vi, describe } from 'vitest'

import { importMapSpecifier } from '../shared/consts.js'
import { runESZIP, runTarball, useFixture } from '../test/util.js'
import { denoVersion, runESZIP, runTarball, useFixture } from '../test/util.js'

import { BundleError } from './bundle_error.js'
import { bundle, BundleOptions } from './bundler.js'
Expand Down Expand Up @@ -161,7 +160,7 @@
}
})

test('Uses the cache directory as the `DENO_DIR` value', async () => {

Check failure on line 163 in packages/edge-bundler/node/bundler.test.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-24.04, 22, v2.4.2)

node/bundler.test.ts > Uses the cache directory as the `DENO_DIR` value

Error: Test timed out in 30000ms. If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout". ❯ node/bundler.test.ts:163:1
expect.assertions(3)

const { basePath, cleanup, distPath } = await useFixture('with_import_maps')
Expand Down Expand Up @@ -599,7 +598,7 @@
await rm(vendorDirectory.path, { force: true, recursive: true })
})

test('Loads JSON modules', async () => {
test('Loads JSON modules with `with` attribute', async () => {
const { basePath, cleanup, distPath } = await useFixture('imports_json')
const sourceDirectory = join(basePath, 'functions')
const declarations: Declaration[] = [
Expand All @@ -626,6 +625,45 @@
await rm(vendorDirectory.path, { force: true, recursive: true })
})

// We can't run this on versions above 2.0.0 because the bundling will fail
// entirely, and what we're asserting here is that we emit a system log when
// import assertions are detected on successful builds. Also, running it on
// earlier versions won't work either, since those won't even show a warning.
test.skipIf(lt(denoVersion, '1.46.3') || gte(denoVersion, '2.0.0'))(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the current CI setup only runs in 1.39.0 and 2.2.4, so this test will not run in CI. Are we okay with this being a local development only test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I have explained in more detail here. The only option would be to add 1.46.3 to the matrix just for this test, but feels like an overkill.

'Emits a system log when import assertions are used',
async () => {
const { basePath, cleanup, distPath } = await useFixture('with_import_assert')
const sourceDirectory = join(basePath, 'functions')
const declarations: Declaration[] = [
{
function: 'func1',
path: '/func1',
},
]
const vendorDirectory = await tmp.dir()
const systemLogger = vi.fn()

await bundle([sourceDirectory], distPath, declarations, {
basePath,
systemLogger,
vendorDirectory: vendorDirectory.path,
})

const manifestFile = await readFile(resolve(distPath, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)
const bundlePath = join(distPath, manifest.bundles[0].asset)
const { func1 } = await runESZIP(bundlePath, vendorDirectory.path)

expect(func1).toBe(`{"foo":"bar"}`)
expect(systemLogger).toHaveBeenCalledWith(
`Edge function uses import assertions: ${join(sourceDirectory, 'func1.ts')}`,
)

await cleanup()
await rm(vendorDirectory.path, { force: true, recursive: true })
},
)

test('Supports TSX and process.env', async () => {
const { basePath, cleanup, distPath } = await useFixture('tsx')
const sourceDirectory = join(basePath, 'functions')
Expand Down Expand Up @@ -694,8 +732,6 @@
await cleanup()
})

const denoVersion = execSync('deno eval --no-lock "console.log(Deno.version.deno)"').toString()

describe.skipIf(lt(denoVersion, '2.4.2'))(
'Produces a tarball bundle',
() => {
Expand Down
9 changes: 7 additions & 2 deletions packages/edge-bundler/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ export const getFunctionConfig = async ({
// with the extractor.
const collector = await tmp.file()

// Retrieving the version of Deno.
const version = new SemVer((await deno.getBinaryVersion((await deno.getBinaryPath({ silent: true })).path)) || '')

// The extractor will use its exit code to signal different error scenarios,
// based on the list of exit codes we send as an argument. We then capture
// the exit code to know exactly what happened and guide people accordingly.
const version = new SemVer((await deno.getBinaryVersion((await deno.getBinaryPath({ silent: true })).path)) || '')

const { exitCode, stderr, stdout } = await deno.run(
[
'run',
Expand All @@ -121,6 +122,10 @@ export const getFunctionConfig = async ({
{ rejectOnExitCode: false },
)

if (stderr.includes('Import assertions are deprecated')) {
log.system(`Edge function uses import assertions: ${func.path}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any insight as to why the new test isn't detecting this message?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because Deno 1.39.0 (the lowest version we support, and used in the tests) doesn't even print the warning. 😢

}

if (exitCode !== ConfigExitCode.Success) {
handleConfigError(func, exitCode, stderr, log)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import dict from './dict.json' assert { type: "json" }


export default async () => Response.json(dict)
11 changes: 7 additions & 4 deletions packages/edge-bundler/test/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { promises as fs } from 'fs'
import { join, resolve } from 'path'
import { stderr, stdout } from 'process'
import { fileURLToPath, pathToFileURL } from 'url'
import { execSync } from 'node:child_process'
import { promises as fs } from 'node:fs'
import { join, resolve } from 'node:path'
import { stderr, stdout } from 'node:process'
import { fileURLToPath, pathToFileURL } from 'node:url'

import cpy from 'cpy'
import { execa } from 'execa'
Expand Down Expand Up @@ -169,3 +170,5 @@ export const runTarball = async (tarballPath: string) => {

return JSON.parse(result.stdout)
}

export const denoVersion = execSync('deno eval --no-lock "console.log(Deno.version.deno)"').toString()
Loading