Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: function:serve shows undefined port in url #4146

Merged
merged 10 commits into from
Feb 4, 2022
8 changes: 7 additions & 1 deletion src/lib/functions/netlify-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ class NetlifyFunction {
}

get url() {
return `http://localhost:${this.settings.port}/.netlify/functions/${this.name}`
// This line fixes the issue here https://github.com/netlify/cli/issues/4116
// Not sure why `settings.port` was used here nor does a valid reference exist.
// However, it remains here to serve whatever purpose for which it was added.
const port = this.settings.port || this.settings.functionsPort

const url = new URL(`/.netlify/functions/${this.name}`, `http://localhost:${port}`)
return url.href
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/integration/20.command.functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const getPort = require('get-port')
const waitPort = require('wait-port')

const fs = require('../../src/lib/fs')
const { NetlifyFunction } = require('../../src/lib/functions/netlify-function')

const callCli = require('./utils/call-cli')
const cliPath = require('./utils/cli-path')
Expand All @@ -21,6 +22,20 @@ const { withSiteBuilder } = require('./utils/site-builder')

const test = isCI ? avaTest.serial.bind(avaTest) : avaTest

test('should return the correct function url for a NetlifyFunction object', (t) => {
const port = 7331
const functionName = 'test-function'

const functionUrl = `http://localhost:${port}/.netlify/functions/${functionName}`

const ntlFunction = new NetlifyFunction({
name: functionName,
settings: { functionsPort: port },
})

t.is(ntlFunction.url, functionUrl)
})

test('should return function response when invoked with no identity argument', async (t) => {
await withSiteBuilder('function-invoke-with-no-identity-argument', async (builder) => {
builder.withNetlifyToml({ config: { functions: { directory: 'functions' } } }).withFunction({
Expand Down