Skip to content

Commit

Permalink
fix(command-functions): inject env variables to functions:serve (#2925)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah authored Jul 13, 2021
1 parent e459351 commit 321e2b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/commands/functions/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { flags: flagsLib } = require('@oclif/command')

const { startFunctionsServer } = require('../../lib/functions/server')
const Command = require('../../utils/command')
const { getSiteInformation, acquirePort } = require('../../utils/dev')
const { getSiteInformation, acquirePort, injectEnvVariables } = require('../../utils/dev')
const { getFunctionsDir } = require('../../utils/functions')

const DEFAULT_PORT = 9999
Expand All @@ -18,6 +18,8 @@ class FunctionsServeCommand extends Command {

const functionsDir = getFunctionsDir({ flags, config }, join('netlify', 'functions'))

await injectEnvVariables({ env: this.netlify.cachedConfig.env, log, site, warn })

const { siteUrl, capabilities, timeouts } = await getSiteInformation({
flags,
api,
Expand Down
24 changes: 24 additions & 0 deletions tests/command.functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,28 @@ test('should trigger background function from event', async (t) => {
})
})

test('should inject env variables', async (t) => {
await withSiteBuilder('site-with-env-function', async (builder) => {
await builder
.withNetlifyToml({
config: { build: { environment: { TEST: 'FROM_CONFIG_FILE' } }, functions: { directory: 'functions' } },
})
.withFunction({
path: 'echo-env.js',
handler: async () => ({
statusCode: 200,
// eslint-disable-next-line node/prefer-global/process
body: `${process.env.TEST}`,
}),
})
.buildAsync()

const port = await getPort()
await withFunctionsServer({ builder, args: ['--port', port], port }, async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/echo-env`).text()
t.is(response, 'FROM_CONFIG_FILE')
})
})
})

/* eslint-enable require-await */

1 comment on commit 321e2b2

@github-actions
Copy link

Choose a reason for hiding this comment

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

📊 Benchmark results

Package size: 330 MB

Please sign in to comment.