简体   繁体   English

如何在 typescript 中开玩笑地模拟全局 window object

[英]how to mock globals window object in jest in typescript

anyone know how I can mock window object for testing the value of feature ?有人知道我如何模拟 window object 来测试feature的价值吗? Following is my concept code, I want to test if expEnabled is enable feature is 'exp is enabled' otherwise feature is 'exp is disable' I have tried to mocking global.window in jest and Mocking globals in Jest but it does not seem work for me.以下是我的概念代码,我想测试是否启用 expEnabled 功能是否启用了 'exp 启用' 否则功能是 'exp 禁用' 我尝试过mocking global.windowZ0D7794B8F1C7EF38D4A1C19A3 中的 globals似乎不起作用为了我。

Updated: after moving the code const expEnabled = typeof window?== 'undefined' &&.?window.?app.?initialAppProps.;exp?.my_feature;更新:移动代码后const expEnabled = typeof window?== 'undefined' &&.?window.?app.?initialAppProps.;exp?.my_feature; into function it seems like use Object.defineProperty works.进入 function 似乎使用Object.defineProperty有效。

//source code
import Koa from 'koa';
export interface InitialAppProps {
  exp: Koa.DefaultState['exp'];

}
const expEnabled = typeof window !== 'undefined' && !!window?.app?.initialAppProps?.exp?.my_feature;

export function testMock(){
  return typeof window !== 'undefined' && !!window?.app?.initialAppProps?.exp?.my_feature;
}

//app is Functional component of my app from react
export const feature = [expEnabled  ? 'exp is enabled' : 'exp is disable']
export const featureNew = [testMock()  ? 'exp is enabled' : 'exp is disable']



//test code
describe('expEnabled', () => {
  it('feature should be enabled if expEnabled is trued', () => {
/*
    how can I mock expEnabled is true?
    I have tried this and console.log before expect. the right value print out but I still fail on expect
    Object.defineProperty(global, 'window', {
       value: {
         app: {initialAppProps: {exp: {my_feature: true}}},
       },
       writable: true,
     });
*/
    expect(feature).toEqual('exp is enabled');
  });
  it('feature should be enabled if expEnabled is trued', () => {
    Object.defineProperty(global, 'window', {
       value: {
         app: {initialAppProps: {exp: {my_feature: true}}},
       },
       writable: true,
     });
     expect(featureNew).toEqual('exp is enabled');
  })
});

Just assign a value to window.app.initialAppProps.exp.my_feature .只需为window.app.initialAppProps.exp.my_feature分配一个值。 You need to reset the module before executing each test case since the ./index module will be cached in the require.cache object.您需要在执行每个测试用例之前重置模块,因为./index模块将缓存在require.cache object 中。 This means the value of expEnabled variable will be cached too.这意味着expEnabled变量的值也将被缓存。

Eg例如

index.ts : index.ts

declare global {
  interface Window {
    app: {
      initialAppProps: {
        exp: {
          my_feature: boolean;
        };
      };
    };
  }
}

const expEnabled = typeof window !== 'undefined' && !!window.app.initialAppProps.exp.my_feature;
export const feature = [expEnabled ? 'exp is enabled' : 'exp is disable'];

index.test.ts : index.test.ts

describe('67041178', () => {
  beforeAll(() => {
    window.app = {
      initialAppProps: {
        exp: { my_feature: false },
      },
    };
  });
  beforeEach(() => {
    jest.resetModules();
  });
  it('should enable', () => {
    window.app.initialAppProps.exp.my_feature = true;
    const { feature } = require('./');
    expect(feature).toEqual(['exp is enabled']);
  });

  it('should disable', () => {
    window.app.initialAppProps.exp.my_feature = false;
    const { feature } = require('./');
    expect(feature).toEqual(['exp is disable']);
  });
  it('should pass', () => {
    Object.defineProperty(global, 'window', {
      value: {
        app: { initialAppProps: { exp: { my_feature: true } } },
      },
      writable: true,
    });
    const { feature } = require('./');
    expect(feature).toEqual(['exp is enabled']);
  });
});

unit test result:单元测试结果:

 PASS  examples/67041178/index.test.ts (13.133 s)
  67041178
    ✓ should enable (8 ms)
    ✓ should disable (1 ms)
    ✓ should pass (1 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        14.167 s

source code: https://github.com/mrdulin/jest-v26-codelab/tree/main/examples/67041178源代码: https://github.com/mrdulin/jest-v26-codelab/tree/main/examples/67041178

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM