mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
test: stop mocking env vars in jest.utils (#53985)
This commit is contained in:
committed by
GitHub
parent
87762e9a93
commit
9f71d1c3ac
@@ -7,23 +7,6 @@ import { createUserInput } from './src/utils/create-user';
|
||||
import { examJson } from './__mocks__/exam';
|
||||
import { MONGOHQ_URL } from './src/utils/env';
|
||||
|
||||
jest.mock('./src/utils/env', () => {
|
||||
const createTestConnectionURL = (url: string, dbId: string) =>
|
||||
url.replace(/(.*)(\?.*)/, `$1${dbId}$2`);
|
||||
// There are other properties, and this type is too narrow, but we're only
|
||||
// interested in MONGOHQ_URL here.
|
||||
const actual: {
|
||||
MONGOHQ_URL: string;
|
||||
} = jest.requireActual('./src/utils/env');
|
||||
return {
|
||||
...actual,
|
||||
MONGOHQ_URL: createTestConnectionURL(
|
||||
actual.MONGOHQ_URL,
|
||||
process.env.JEST_WORKER_ID!
|
||||
)
|
||||
};
|
||||
});
|
||||
|
||||
type FastifyTestInstance = Awaited<ReturnType<typeof build>>;
|
||||
|
||||
declare global {
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
import { setupServer, superRequest } from '../jest.utils';
|
||||
import { HOME_LOCATION, COOKIE_DOMAIN } from './utils/env';
|
||||
|
||||
jest.mock('./utils/env', () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return {
|
||||
...jest.requireActual('./utils/env'),
|
||||
COOKIE_DOMAIN: '.freecodecamp.org'
|
||||
};
|
||||
});
|
||||
|
||||
describe('server', () => {
|
||||
setupServer();
|
||||
|
||||
|
||||
+23
-3
@@ -26,6 +26,20 @@ function isAllowedProvider(provider: string): provider is 'ses' | 'nodemailer' {
|
||||
return ['ses', 'nodemailer'].includes(provider);
|
||||
}
|
||||
|
||||
function createTestConnectionURL(url: string, dbId?: string) {
|
||||
assert.notEqual(
|
||||
process.env.FREECODECAMP_NODE_ENV,
|
||||
'production',
|
||||
"The database URL can't be modified in production."
|
||||
);
|
||||
assert.ok(
|
||||
dbId,
|
||||
`dbId is required for test connection URL. Is this running in a test environment?
|
||||
If so, ensure that the environment variable JEST_WORKER_ID is set.`
|
||||
);
|
||||
return url.replace(/(.*)(\?.*)/, `$1${dbId}$2`);
|
||||
}
|
||||
|
||||
assert.ok(process.env.HOME_LOCATION);
|
||||
assert.ok(process.env.FREECODECAMP_NODE_ENV);
|
||||
assert.ok(isAllowedEnv(process.env.FREECODECAMP_NODE_ENV));
|
||||
@@ -40,6 +54,7 @@ assert.ok(process.env.FCC_ENABLE_DEV_LOGIN_MODE);
|
||||
assert.ok(process.env.JWT_SECRET);
|
||||
assert.ok(process.env.STRIPE_SECRET_KEY);
|
||||
assert.ok(process.env.SHOW_UPCOMING_CHANGES);
|
||||
assert.ok(process.env.MONGOHQ_URL);
|
||||
|
||||
if (process.env.FREECODECAMP_NODE_ENV !== 'development') {
|
||||
assert.ok(process.env.SES_ID);
|
||||
@@ -52,7 +67,6 @@ if (process.env.FREECODECAMP_NODE_ENV !== 'development') {
|
||||
assert.ok(process.env.SES_REGION);
|
||||
assert.ok(process.env.COOKIE_DOMAIN);
|
||||
assert.ok(process.env.PORT);
|
||||
assert.ok(process.env.MONGOHQ_URL);
|
||||
assert.ok(process.env.SENTRY_DSN);
|
||||
// The following values can exist in development, but production-like
|
||||
// environments need to override the defaults.
|
||||
@@ -84,12 +98,18 @@ if (process.env.FREECODECAMP_NODE_ENV !== 'development') {
|
||||
'sk_from_stripe_dashboard',
|
||||
'The Stripe secret should be changed from the default value.'
|
||||
);
|
||||
assert.notEqual(process.env.NODE_ENV, 'test');
|
||||
}
|
||||
|
||||
export const HOME_LOCATION = process.env.HOME_LOCATION;
|
||||
export const MONGOHQ_URL =
|
||||
process.env.MONGOHQ_URL ??
|
||||
'mongodb://localhost:27017/freecodecamp?directConnection=true';
|
||||
process.env.NODE_ENV === 'test'
|
||||
? createTestConnectionURL(
|
||||
process.env.MONGOHQ_URL,
|
||||
process.env.JEST_WORKER_ID
|
||||
)
|
||||
: process.env.MONGOHQ_URL;
|
||||
|
||||
export const FREECODECAMP_NODE_ENV = process.env.FREECODECAMP_NODE_ENV;
|
||||
export const AUTH0_DOMAIN = process.env.AUTH0_DOMAIN;
|
||||
export const AUTH0_AUDIENCE = process.env.AUTH0_AUDIENCE;
|
||||
|
||||
Reference in New Issue
Block a user