最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

玩笑自动嘲笑:有趣的示例结果

IT培训 admin 1浏览 0评论

玩笑自动嘲笑:有趣的示例结果

这是指Facebook示例tutorials

// utils.js
// Copyright 2004-present Facebook. All Rights Reserved.

export default {
  authorize: () => 'token',
  isAuthorized: (secret) => secret === 'wizard',
};

下面是测试文件。我没有在配置文件中添加自动模拟,而是在代码内部添加以显示差异。

import utils from './utils';

jest.enableAutomock();

test('implementation created by automock', () => {
  expect(utils.authorize('wizzard')).toBeUndefined();
  expect(utils.isAuthorized()).toBeUndefined();
});

结果:

TypeError: Cannot read property 'default' of undefined

   6 | 
   7 | test('implementation created by automock', () => {
>  8 |   expect(utils.authorize('wizzard')).toBeUndefined();
     |          ^
   9 |   expect(utils.isAuthorized()).toBeUndefined();
  10 | });
  11 | 

  at Object.utils (__tests__/example/automatic-mocks/genMockFromModule.test.js:8:10)

为什么?它发生在另一个文件automock.test.js上。错误消息是相同的。

// Copyright 2004-present Facebook. All Rights Reserved.

import utils from './utils';

jest.enableAutomock();

test('if utils are mocked', () => {
  expect(utils.authorize.mock).toBeTruthy();
  expect(utils.isAuthorized.mock).toBeTruthy();
});

test('mocked implementation', () => {
  utils.authorize.mockReturnValue('mocked_token');
  utils.isAuthorized.mockReturnValue(true);

  expect(utils.authorize()).toBe('mocked_token');
  expect(utils.isAuthorized('not_wizard')).toBeTruthy();
});
回答如下:

下面的示例对我有用,我将jestjstypescript结合使用,文档说:

注意:此方法以前称为autoMockOn。使用babel-jest时,对enableAutomock的调用将自动提升到代码块的顶部。如果要明确避免此行为,请使用autoMockOn。

utils.ts

const utils = {
  getJSON: data => JSON.stringify(data),
  authorize: () => 'token',
  isAuthorized: secret => secret === 'wizard'
};

export default utils;

utils.spec.ts

jest.enableAutomock();

import utils from './utils';

describe('automatic mocks test suites', () => {
  it('should mock all methods of utils', () => {
    expect((utils.getJSON as jest.Mock).mock).toBeTruthy();
    expect(jest.isMockFunction(utils.authorize)).toBeTruthy();
    expect(jest.isMockFunction(utils.isAuthorized)).toBeTruthy();
  });

  test('implementation created by automock', () => {
    expect(utils.authorize()).toBeUndefined();
    expect(utils.isAuthorized('wizard')).toBeUndefined();
  });

  it('mocked implementation', () => {
    (utils.getJSON as jest.Mock).mockReturnValue(123);
    expect(utils.getJSON({ name: 'test' })).toBe(123);
  });
});

单元测试结果:

 PASS  src/automatic-mocks/utils.spec.ts (17.906s)
  automatic mocks test suites
    ✓ should mock all methods of utils (4ms)
    ✓ implementation created by automock (2ms)
    ✓ mocked implementation (1ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        22.923s, estimated 23s

玩笑自动嘲笑:有趣的示例结果

这是指Facebook示例tutorials

// utils.js
// Copyright 2004-present Facebook. All Rights Reserved.

export default {
  authorize: () => 'token',
  isAuthorized: (secret) => secret === 'wizard',
};

下面是测试文件。我没有在配置文件中添加自动模拟,而是在代码内部添加以显示差异。

import utils from './utils';

jest.enableAutomock();

test('implementation created by automock', () => {
  expect(utils.authorize('wizzard')).toBeUndefined();
  expect(utils.isAuthorized()).toBeUndefined();
});

结果:

TypeError: Cannot read property 'default' of undefined

   6 | 
   7 | test('implementation created by automock', () => {
>  8 |   expect(utils.authorize('wizzard')).toBeUndefined();
     |          ^
   9 |   expect(utils.isAuthorized()).toBeUndefined();
  10 | });
  11 | 

  at Object.utils (__tests__/example/automatic-mocks/genMockFromModule.test.js:8:10)

为什么?它发生在另一个文件automock.test.js上。错误消息是相同的。

// Copyright 2004-present Facebook. All Rights Reserved.

import utils from './utils';

jest.enableAutomock();

test('if utils are mocked', () => {
  expect(utils.authorize.mock).toBeTruthy();
  expect(utils.isAuthorized.mock).toBeTruthy();
});

test('mocked implementation', () => {
  utils.authorize.mockReturnValue('mocked_token');
  utils.isAuthorized.mockReturnValue(true);

  expect(utils.authorize()).toBe('mocked_token');
  expect(utils.isAuthorized('not_wizard')).toBeTruthy();
});
回答如下:

下面的示例对我有用,我将jestjstypescript结合使用,文档说:

注意:此方法以前称为autoMockOn。使用babel-jest时,对enableAutomock的调用将自动提升到代码块的顶部。如果要明确避免此行为,请使用autoMockOn。

utils.ts

const utils = {
  getJSON: data => JSON.stringify(data),
  authorize: () => 'token',
  isAuthorized: secret => secret === 'wizard'
};

export default utils;

utils.spec.ts

jest.enableAutomock();

import utils from './utils';

describe('automatic mocks test suites', () => {
  it('should mock all methods of utils', () => {
    expect((utils.getJSON as jest.Mock).mock).toBeTruthy();
    expect(jest.isMockFunction(utils.authorize)).toBeTruthy();
    expect(jest.isMockFunction(utils.isAuthorized)).toBeTruthy();
  });

  test('implementation created by automock', () => {
    expect(utils.authorize()).toBeUndefined();
    expect(utils.isAuthorized('wizard')).toBeUndefined();
  });

  it('mocked implementation', () => {
    (utils.getJSON as jest.Mock).mockReturnValue(123);
    expect(utils.getJSON({ name: 'test' })).toBe(123);
  });
});

单元测试结果:

 PASS  src/automatic-mocks/utils.spec.ts (17.906s)
  automatic mocks test suites
    ✓ should mock all methods of utils (4ms)
    ✓ implementation created by automock (2ms)
    ✓ mocked implementation (1ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        22.923s, estimated 23s
发布评论

评论列表 (0)

  1. 暂无评论