-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: next/jest does not support import.meta (#91)
- Loading branch information
Showing
21 changed files
with
246 additions
and
124 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
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
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,30 @@ | ||
import { afterEach, describe, it, expect } from 'vitest'; | ||
import { getBasePath } from './utils'; | ||
|
||
describe('getBasePath()', () => { | ||
const processSave = { ...process }; | ||
const envSave = { ...process.env }; | ||
|
||
afterEach(() => { | ||
global.process = { ...processSave }; | ||
process.env = { ...envSave }; | ||
}); | ||
|
||
it('returns null without process', () => { | ||
// @ts-expect-error -- yes, we want to completely drop process for this test!! | ||
global.process = undefined; | ||
expect(getBasePath()).toBeUndefined(); | ||
}); | ||
|
||
it('returns null without process.env', () => { | ||
// @ts-expect-error -- yes, we want to completely drop process.env for this test!! | ||
process.env = undefined; | ||
expect(getBasePath()).toBeUndefined(); | ||
}); | ||
|
||
it('returns basepath set for Nextjs', () => { | ||
const basepath = `/_vercel-${Math.random()}/insights`; | ||
process.env.NEXT_PUBLIC_VERCEL_OBSERVABILITY_BASEPATH = basepath; | ||
expect(getBasePath()).toBe(basepath); | ||
}); | ||
}); |
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
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,30 @@ | ||
import { afterEach, describe, it, expect } from 'vitest'; | ||
import { getBasePath } from './utils'; | ||
|
||
describe('getBasePath()', () => { | ||
const processSave = { ...process }; | ||
const envSave = { ...process.env }; | ||
|
||
afterEach(() => { | ||
global.process = { ...processSave }; | ||
process.env = { ...envSave }; | ||
}); | ||
|
||
it('returns null without process', () => { | ||
// @ts-expect-error -- yes, we want to completely drop process for this test!! | ||
global.process = undefined; | ||
expect(getBasePath()).toBeUndefined(); | ||
}); | ||
|
||
it('returns null without process.env', () => { | ||
// @ts-expect-error -- yes, we want to completely drop process.env for this test!! | ||
process.env = undefined; | ||
expect(getBasePath()).toBeUndefined(); | ||
}); | ||
|
||
it('returns basepath set for CRA', () => { | ||
const basepath = `/_vercel-${Math.random()}/insights`; | ||
process.env.REACT_APP_VERCEL_OBSERVABILITY_BASEPATH = basepath; | ||
expect(getBasePath()).toBe(basepath); | ||
}); | ||
}); |
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,10 @@ | ||
export function getBasePath(): string | undefined { | ||
// !! important !! | ||
// do not access env variables using process.env[varname] | ||
// some bundles won't replace the value at build time. | ||
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain -- we can't use optionnal here, it'll break if process does not exist. | ||
if (typeof process === 'undefined' || typeof process.env === 'undefined') { | ||
return undefined; | ||
} | ||
return process.env.REACT_APP_VERCEL_OBSERVABILITY_BASEPATH; | ||
} |
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,24 @@ | ||
import { afterEach, describe, it, expect } from 'vitest'; | ||
import { getBasePath } from './utils'; | ||
|
||
describe('getBasePath()', () => { | ||
const processSave = { ...process }; | ||
const envSave = { ...process.env }; | ||
|
||
afterEach(() => { | ||
global.process = { ...processSave }; | ||
process.env = { ...envSave }; | ||
}); | ||
|
||
it('returns basepath set for Remix', () => { | ||
const basepath = `/_vercel-${Math.random()}/insights`; | ||
import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH = basepath; | ||
expect(getBasePath()).toBe(basepath); | ||
}); | ||
|
||
it('returns null without import.meta', () => { | ||
// @ts-expect-error -- yes, we want to completely drop import.meta.env for this test!! | ||
import.meta.env = undefined; | ||
expect(getBasePath()).toBeUndefined(); | ||
}); | ||
}); |
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
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,24 @@ | ||
import { afterEach, describe, it, expect } from 'vitest'; | ||
import { getBasePath } from './utils'; | ||
|
||
describe('getBasePath()', () => { | ||
const processSave = { ...process }; | ||
const envSave = { ...process.env }; | ||
|
||
afterEach(() => { | ||
global.process = { ...processSave }; | ||
process.env = { ...envSave }; | ||
}); | ||
|
||
it('returns basepath set for Sveltekit', () => { | ||
const basepath = `/_vercel-${Math.random()}/insights`; | ||
import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH = basepath; | ||
expect(getBasePath()).toBe(basepath); | ||
}); | ||
|
||
it('returns null without import.meta', () => { | ||
// @ts-expect-error -- yes, we want to completely drop import.meta.env for this test!! | ||
import.meta.env = undefined; | ||
expect(getBasePath()).toBeUndefined(); | ||
}); | ||
}); |
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,12 @@ | ||
export function getBasePath(): string | undefined { | ||
// !! important !! | ||
// do not access env variables using import.meta.env[varname] | ||
// some bundles won't replace the value at build time. | ||
try { | ||
return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH as | ||
| string | ||
| undefined; | ||
} catch { | ||
// do nothing | ||
} | ||
} |
Oops, something went wrong.