Skip to content

Commit

Permalink
test: add test cases for using pattern and excludedPattern in edge fu…
Browse files Browse the repository at this point in the history
…nction inline config
  • Loading branch information
pieh committed Jul 2, 2024
1 parent 0a5eb09 commit 2f312cb
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
52 changes: 47 additions & 5 deletions packages/edge-bundler/node/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ test('Loads function paths from the in-source `config` function', async () => {
})
const generatedFiles = await fs.readdir(distPath)

expect(result.functions.length).toBe(7)
expect(result.functions.length).toBe(11)
expect(generatedFiles.length).toBe(2)

const manifestFile = await fs.readFile(resolve(distPath, 'manifest.json'), 'utf8')
Expand All @@ -251,7 +251,7 @@ test('Loads function paths from the in-source `config` function', async () => {
expect(bundles[0].format).toBe('eszip2')
expect(generatedFiles.includes(bundles[0].asset)).toBe(true)

expect(routes.length).toBe(6)
expect(routes.length).toBe(12)
expect(routes[0]).toEqual({
function: 'framework-func2',
pattern: '^/framework-func2/?$',
Expand All @@ -272,24 +272,54 @@ test('Loads function paths from the in-source `config` function', async () => {
path: '/framework-func1',
})
expect(routes[3]).toEqual({
function: 'framework-func3',
pattern: '^/framework-func3(/.*)?$',
excluded_patterns: ['^/framework-func3/excluded$'],
})
expect(routes[4]).toEqual({
function: 'framework-func4',
pattern: '^/framework-func4(/.*)?$',
excluded_patterns: ['^/framework-func4/excluded$', '^/framework-func4-alt/excluded$'],
})
expect(routes[5]).toEqual({
function: 'framework-func4',
pattern: '^/framework-func4-alt(/.*)?$',
excluded_patterns: ['^/framework-func4/excluded$', '^/framework-func4-alt/excluded$'],
})
expect(routes[6]).toEqual({
function: 'user-func1',
pattern: '^/user-func1/?$',
excluded_patterns: [],
path: '/user-func1',
})
expect(routes[4]).toEqual({
expect(routes[7]).toEqual({
function: 'user-func3',
pattern: '^/user-func3/?$',
excluded_patterns: [],
path: '/user-func3',
})
expect(routes[5]).toEqual({
expect(routes[8]).toEqual({
function: 'user-func5',
pattern: '^/user-func5(?:/(.*))/?$',
excluded_patterns: ['^/user-func5/excluded/?$'],
path: '/user-func5/*',
methods: ['GET'],
})
expect(routes[9]).toEqual({
function: 'user-func6',
pattern: '^/user-func6(/.*)?$',
excluded_patterns: ['^/user-func6/excluded$'],
})
expect(routes[10]).toEqual({
function: 'user-func7',
pattern: '^/user-func7(/.*)?$',
excluded_patterns: ['^/user-func7/excluded$', '^/user-func7-alt/excluded$'],
})
expect(routes[11]).toEqual({
function: 'user-func7',
pattern: '^/user-func7-alt(/.*)?$',
excluded_patterns: ['^/user-func7/excluded$', '^/user-func7-alt/excluded$'],
})

expect(postCacheRoutes.length).toBe(1)
expect(postCacheRoutes[0]).toEqual({
Expand All @@ -300,10 +330,22 @@ test('Loads function paths from the in-source `config` function', async () => {
methods: ['POST', 'PUT'],
})

expect(Object.keys(functionConfig)).toHaveLength(1)
expect(Object.keys(functionConfig)).toHaveLength(5)
expect(functionConfig['user-func5']).toEqual({
excluded_patterns: ['^/user-func5/excluded/?$'],
})
expect(functionConfig['user-func6']).toEqual({
excluded_patterns: ['^/user-func6/excluded$'],
})
expect(functionConfig['user-func7']).toEqual({
excluded_patterns: ['^/user-func7/excluded$', '^/user-func7-alt/excluded$'],
})
expect(functionConfig['framework-func3']).toEqual({
excluded_patterns: ['^/framework-func3/excluded$'],
})
expect(functionConfig['framework-func4']).toEqual({
excluded_patterns: ['^/framework-func4/excluded$', '^/framework-func4-alt/excluded$'],
})

await cleanup()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IntegrationsConfig } from 'https://edge.netlify.com'
import { greet } from 'alias:helper'

export default async () => {
const greeting = greet('framework function 3')

return new Response(greeting)
}

export const config: IntegrationsConfig = {
pattern: '/framework-func3(/.*)?',
excludedPattern: '/framework-func3/excluded',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IntegrationsConfig } from 'https://edge.netlify.com'
import { greet } from 'alias:helper'

export default async () => {
const greeting = greet('framework function 4')

return new Response(greeting)
}

export const config: IntegrationsConfig = {
pattern: ['/framework-func4(/.*)?', '/framework-func4-alt(/.*)?'],
excludedPattern: ['/framework-func4/excluded', '/framework-func4-alt/excluded'],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default async () => new Response('Hello from user function 6.')

export const config = {
pattern: '/user-func6(/.*)?',
excludedPattern: '/user-func6/excluded',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default async () => new Response('Hello from user function 7.')

export const config = {
pattern: ['/user-func7(/.*)?', '/user-func7-alt(/.*)?'],
excludedPattern: ['/user-func7/excluded', '/user-func7-alt/excluded']
}

0 comments on commit 2f312cb

Please sign in to comment.