javascript - 如何为类型类编写单元测试?

标签 javascript typescript unit-testing jestjs

我一直在学习使用 Jest 库编写 JavaScript/TypeScript 代码的单元测试。这是一个我不知道如何处理的例子。它是用 TypeScript 键入的——只有两个公共(public)方法和一个需要 service1 参数的构造函数。

我想我需要测试两种情况:

  • 如果 this.attr is <= 42并发生递增,

  • 如果 this.attr is > 42和方法end()开火。

我的问题是:

  • 我无法访问 attr属性,它是私有(private)的,我不知道如何为其分配任何值(也许在测试中创建实例时,但我不知道如何分配)

  • 我不知道 this.service1.get() 是什么功能是。我没有在代码中看到它的任何实现,也不知道它是如何工作的。我应该将它作为参数传递给此类的实例吗?

  • 在这个特定示例中,我应该使用 fakeTimers 还是 mock/spy 感到困惑?

export class Class4 {
    private attr: number;
    private intervalId;

    constructor(private service1) { }

    public method() {
        this.intervalId = setInterval(() => {
            if (this.service1.get() > 42) {
                this.end()
            } else {
                this.attr++;
            }
        }, 100);
    }

    public getAttr() {
        return this.attr;
    }

    private end() {
        clearInterval(this.intervalId);
    }
}

我只需要您帮助我在 Jest 中为我描述的两种情况编写测试。

编辑。 这是基于此类的简单测试。它没有分配 this.attr 的值(虽然我的参数值被分配到 service1)并且在运行测试后我收到一条错误消息

Expected: 40 Received: undefined

代码:

    it('should stop incrementing Class4.attr if it\'s > 42', () => {
        const class4 = new Class4(40);
        const attrVal = class4.getAttr();
        expect(attrVal).toBe(40);
    });

最佳答案

我不太确定这是否有帮助,但下面是一个示例,说明您可以如何使用 Jest 来测试类似的东西。

这是将您的代码从 typescript 转换为 es6,并附加了一个轻型伪造的 Jest 实现。 它位于一个单独的脚本中,以单独保留示例本身。

伪造的 Jest 仅在本次测试中实现了所需的 Jest 匹配器:expect , toBeGreaterThan , not , toHaveBeenCalledTimes .

以及以下 Jest 实用程序:useFakeTimers , advanceTimersByTime , clearAllTimers , mock

// self calling function is required to simulate Class4 module and for fake Jest mock to work
(function() {
// translated from typescript to es6
class Class4 {
    attr = 0;

    intervalId = null;

    constructor(service1) {
        this.service1 = service1;
    }

    method() {
        this.intervalId = setInterval(() => {
            if (this.service1.get() > 42) {
                this.end();
            } else {
                this.attr++;
            }
        }, 100);
    }

    getAttr() {
        return this.attr;
    }

    end() {
        clearInterval(this.intervalId);
    }
}
// this is required to simulate Class4 module and for fake Jest mock to work
window.Class4 = Class4;
})();

// even if we do not know exactly what Service is,
// we know that it has a get method which returns a varying number.
// so this implementation of Service will do
// (it's ok since we're testing Class4, not Service)
class ServiceImpl {
    v = 0;
    set(v) { this.v = v; }
    get() { return this.v; }
}

// after this call, jest will control the flow of
// time in the following tests
// (reimplements the global methods setInterval, setTimeout...etc)
jest.useFakeTimers();

// actually it should be jest.mock('<path to your module>')
// but remember we're using a fake Jest working in SO's snippet)
// now Class4 is a mock
jest.mock(Class4);

// we need a Service instance for a Class4 object to be instanciated
const service = new ServiceImpl();

const class4 = new Class4(service);

it('Class4 constructor has been called 1 time', () => {
    expect(Class4).toHaveBeenCalledTimes(1);
});

it('should be incrementing Class4.attr if service.get() < 42', () => {
    // service.get() will return 40
    service.set(40);

    // storing initial attr val
    let lastAttrVal = class4.getAttr();

    // now class4 is running and should be incrementing
    class4.method();

    // jest controls the time, advances time by 1 second
    jest.advanceTimersByTime(1000);

    expect(class4.getAttr()).toBeGreaterThan(lastAttrVal);
});

it('should have been called Class4.end 0 time', () => {
    expect(Class4.mock.instances[0].end).toHaveBeenCalledTimes(0);
});

it('should stop incrementing Class4.attr if service.get() > 42', () => {
    // service.get() will now return 45, this should end class4
    // incrementation in the next interval
    service.set(45);

    // storing current attr val
    let lastAttrVal = class4.getAttr();

    jest.advanceTimersByTime(1000);

    expect(class4.getAttr()).not.toBeGreaterThan(lastAttrVal);

});

it('end should have been called end 1 time', () => {
    expect(Class4.mock.instances[0].end).toHaveBeenCalledTimes(1);
});

jest.clearAllTimers();
<script type="text/javascript">
window.jest = {};
jest.useFakeTimers = () => {
    jest.oldSetTimeout = window.setTimeout;
    jest.oldSetInterval = window.setInterval;
    jest.oldClearTimeout = window.clearTimeout;
    jest.oldClearInterval = window.clearInterval;
    jest.time = 0;
    jest.runningIntervals = [];
    window.setInterval = (callback, delay) => {
        let interIndex = jest.runningIntervals.findIndex(i => i.cancelled);
        let inter = interIndex !== -1 && jest.runningIntervals[interIndex];
        if (!inter) {
            inter = {};
            interIndex = jest.runningIntervals.length;
            jest.runningIntervals.push(inter);
        }
        Object.assign(
            inter,
            {
                start: jest.time,
                last: jest.time,
                callback,
                delay,
                cancelled: false
            }
        );
        callback();
        return interIndex;
    };
    window.clearInterval = idx => {
        jest.runningIntervals[idx].cancelled = true;
    };
    jest.advanceTimersByTime = advance => {
        for (const end = jest.time + advance;jest.time < end; jest.time++) {
            jest.runningIntervals.forEach(inter => {
                if (!inter.cancelled && jest.time - inter.last >= inter.delay) {
                    inter.last = jest.time;
                    inter.callback();
                }
            });
        }
    };
    jest.clearAllTimers = () => {
        jest.runningIntervals.length = 0;
        window.setTimeout = jest.oldSetTimeout;
        window.setInterval = jest.oldSetInterval;
        window.clearTimeout = jest.oldClearTimeout;
        window.clearInterval = jest.oldClearInterval;
    };
};

jest.resolve = (v) => {
  console.log(v ? 'PASS' : 'FAIL');
}
window.it = (description, test) => {
    console.log(description);
    test();
};
window.expect = (received) => {
  return {
    toBeGreaterThan: (expected) => jest.resolve(received > expected),
    not: {
      toBeGreaterThan: (expected) => jest.resolve(received <= expected),
    },
    toHaveBeenCalledTimes: (expected) => jest.resolve((received ? received.mock.calls.length : 0) === expected),
  }
}
jest.mock = (cls) => {
    if (cls.mock) return;
    const mock = {
        instances: [],
        calls: []
    }
    const proto0 = cls.prototype;

    function ClassMock(...args) {
        mock.calls.push(args);
        
        this.instance = new proto0.constructor(...args);
        this.instanceMock = {};
        mock.instances.push(this.instanceMock);
        Object.getOwnPropertyNames(proto0).forEach((member) => {
          if (member === 'constructor' || typeof proto0[member] !== 'function') return;
          this.instanceMock[member] = this.instanceMock[member] || { mock: { calls: [] } };
          this.instance[member] = (function(...args) {
              this.instanceMock[member].mock.calls.push(args);
              return proto0[member].apply(this.instance, [args]);
          }).bind(this);
      });
    }

    Object.getOwnPropertyNames(proto0).forEach((member) => {
        if (member === 'constructor' || typeof proto0[member] !== 'function') return;
        ClassMock.prototype[member] = function(...args) {
            return this.instance[member](...args);
        }
    });
    
    
    ClassMock.mock = mock;
    window[proto0.constructor.name] = ClassMock;
}
</script>

关于javascript - 如何为类型类编写单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57526051/

相关文章:

javascript setinterval 不会以低间隔同步触发

Typescript 允许构建无效的部分对象

javascript - Facebook 登录按钮回调函数

typescript - "CheckFileSystemCaseSensitive"任务意外失败,无法加载文件或程序集 'System.IO.FileSystem

typescript - 使用 TypeScript 从 Angular2 中的 http 数据链接 RxJS Observables

c# - Xamarin NUnitLite 测试方法 : How to do shared tests that run in every platform?

javascript - jasmine-node 是否提供任何类型的 "fail fast"选项?

.net - TFS 在构建服务器上不部署引用的程序集来测试目录

javascript - 如何在 ember 中为类属性编写 CSS 条件?

javascript - 在悬停时选择 sibling