Skip to content

Load test environment variables in Jest #115

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

Merged
merged 3 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_DATABSE: !!(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_DATABSE).toBeDefined()
})
})
8 changes: 5 additions & 3 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.info('Jest setupTests script will:');
console.log("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.log("Loading derived variables in Jest");
const envVariables = extendEnv({}).env;
Object.entries(envVariables).forEach(([varName, varValue]) => {
process.env[varName] = varValue;
});
*/
};