Unverified Commit 47a61207 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update .env HMR handling (#39566)

parent 91d09bba
...@@ -13,6 +13,7 @@ export type LoadedEnvFiles = Array<{ ...@@ -13,6 +13,7 @@ export type LoadedEnvFiles = Array<{
let initialEnv: Env | undefined = undefined let initialEnv: Env | undefined = undefined
let combinedEnv: Env | undefined = undefined let combinedEnv: Env | undefined = undefined
let cachedLoadedEnvFiles: LoadedEnvFiles = [] let cachedLoadedEnvFiles: LoadedEnvFiles = []
let previousLoadedEnvFiles: LoadedEnvFiles = []
type Log = { type Log = {
info: (...args: any[]) => void info: (...args: any[]) => void
...@@ -49,7 +50,13 @@ export function processEnv( ...@@ -49,7 +50,13 @@ export function processEnv(
result = dotenvExpand(result) result = dotenvExpand(result)
if (result.parsed) { if (
result.parsed &&
!previousLoadedEnvFiles.some(
(item) =>
item.contents === envFile.contents && item.path === envFile.path
)
) {
log.info(`Loaded env from ${path.join(dir || '', envFile.path)}`) log.info(`Loaded env from ${path.join(dir || '', envFile.path)}`)
} }
...@@ -88,6 +95,7 @@ export function loadEnvConfig( ...@@ -88,6 +95,7 @@ export function loadEnvConfig(
return { combinedEnv, loadedEnvFiles: cachedLoadedEnvFiles } return { combinedEnv, loadedEnvFiles: cachedLoadedEnvFiles }
} }
process.env = Object.assign({}, initialEnv) process.env = Object.assign({}, initialEnv)
previousLoadedEnvFiles = cachedLoadedEnvFiles
cachedLoadedEnvFiles = [] cachedLoadedEnvFiles = []
const isTest = process.env.NODE_ENV === 'test' const isTest = process.env.NODE_ENV === 'test'
......
This diff is collapsed.
import type { webpack5 } from 'next/dist/compiled/webpack/webpack' import { webpack5 } from 'next/dist/compiled/webpack/webpack'
import type { NextConfigComplete } from '../config-shared' import type { NextConfigComplete } from '../config-shared'
import type { CustomRoutes } from '../../lib/load-custom-routes' import type { CustomRoutes } from '../../lib/load-custom-routes'
import { getOverlayMiddleware } from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware' import { getOverlayMiddleware } from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware'
...@@ -48,6 +48,7 @@ import ws from 'next/dist/compiled/ws' ...@@ -48,6 +48,7 @@ import ws from 'next/dist/compiled/ws'
import { promises as fs } from 'fs' import { promises as fs } from 'fs'
import { getPageStaticInfo } from '../../build/analysis/get-page-static-info' import { getPageStaticInfo } from '../../build/analysis/get-page-static-info'
import { serverComponentRegex } from '../../build/webpack/loaders/utils' import { serverComponentRegex } from '../../build/webpack/loaders/utils'
import { UnwrapPromise } from '../../lib/coalesced-function'
const wsServer = new ws.Server({ noServer: true }) const wsServer = new ws.Server({ noServer: true })
...@@ -159,8 +160,8 @@ export default class HotReloader { ...@@ -159,8 +160,8 @@ export default class HotReloader {
private distDir: string private distDir: string
private webpackHotMiddleware?: WebpackHotMiddleware private webpackHotMiddleware?: WebpackHotMiddleware
private config: NextConfigComplete private config: NextConfigComplete
private hasServerComponents: boolean public hasServerComponents: boolean
private hasReactRoot: boolean public hasReactRoot: boolean
public clientStats: webpack5.Stats | null public clientStats: webpack5.Stats | null
public serverStats: webpack5.Stats | null public serverStats: webpack5.Stats | null
public edgeServerStats: webpack5.Stats | null public edgeServerStats: webpack5.Stats | null
...@@ -176,6 +177,10 @@ export default class HotReloader { ...@@ -176,6 +177,10 @@ export default class HotReloader {
private hotReloaderSpan: Span private hotReloaderSpan: Span
private pagesMapping: { [key: string]: string } = {} private pagesMapping: { [key: string]: string } = {}
private appDir?: string private appDir?: string
public multiCompiler?: webpack5.MultiCompiler
public activeConfigs?: Array<
UnwrapPromise<ReturnType<typeof getBaseWebpackConfig>>
>
constructor( constructor(
dir: string, dir: string,
...@@ -452,6 +457,7 @@ export default class HotReloader { ...@@ -452,6 +457,7 @@ export default class HotReloader {
.traceChild('generate-webpack-config') .traceChild('generate-webpack-config')
.traceAsyncFn(() => .traceAsyncFn(() =>
Promise.all([ Promise.all([
// order is important here
getBaseWebpackConfig(this.dir, { getBaseWebpackConfig(this.dir, {
...commonWebpackOptions, ...commonWebpackOptions,
compilerType: COMPILER_NAMES.client, compilerType: COMPILER_NAMES.client,
...@@ -525,24 +531,23 @@ export default class HotReloader { ...@@ -525,24 +531,23 @@ export default class HotReloader {
}) })
} }
public async start(): Promise<void> { public async start(initial?: boolean): Promise<void> {
const startSpan = this.hotReloaderSpan.traceChild('start') const startSpan = this.hotReloaderSpan.traceChild('start')
startSpan.stop() // Stop immediately to create an artificial parent span startSpan.stop() // Stop immediately to create an artificial parent span
await this.clean(startSpan) if (initial) {
await this.clean(startSpan)
// Ensure distDir exists before writing package.json
await fs.mkdir(this.distDir, { recursive: true })
// Ensure distDir exists before writing package.json const distPackageJsonPath = join(this.distDir, 'package.json')
await fs.mkdir(this.distDir, { recursive: true }) // Ensure commonjs handling is used for files in the distDir (generally .next)
// Files outside of the distDir can be "type": "module"
const distPackageJsonPath = join(this.distDir, 'package.json') await fs.writeFile(distPackageJsonPath, '{"type": "commonjs"}')
}
// Ensure commonjs handling is used for files in the distDir (generally .next) this.activeConfigs = await this.getWebpackConfig(startSpan)
// Files outside of the distDir can be "type": "module"
await fs.writeFile(distPackageJsonPath, '{"type": "commonjs"}')
const configs = await this.getWebpackConfig(startSpan)
for (const config of configs) { for (const config of this.activeConfigs) {
const defaultEntry = config.entry const defaultEntry = config.entry
config.entry = async (...args) => { config.entry = async (...args) => {
// @ts-ignore entry is always a function // @ts-ignore entry is always a function
...@@ -679,14 +684,16 @@ export default class HotReloader { ...@@ -679,14 +684,16 @@ export default class HotReloader {
// Enable building of client compilation before server compilation in development // Enable building of client compilation before server compilation in development
// @ts-ignore webpack 5 // @ts-ignore webpack 5
configs.parallelism = 1 this.activeConfigs.parallelism = 1
const multiCompiler = webpack(configs) as unknown as webpack5.MultiCompiler this.multiCompiler = webpack(
this.activeConfigs
) as unknown as webpack5.MultiCompiler
watchCompilers( watchCompilers(
multiCompiler.compilers[0], this.multiCompiler.compilers[0],
multiCompiler.compilers[1], this.multiCompiler.compilers[1],
multiCompiler.compilers[2] this.multiCompiler.compilers[2]
) )
// Watch for changes to client/server page files so we can tell when just // Watch for changes to client/server page files so we can tell when just
...@@ -757,21 +764,21 @@ export default class HotReloader { ...@@ -757,21 +764,21 @@ export default class HotReloader {
} }
} }
multiCompiler.compilers[0].hooks.emit.tap( this.multiCompiler.compilers[0].hooks.emit.tap(
'NextjsHotReloaderForClient', 'NextjsHotReloaderForClient',
trackPageChanges(prevClientPageHashes, changedClientPages) trackPageChanges(prevClientPageHashes, changedClientPages)
) )
multiCompiler.compilers[1].hooks.emit.tap( this.multiCompiler.compilers[1].hooks.emit.tap(
'NextjsHotReloaderForServer', 'NextjsHotReloaderForServer',
trackPageChanges(prevServerPageHashes, changedServerPages) trackPageChanges(prevServerPageHashes, changedServerPages)
) )
multiCompiler.compilers[2].hooks.emit.tap( this.multiCompiler.compilers[2].hooks.emit.tap(
'NextjsHotReloaderForServer', 'NextjsHotReloaderForServer',
trackPageChanges(prevEdgeServerPageHashes, changedEdgeServerPages) trackPageChanges(prevEdgeServerPageHashes, changedEdgeServerPages)
) )
// This plugin watches for changes to _document.js and notifies the client side that it should reload the page // This plugin watches for changes to _document.js and notifies the client side that it should reload the page
multiCompiler.compilers[1].hooks.failed.tap( this.multiCompiler.compilers[1].hooks.failed.tap(
'NextjsHotReloaderForServer', 'NextjsHotReloaderForServer',
(err: Error) => { (err: Error) => {
this.serverError = err this.serverError = err
...@@ -779,7 +786,7 @@ export default class HotReloader { ...@@ -779,7 +786,7 @@ export default class HotReloader {
} }
) )
multiCompiler.compilers[2].hooks.done.tap( this.multiCompiler.compilers[2].hooks.done.tap(
'NextjsHotReloaderForServer', 'NextjsHotReloaderForServer',
(stats) => { (stats) => {
this.serverError = null this.serverError = null
...@@ -787,7 +794,7 @@ export default class HotReloader { ...@@ -787,7 +794,7 @@ export default class HotReloader {
} }
) )
multiCompiler.compilers[1].hooks.done.tap( this.multiCompiler.compilers[1].hooks.done.tap(
'NextjsHotReloaderForServer', 'NextjsHotReloaderForServer',
(stats) => { (stats) => {
this.serverError = null this.serverError = null
...@@ -820,7 +827,7 @@ export default class HotReloader { ...@@ -820,7 +827,7 @@ export default class HotReloader {
this.serverPrevDocumentHash = documentChunk.hash || null this.serverPrevDocumentHash = documentChunk.hash || null
} }
) )
multiCompiler.hooks.done.tap('NextjsHotReloaderForServer', () => { this.multiCompiler.hooks.done.tap('NextjsHotReloaderForServer', () => {
const serverOnlyChanges = difference<string>( const serverOnlyChanges = difference<string>(
changedServerPages, changedServerPages,
changedClientPages changedClientPages
...@@ -862,14 +869,14 @@ export default class HotReloader { ...@@ -862,14 +869,14 @@ export default class HotReloader {
} }
}) })
multiCompiler.compilers[0].hooks.failed.tap( this.multiCompiler.compilers[0].hooks.failed.tap(
'NextjsHotReloaderForClient', 'NextjsHotReloaderForClient',
(err: Error) => { (err: Error) => {
this.clientError = err this.clientError = err
this.clientStats = null this.clientStats = null
} }
) )
multiCompiler.compilers[0].hooks.done.tap( this.multiCompiler.compilers[0].hooks.done.tap(
'NextjsHotReloaderForClient', 'NextjsHotReloaderForClient',
(stats) => { (stats) => {
this.clientError = null this.clientError = null
...@@ -908,15 +915,15 @@ export default class HotReloader { ...@@ -908,15 +915,15 @@ export default class HotReloader {
) )
this.webpackHotMiddleware = new WebpackHotMiddleware( this.webpackHotMiddleware = new WebpackHotMiddleware(
multiCompiler.compilers this.multiCompiler.compilers
) )
let booted = false let booted = false
this.watcher = await new Promise((resolve) => { this.watcher = await new Promise((resolve) => {
const watcher = multiCompiler.watch( const watcher = this.multiCompiler?.watch(
// @ts-ignore webpack supports an array of watchOptions when using a multiCompiler // @ts-ignore webpack supports an array of watchOptions when using a multiCompiler
configs.map((config) => config.watchOptions!), this.activeConfigs.map((config) => config.watchOptions!),
// Errors are handled separately // Errors are handled separately
(_err: any) => { (_err: any) => {
if (!booted) { if (!booted) {
...@@ -928,7 +935,7 @@ export default class HotReloader { ...@@ -928,7 +935,7 @@ export default class HotReloader {
}) })
this.onDemandEntries = onDemandEntryHandler({ this.onDemandEntries = onDemandEntryHandler({
multiCompiler, multiCompiler: this.multiCompiler,
pagesDir: this.pagesDir, pagesDir: this.pagesDir,
appDir: this.appDir, appDir: this.appDir,
rootDir: this.dir, rootDir: this.dir,
...@@ -950,7 +957,13 @@ export default class HotReloader { ...@@ -950,7 +957,13 @@ export default class HotReloader {
// trigger invalidation to ensure any previous callbacks // trigger invalidation to ensure any previous callbacks
// are handled in the on-demand-entry-handler // are handled in the on-demand-entry-handler
getInvalidator()?.invalidate() if (!initial) {
this.invalidate()
}
}
public invalidate() {
return getInvalidator()?.invalidate()
} }
public async stop(): Promise<void> { public async stop(): Promise<void> {
...@@ -965,6 +978,7 @@ export default class HotReloader { ...@@ -965,6 +978,7 @@ export default class HotReloader {
) )
}) })
} }
this.multiCompiler = undefined
} }
public async getCompilationErrors(page: string) { public async getCompilationErrors(page: string) {
......
...@@ -72,6 +72,7 @@ import { ...@@ -72,6 +72,7 @@ import {
isMiddlewareFile, isMiddlewareFile,
NestedMiddlewareError, NestedMiddlewareError,
} from '../../build/utils' } from '../../build/utils'
import { getDefineEnv } from '../../build/webpack-config'
// Load ReactDevOverlay only when needed // Load ReactDevOverlay only when needed
let ReactDevOverlayImpl: React.FunctionComponent let ReactDevOverlayImpl: React.FunctionComponent
...@@ -391,8 +392,46 @@ export default class DevServer extends Server { ...@@ -391,8 +392,46 @@ export default class DevServer extends Server {
if (envChange) { if (envChange) {
this.loadEnvConfig({ dev: true, forceReload: true }) this.loadEnvConfig({ dev: true, forceReload: true })
await this.hotReloader?.stop()
await this.hotReloader?.start() this.hotReloader?.activeConfigs?.forEach((config, idx) => {
const isClient = idx === 0
const isNodeServer = idx === 1
const isEdgeServer = idx === 2
const hasRewrites =
this.customRoutes.rewrites.afterFiles.length > 0 ||
this.customRoutes.rewrites.beforeFiles.length > 0 ||
this.customRoutes.rewrites.fallback.length > 0
config.plugins?.forEach((plugin: any) => {
// we look for the DefinePlugin definitions so we can
// update them on the active compilers
if (
plugin &&
typeof plugin.definitions === 'object' &&
plugin.definitions.__NEXT_DEFINE_ENV
) {
const newDefine = getDefineEnv({
dev: true,
config: this.nextConfig,
distDir: this.distDir,
isClient,
hasRewrites,
hasReactRoot: this.hotReloader?.hasReactRoot,
isNodeServer,
isEdgeServer,
hasServerComponents: this.hotReloader?.hasServerComponents,
})
Object.keys(plugin.definitions).forEach((key) => {
if (!(key in newDefine)) {
delete plugin.definitions[key]
}
})
Object.assign(plugin.definitions, newDefine)
}
})
})
this.hotReloader?.invalidate()
} }
if (nestedMiddleware.length > 0) { if (nestedMiddleware.length > 0) {
...@@ -512,7 +551,7 @@ export default class DevServer extends Server { ...@@ -512,7 +551,7 @@ export default class DevServer extends Server {
}) })
await super.prepare() await super.prepare()
await this.addExportPathMapRoutes() await this.addExportPathMapRoutes()
await this.hotReloader.start() await this.hotReloader.start(true)
await this.startWatcher() await this.startWatcher()
this.setDevReady!() this.setDevReady!()
......
export default () => <p>{process.env.NEXT_PUBLIC_HELLO_WORLD}</p> export default function Page() {
return <p>{process.env.NEXT_PUBLIC_HELLO_WORLD}</p>
}
...@@ -27,7 +27,7 @@ const variables = [ ...@@ -27,7 +27,7 @@ const variables = [
'NEXT_PUBLIC_HELLO_WORLD', 'NEXT_PUBLIC_HELLO_WORLD',
] ]
export default async (req, res) => { export default async function handler(req, res) {
const items = { const items = {
nextConfigEnv: process.env.nextConfigEnv, nextConfigEnv: process.env.nextConfigEnv,
nextConfigPublicEnv: process.env.nextConfigPublicEnv, nextConfigPublicEnv: process.env.nextConfigPublicEnv,
......
export default () => <p>{process.env.NEXT_PUBLIC_TEST_DEST}</p> export default function Page() {
return <p>{process.env.NEXT_PUBLIC_TEST_DEST}</p>
}
...@@ -44,10 +44,12 @@ export async function getStaticProps() { ...@@ -44,10 +44,12 @@ export async function getStaticProps() {
} }
} }
export default ({ env }) => ( export default function Page({ env }) {
<> return (
<p>{JSON.stringify(env)}</p> <>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div> <p>{JSON.stringify(env)}</p>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div> <div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
</> <div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
) </>
)
}
...@@ -44,10 +44,12 @@ export async function getStaticProps() { ...@@ -44,10 +44,12 @@ export async function getStaticProps() {
} }
} }
export default ({ env }) => ( export default function Page({ env }) {
<> return (
<p>{JSON.stringify(env)}</p> <>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div> <p>{JSON.stringify(env)}</p>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div> <div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
</> <div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
) </>
)
}
...@@ -43,10 +43,12 @@ export async function getServerSideProps() { ...@@ -43,10 +43,12 @@ export async function getServerSideProps() {
} }
} }
export default ({ env }) => ( export default function Page({ env }) {
<> return (
<p>{JSON.stringify(env)}</p> <>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div> <p>{JSON.stringify(env)}</p>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div> <div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
</> <div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
) </>
)
}
...@@ -4,6 +4,7 @@ import url from 'url' ...@@ -4,6 +4,7 @@ import url from 'url'
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import { import {
nextBuild, nextBuild,
nextStart, nextStart,
...@@ -170,6 +171,9 @@ describe('Env Config', () => { ...@@ -170,6 +171,9 @@ describe('Env Config', () => {
const originalContents = [] const originalContents = []
beforeAll(async () => { beforeAll(async () => {
const outputIndex = output.length const outputIndex = output.length
const envFiles = (await fs.readdir(appDir)).filter((file) =>
file.startsWith('.env')
)
const envToUpdate = [ const envToUpdate = [
{ {
toAdd: 'NEW_ENV_KEY=true', toAdd: 'NEW_ENV_KEY=true',
...@@ -185,12 +189,16 @@ describe('Env Config', () => { ...@@ -185,12 +189,16 @@ describe('Env Config', () => {
}, },
] ]
for (const { file, toAdd } of envToUpdate) { for (const file of envFiles) {
const content = await fs.readFile(join(appDir, file), 'utf8') const filePath = join(appDir, file)
const content = await fs.readFile(filePath, 'utf8')
originalContents.push({ file, content }) originalContents.push({ file, content })
await fs.writeFile(join(appDir, file), content + '\n' + toAdd)
}
const toUpdate = envToUpdate.find((item) => item.file === file)
if (toUpdate) {
await fs.writeFile(filePath, content + `\n${toUpdate.toAdd}`)
}
}
await check(() => { await check(() => {
return output.substring(outputIndex) return output.substring(outputIndex)
}, /Loaded env from/) }, /Loaded env from/)
...@@ -203,24 +211,138 @@ describe('Env Config', () => { ...@@ -203,24 +211,138 @@ describe('Env Config', () => {
runTests('dev', true) runTests('dev', true)
it('should update inlined values correctly', async () => { it('should have updated runtime values after change', async () => {
await renderViaHTTP(appPort, '/another-global') let html = await fetchViaHTTP(appPort, '/').then((res) => res.text())
let renderedEnv = JSON.parse(cheerio.load(html)('p').text())
const buildManifest = await fs.readJson(
join(__dirname, '../app/.next/build-manifest.json') expect(renderedEnv['ENV_FILE_KEY']).toBe('env')
) expect(renderedEnv['ENV_FILE_LOCAL_OVERRIDE_TEST']).toBe('localenv')
let outputIdx = output.length
const pageFile = buildManifest.pages['/another-global'].find(
(filename) => filename.includes('pages/another-global') const envFile = join(appDir, '.env')
) const envLocalFile = join(appDir, '.env.local')
const envContent = originalContents.find(
(item) => item.file === '.env'
).content
const envLocalContent = originalContents.find(
(item) => item.file === '.env.local'
).content
try {
await fs.writeFile(
envFile,
envContent.replace(`ENV_FILE_KEY=env`, `ENV_FILE_KEY=env-updated`)
)
// we should only log we loaded new env from .env
await check(() => output.substring(outputIdx), /Loaded env from/)
expect(
[...output.substring(outputIdx).matchAll(/Loaded env from/g)].length
).toBe(1)
expect(output.substring(outputIdx)).not.toContain('.env.local')
await check(async () => {
html = await fetchViaHTTP(appPort, '/').then((res) => res.text())
renderedEnv = JSON.parse(cheerio.load(html)('p').text())
expect(renderedEnv['ENV_FILE_KEY']).toBe('env-updated')
expect(renderedEnv['ENV_FILE_LOCAL_OVERRIDE_TEST']).toBe('localenv')
return 'success'
}, 'success')
outputIdx = output.length
await fs.writeFile(
envLocalFile,
envLocalContent.replace(
`ENV_FILE_LOCAL_OVERRIDE_TEST=localenv`,
`ENV_FILE_LOCAL_OVERRIDE_TEST=localenv-updated`
)
)
// we should only log we loaded new env from .env
await check(() => output.substring(outputIdx), /Loaded env from/)
expect(
[...output.substring(outputIdx).matchAll(/Loaded env from/g)].length
).toBe(1)
expect(output.substring(outputIdx)).toContain('.env.local')
await check(async () => {
html = await fetchViaHTTP(appPort, '/').then((res) => res.text())
renderedEnv = JSON.parse(cheerio.load(html)('p').text())
expect(renderedEnv['ENV_FILE_KEY']).toBe('env-updated')
expect(renderedEnv['ENV_FILE_LOCAL_OVERRIDE_TEST']).toBe(
'localenv-updated'
)
return 'success'
}, 'success')
} finally {
await fs.writeFile(envFile, envContent)
await fs.writeFile(envLocalFile, envLocalContent)
}
})
// read client bundle contents since a server side render can it('should trigger HMR correctly when NEXT_PUBLIC_ env is changed', async () => {
// have the value available during render but it not be injected const envFile = join(appDir, '.env')
const bundleContent = await fs.readFile( const envLocalFile = join(appDir, '.env.local')
join(appDir, '.next', pageFile), const envContent = originalContents.find(
'utf8' (item) => item.file === '.env'
) ).content
expect(bundleContent).toContain('again') const envLocalContent = originalContents.find(
(item) => item.file === '.env.local'
).content
try {
const browser = await webdriver(appPort, '/global')
expect(await browser.elementByCss('p').text()).toBe('another')
let outputIdx = output.length
await fs.writeFile(
envFile,
envContent.replace(
`NEXT_PUBLIC_TEST_DEST=another`,
`NEXT_PUBLIC_TEST_DEST=replaced`
)
)
// we should only log we loaded new env from .env
await check(() => output.substring(outputIdx), /Loaded env from/)
expect(
[...output.substring(outputIdx).matchAll(/Loaded env from/g)].length
).toBe(1)
expect(output.substring(outputIdx)).not.toContain('.env.local')
await check(() => browser.elementByCss('p').text(), 'replaced')
outputIdx = output.length
await fs.writeFile(
envLocalFile,
envLocalContent + `\nNEXT_PUBLIC_TEST_DEST=overridden`
)
// we should only log we loaded new env from .env
await check(() => output.substring(outputIdx), /Loaded env from/)
expect(
[...output.substring(outputIdx).matchAll(/Loaded env from/g)].length
).toBe(1)
expect(output.substring(outputIdx)).toContain('.env.local')
await check(() => browser.elementByCss('p').text(), 'overridden')
outputIdx = output.length
await fs.writeFile(envLocalFile, envLocalContent)
// we should only log we loaded new env from .env
await check(() => output.substring(outputIdx), /Loaded env from/)
expect(
[...output.substring(outputIdx).matchAll(/Loaded env from/g)].length
).toBe(1)
expect(output.substring(outputIdx)).toContain('.env.local')
await check(() => browser.elementByCss('p').text(), 'replaced')
} finally {
await fs.writeFile(envFile, envContent)
await fs.writeFile(envLocalFile, envLocalContent)
}
}) })
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment