Skip to content
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

Remove global config #1422

Merged
merged 2 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const config: Config.InitialOptions = {
setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
globals: {
__DEV__: true,
__TEST__: true,
'ts-jest': {
tsconfig: './packages/hydrogen/tsconfig.json',
},
Expand Down
2 changes: 0 additions & 2 deletions packages/hydrogen/src/entry-server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
import {ServerPropsProvider} from './foundation/ServerPropsProvider';
import {isBotUA} from './utilities/bot-ua';
import {setContext, setCache, RuntimeContext} from './framework/runtime';
import {setConfig} from './framework/config';
import {
ssrRenderToPipeableStream,
ssrRenderToReadableStream,
Expand Down Expand Up @@ -127,7 +126,6 @@ export const renderHydrogen = (App: any) => {
*/
setCache(cache);
setContext(context);
setConfig({dev});

if (
url.pathname === EVENT_PATHNAME ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import React, {
declare global {
// eslint-disable-next-line no-var
var __DEV__: boolean;
// eslint-disable-next-line no-var
var __TEST__: boolean;
}

const PRIVATE_PROPS = ['request', 'response'] as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function getCacheForType(resource: () => Map<any, any>) {
.ReactCurrentDispatcher.current;

// @ts-ignore
if (__DEV__ && typeof jest !== 'undefined' && !dispatcher.getCacheForType) {
if (__TEST__ && !dispatcher.getCacheForType) {
// Jest does not have access to the RSC runtime, mock it here:
// @ts-ignore
return (globalThis.__jestRscCache ??= resource());
Expand Down Expand Up @@ -82,7 +82,7 @@ export function useServerRequest() {

if (!request) {
// @ts-ignore
if (__DEV__ && typeof jest !== 'undefined') {
if (__TEST__) {
// Unit tests are not wrapped in ServerRequestProvider.
// This mocks it, instead of providing it in every test.
return {ctx: {}} as ServerComponentRequest;
Expand Down
15 changes: 0 additions & 15 deletions packages/hydrogen/src/framework/config.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default () => {
define: {
__DEV__: env.mode !== 'production',
__WORKER__: isWorker,
__TEST__: false, // Used in unit tests
},

envPrefix: ['VITE_', 'PUBLIC_'],
Expand Down
5 changes: 2 additions & 3 deletions packages/hydrogen/src/hooks/useShopQuery/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {useShop} from '../../foundation/useShop';
import {getLoggerWithContext} from '../../utilities/log';
import type {CachingStrategy, PreloadOptions} from '../../types';
import {graphqlRequestBody} from '../../utilities';
import {getConfig} from '../../framework/config';
import {useServerRequest} from '../../foundation/ServerRequestProvider';
import {injectGraphQLTracker} from '../../utilities/graphql-tracker';
import {sendMessageToClient} from '../../utilities/devtools';
Expand Down Expand Up @@ -103,7 +102,7 @@ export function useShopQuery<T>({
log.error(errorMessage);
log.error(useQueryError);

if (getConfig().dev) {
if (__DEV__ && !__TEST__) {
throw new Error(errorMessage);
} else {
// in non-dev environments, we probably don't want super-detailed error messages for the user
Expand All @@ -121,7 +120,7 @@ export function useShopQuery<T>({
const errors = Array.isArray(data.errors) ? data.errors : [data.errors];

for (const error of errors) {
if (getConfig().dev) {
if (__DEV__ && !__TEST__) {
throw new Error(error.message);
} else {
log.error('GraphQL Error', error);
Expand Down