Skip to content

Commit

Permalink
Merge pull request #115 from Timi-Duban/test-env-variables
Browse files Browse the repository at this point in the history
Load test environment variables in Jest
  • Loading branch information
eric-burel authored Jun 24, 2021
2 parents 12cf6ab + 80b314b commit 5bd1d76
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = (phase, { defaultConfig }) => {
NEXT_PUBLIC_IS_USING_DEMO_DATABASE: !!(process.env.MONGO_URI || "").match(
/lbke\-demo/
),
NEXT_PUBLIC_IS_USING_LOCAL_DATABSE: !!(process.env.MONGO_URI || "").match(
NEXT_PUBLIC_IS_USING_LOCAL_DATABASE: !!(process.env.MONGO_URI || "").match(
/localhost/
),
};
Expand Down
68 changes: 68 additions & 0 deletions nextConfig/extendEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const { extendNextConfig } = require("../packages/@vulcanjs/next-config");
const flowRight = require("lodash/flowRight");
const debug = require("debug")("vns:next");
const packageJSON = require("../package.json");

// fooBar => FOO_BAR
const camelToTitle = (camelStr: string): string => {
return camelStr
.replace(/[A-Z]/g, " $1") // fooBar => foo Bar
.split(" ")
.map((t) => t.toUpperCase())
.join("_");
};

const withPkgInfo = (nextConfig) => {
// Public
// It's still unclear where such config should go
// @see https://github.com/vercel/next.js/discussions/14308
const publicPkgInfo = {
version: packageJSON.version,
};
if (!nextConfig.publicRuntimeConfig) nextConfig.publicRuntimeConfig = {};
nextConfig.publicRuntimeConfig.pkgInfo = publicPkgInfo;
// Also enhance environment with the same infos
Object.entries(publicPkgInfo).map(([key, value]) => {
const envKey = `NEXT_PUBLIC_PKGINFO_${camelToTitle(key)}`;
nextConfig.env[envKey] = `${value}`; // we convert to string
});

return nextConfig;
};


// @see https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration
module.exports = ({ defaultConfig }) => {
let extendedConfig;
extendedConfig = extendNextConfig(defaultConfig);

extendedConfig.env = {
NEXT_PUBLIC_IS_USING_DEMO_DATABASE: !!(process.env.MONGO_URI || "").match(
/lbke\-demo/
),
NEXT_PUBLIC_IS_USING_LOCAL_DATABASE: !!(process.env.MONGO_URI || "").match(
/localhost/
),
};

// Enable Webpack analyzer
if (process.env.ANALYZE && process.env.ANALYZE !== "false") {
const debug = require("debug")("webpack");
debug("Enabling Webpack bundle analyzer");
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
});
extendedConfig = withBundleAnalyzer(extendedConfig);
}

// To support markdown import
extendedConfig.pageExtensions = ["js", "jsx", "md", "mdx", "ts", "tsx"];
extendedConfig = flowRight([
withPkgInfo,
// add other wrappers here
])(extendedConfig);

debug("Extended next config FINAL " + JSON.stringify(extendedConfig));

return extendedConfig;
};
8 changes: 8 additions & 0 deletions tests/configTests/extendEnv.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe('Can access to env variables', () => {
test('access to .env.test', () => {
expect(process.env.NEXT_PUBLIC_GRAPHQL_URI).toBeDefined()
})
test('access to derived variables', () => {
expect(process.env.NEXT_PUBLIC_IS_USING_LOCAL_DATABASE).toBeDefined()
})
})
10 changes: 6 additions & 4 deletions tests/globalSetup.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const { loadEnvConfig } = require("@next/env");
const extendEnv = require('../nextConfig/extendEnv');

module.exports = async () => {
console.log("Loading environment variables in Jest from .env files");
console.info('Jest setupTests script will:');
console.info("Loading environment variables in Jest from .env files");
// @see https://github.com/vercel/next.js/issues/17903#issuecomment-708902413
await loadEnvConfig(process.env.PWD);
// Compute next.config env => it defines the constructed variables, so we need
// to run it in Jest for the config to work correctly
/*
const envVariables = extendEnv({}).env; // TODO: we need to refactor next.config.js slightly to extract this "extendEnv" code
console.info("Loading derived variables in Jest");
const envVariables = extendEnv({}).env;
Object.entries(envVariables).forEach(([varName, varValue]) => {
process.env[varName] = varValue;
});
*/
};

0 comments on commit 5bd1d76

Please sign in to comment.