-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from Timi-Duban/test-env-variables
Load test environment variables in Jest
- Loading branch information
Showing
4 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
*/ | ||
}; |