简体   繁体   English

Jest:模拟一个字符串属性不存在

[英]Jest: Simulate that a string property doesn't exist

I have two functions which I'd like to test with Jest.我有两个我想用 Jest 测试的函数。 However, I can't get test coverage to 100% because I can't figure out how to simulate String.prototype.trimLeft being undefined .但是,我无法将测试覆盖率提高到 100%,因为我无法弄清楚如何模拟String.prototype.trimLeftundefined What can I do?我能做什么?

function trimLeft (str: string): string {
  if (String.prototype.trimLeft) {
    return str.trimLeft()
  } else {
    return str.replace(/^[\s\uFEFF\xA0]+/, '')
  }
  return str
  // else something's wrong
}

function trimRight (str: string, type: string): string {
  if (String.prototype.trimRight) {
    return str.trimRight()
  } else {
    return str.replace(/[\s\uFEFF\xA0]+$/, '')
  }

  return str
}

export { trimLeft, trimRight }

First of all, the last statement return str is not reachable in these two methods.首先,在这两种方法中,最后一条语句return str是不可达的。 After fixing them.修好之后。 Here is the unit test solution:这是单元测试解决方案:

index.ts : index.ts

function trimLeft(str: string): string {
  if (String.prototype.trimLeft) {
    return str.trimLeft();
  } else {
    return str.replace(/^[\s\uFEFF\xA0]+/, '');
  }
}

function trimRight(str: string): string {
  if (String.prototype.trimRight) {
    return str.trimRight();
  } else {
    return str.replace(/[\s\uFEFF\xA0]+$/, '');
  }
}

export { trimLeft, trimRight };

index.spec.ts : index.spec.ts

import { trimLeft, trimRight } from './';

describe('59430114', () => {
  describe('#trimLeft', () => {
    it('t1', () => {
      expect(trimLeft(' jestjs')).toBe('jestjs');
    });
    it('t2', () => {
      Object.defineProperty(String.prototype, 'trimLeft', { value: undefined });
      expect(trimLeft(' jestjs')).toBe('jestjs');
    });
  });

  describe('#trimRight', () => {
    it('t1', () => {
      expect(trimRight('jestjs ')).toBe('jestjs');
    });
    it('t2', () => {
      Object.defineProperty(String.prototype, 'trimRight', { value: undefined });
      expect(trimRight('jestjs ')).toBe('jestjs');
    });
  });
});

Unit test result with 100% coverage: 100% 覆盖率的单元测试结果:

 PASS  src/stackoverflow/59430114/index.spec.ts (8.386s)
  59430114
    #trimLeft
      ✓ t1 (4ms)
      ✓ t2 (1ms)
    #trimRight
      ✓ t1 (1ms)
      ✓ t2 (1ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        10.085s

Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59430114源代码: https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59430114

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

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