Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit 17af603

Browse files
committed
fixed 'window' in jest nodejs environment
1 parent c4a9973 commit 17af603

File tree

8 files changed

+46
-45
lines changed

8 files changed

+46
-45
lines changed

jest.base.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ module.exports = {
1515
'ts-jest': {
1616
tsconfig: '<rootDir>/packages/tsconfig.default.json',
1717
},
18+
__DEV__: true,
1819
},
1920
};

packages/core/jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ module.exports = {
88
roots: [`<rootDir>/packages/${packageName}`],
99
name: packageName,
1010
displayName: packageName,
11+
globals: {
12+
...baseConfig.globals,
13+
...{ window: {} }, // https://stackoverflow.com/questions/46274889/jest-test-fails-with-window-is-not-defined
14+
},
1115
};

packages/core/src/agile.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
ComputeFunctionType,
2525
removeProperties,
2626
shared,
27-
runsOnServer,
2827
} from './internal';
2928

3029
export class Agile {
@@ -124,10 +123,6 @@ export class Agile {
124123
* @param config - Configuration object
125124
*/
126125
public configureLogger(config: CreateLoggerConfigInterface = {}): this {
127-
if (runsOnServer()) {
128-
Agile.logger = new Logger({ active: false });
129-
return this;
130-
}
131126
config = defineConfig(config, {
132127
prefix: 'Agile',
133128
active: true,

packages/core/src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ export function globalBind(
261261
* @public
262262
*/
263263
export const runsOnServer = (): boolean => {
264-
return (
265-
typeof window === 'undefined' ||
266-
typeof window.document === 'undefined' ||
267-
typeof window.document.createElement === 'undefined'
264+
return !(
265+
typeof window !== 'undefined' &&
266+
typeof window.document !== 'undefined' &&
267+
typeof window.document.createElement !== 'undefined'
268268
);
269269
};

packages/core/tests/helper/logMock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const logTypes = {
2222
};
2323

2424
function mockLogs(mockArg?: LogTypes[]): void {
25-
const _mockArg = mockArg ?? ['warn', 'error'];
25+
const _mockArg = mockArg ?? ['warn', 'error', 'log'];
2626
mockConsole(_mockArg);
2727
}
2828

packages/core/tests/unit/agile.test.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
} from '../../src';
1313
import testIntegration from '../helper/test.integration';
1414
import { LogMock } from '../helper/logMock';
15-
import * as Utils from '../../src/utils';
1615

1716
// https://github.com/facebook/jest/issues/5023
1817
jest.mock('../../src/runtime', () => {
@@ -175,27 +174,7 @@ describe('Agile Tests', () => {
175174
});
176175

177176
describe('configureLogger function tests', () => {
178-
it('should overwrite the static Logger with a new Logger Instance (runsOnServer = true)', () => {
179-
jest.spyOn(Utils, 'runsOnServer').mockReturnValueOnce(true);
180-
Agile.logger.config = 'outdated' as any;
181-
182-
agile.configureLogger({
183-
active: true,
184-
level: 0,
185-
});
186-
187-
expect(Agile.logger.config).toStrictEqual({
188-
canUseCustomStyles: true,
189-
level: 0,
190-
prefix: '',
191-
timestamp: false,
192-
});
193-
expect(Agile.logger.isActive).toBeFalsy();
194-
expect(Agile.logger.allowedTags).toStrictEqual([]);
195-
});
196-
197177
it('should overwrite the static Logger with a new Logger Instance (runsOnServer = false)', () => {
198-
jest.spyOn(Utils, 'runsOnServer').mockReturnValueOnce(false);
199178
Agile.logger.config = 'outdated' as any;
200179

201180
agile.configureLogger({
@@ -332,6 +311,10 @@ describe('Agile Tests', () => {
332311
});
333312

334313
describe('registerStorage function tests', () => {
314+
beforeEach(() => {
315+
agile.storages.register = jest.fn();
316+
});
317+
335318
it('should register provided Storage', () => {
336319
const dummyStorage = new Storage({
337320
prefix: 'test',

packages/core/tests/unit/utils.test.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import {
2-
globalBind,
3-
getAgileInstance,
42
Agile,
53
State,
64
Observer,
@@ -32,30 +30,30 @@ describe('Utils Tests', () => {
3230
it('should get agileInstance from State', () => {
3331
const dummyState = new State(dummyAgile, 'dummyValue');
3432

35-
expect(getAgileInstance(dummyState)).toBe(dummyAgile);
33+
expect(Utils.getAgileInstance(dummyState)).toBe(dummyAgile);
3634
});
3735

3836
it('should get agileInstance from Collection', () => {
3937
const dummyCollection = new Collection(dummyAgile);
4038

41-
expect(getAgileInstance(dummyCollection)).toBe(dummyAgile);
39+
expect(Utils.getAgileInstance(dummyCollection)).toBe(dummyAgile);
4240
});
4341

4442
it('should get agileInstance from Observer', () => {
4543
const dummyObserver = new Observer(dummyAgile);
4644

47-
expect(getAgileInstance(dummyObserver)).toBe(dummyAgile);
45+
expect(Utils.getAgileInstance(dummyObserver)).toBe(dummyAgile);
4846
});
4947

5048
it('should get agileInstance from globalThis if passed instance holds no agileInstance', () => {
51-
expect(getAgileInstance('weiredInstance')).toBe(dummyAgile);
49+
expect(Utils.getAgileInstance('weiredInstance')).toBe(dummyAgile);
5250
});
5351

5452
it('should print error if something went wrong', () => {
5553
// @ts-ignore | Destroy globalThis
5654
globalThis = undefined;
5755

58-
const response = getAgileInstance('weiredInstance');
56+
const response = Utils.getAgileInstance('weiredInstance');
5957

6058
expect(response).toBeUndefined();
6159
LogMock.hasLoggedCode('20:03:00', [], 'weiredInstance');
@@ -360,23 +358,23 @@ describe('Utils Tests', () => {
360358
});
361359

362360
it('should bind Instance globally at the specified key (default config)', () => {
363-
globalBind(dummyKey, 'dummyInstance');
361+
Utils.globalBind(dummyKey, 'dummyInstance');
364362

365363
expect(globalThis[dummyKey]).toBe('dummyInstance');
366364
});
367365

368366
it("shouldn't overwrite already globally bound Instance at the same key (default config)", () => {
369-
globalBind(dummyKey, 'I am first!');
367+
Utils.globalBind(dummyKey, 'I am first!');
370368

371-
globalBind(dummyKey, 'dummyInstance');
369+
Utils.globalBind(dummyKey, 'dummyInstance');
372370

373371
expect(globalThis[dummyKey]).toBe('I am first!');
374372
});
375373

376374
it('should overwrite already globally bound Instance at the same key (overwrite = true)', () => {
377-
globalBind(dummyKey, 'I am first!');
375+
Utils.globalBind(dummyKey, 'I am first!');
378376

379-
globalBind(dummyKey, 'dummyInstance', true);
377+
Utils.globalBind(dummyKey, 'dummyInstance', true);
380378

381379
expect(globalThis[dummyKey]).toBe('dummyInstance');
382380
});
@@ -385,9 +383,29 @@ describe('Utils Tests', () => {
385383
// @ts-ignore | Destroy globalThis
386384
globalThis = undefined;
387385

388-
globalBind(dummyKey, 'dummyInstance');
386+
Utils.globalBind(dummyKey, 'dummyInstance');
389387

390388
LogMock.hasLoggedCode('20:03:01', [dummyKey]);
391389
});
392390
});
391+
392+
describe('runsOnServer function tests', () => {
393+
it("should return 'false' if the current environment isn't a server", () => {
394+
// eslint-disable-next-line no-global-assign
395+
window = {
396+
document: {
397+
createElement: 'isSet' as any,
398+
} as any,
399+
} as any;
400+
401+
expect(Utils.runsOnServer()).toBeFalsy();
402+
});
403+
404+
it("should return 'true' if the current environment is a server", () => {
405+
// eslint-disable-next-line no-global-assign
406+
window = undefined as any;
407+
408+
expect(Utils.runsOnServer()).toBeTruthy();
409+
});
410+
});
393411
});

packages/react/src/hooks/useIsomorphicLayoutEffect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ import { runsOnServer } from '@agile-ts/core';
1010
// is created synchronously, otherwise a store update may occur before the
1111
// subscription is created and an inconsistent state may be observed
1212

13-
export const useIsomorphicLayoutEffect = runsOnServer
13+
export const useIsomorphicLayoutEffect = runsOnServer()
1414
? useLayoutEffect
1515
: useEffect;

0 commit comments

Comments
 (0)