Help understanding the Netlify CLI `build` and `serve` commands

This question is mostly for my own edification at this point.

I have a site that I built with Gatsby and host/serve from Netlify. I’m testing out a new feature locally that makes use of Netlify functions.

Before deploying, in an effort to avoid build minutes and trouble shooting from the Netlify UI, I’m trying to use the Netlify CLI build and serve commands to make sure that some environment variables respond to different environments (duh, right?).

When I run netlify build --context production things behave as I would expect them to. I see the environment variable I’m monitoring (which is just the URL for my API endpoint) responding to the --context production flag – that is to say, it goes from http://localhost:8888/ to https://<URL for my site>. The build creates a public directory that I assume is what Netlify serves to people out on the World Wide Web.

To try to SERVE the output of running the build command, I’ve then tried running the netlify serve --dir public command. But what happens that I’m trying to understand is that the environment variable I’m monitoring behaves as if it’s in local development – that is to say, the aforementioned URL remains http://localhost:8888. I’ve tried running the command as netlify serve --context production --dir public as well, and the same thing seems to happen.

What I’m trying to ascertain: how would I mimic the build environment as precisely as possible on my local machine? In theory, serving the exact build would not allow me to actually test my function locally since the API endpoint would point to a function hosted somewhere on the web, but I would love to see basically a running version of my app locally that has an API endpoint that points to https://<URL for my site>.

Or another way to ask this question: Is the netlify serve command forcing the use of my .env.development environment variables for some reason? Is there a way for me to override this so I can see the use of my production environment variables reflected in some build-time console logging I’m doing to verify the values used for those variables?

Thanks!

If all you want is to test the Function, are you unable to run netlify dev? Not sure if I see a point in running the entire build just to test a Function. Is there something I’m missing?