如何在基本 Typescript 示例上增加 Mocha 测试的代码覆盖率?
How to increase code coverage for Mocha testing on basic Typescript example?
我想更好地了解如何使用 mocha 在 Typescript 中测试这些类型的函数。即,我有以下小文件:
import { Request, Response } from 'express';
export const ping = (req: Request, res: Response) => {
res.send('pong');
};
export const health = (req: Request, res: Response) => {
res.send('OK');
};
这个练习可能很简单,因为这里没有太多要测试的,但下面我有一些基本的 mocha 测试:
describe('Health Tests', () => {
it('Check ping', () => {
expect(ping).to.not.be.null;
})
it('Check health', () => {
expect(health).to.not.be.null;
})
})
当我 运行 此测试的代码覆盖率时,我得到:50% Stmts | 100% 分支 | 0% 功能 | 50% 线。特别是因为这是这么一小段代码,如果可能的话,我想对所有类别进行 100% 的覆盖。有人会对如何实现这一目标有任何建议吗?另外,有人可以解释一下为什么我的函数覆盖率为 0% 吗?通过检查 ping
或 health
,我是不是也调用了一个函数,因此也测试了它。
如有任何建议,我们将不胜感激!
您需要使用一些 mock/stub 库,例如 sinonjs。这是覆盖率为 100% 的单元测试解决方案:
index.ts
:
import { Request, Response } from 'express';
export const ping = (req: Request, res: Response) => {
res.send('pong');
};
export const health = (req: Request, res: Response) => {
res.send('OK');
};
index.test.ts
:
import { ping, health } from './';
import sinon from 'sinon';
describe('63065938', () => {
describe('#ping', () => {
it('should pass', () => {
const mReq = {};
const mRes = { send: sinon.stub() };
ping(mReq, mRes as any);
sinon.assert.calledWith(mRes.send, 'pong');
});
});
describe('#health', () => {
it('should pass', () => {
const mReq = {};
const mRes = { send: sinon.stub() };
health(mReq, mRes as any);
sinon.assert.calledWith(mRes.send, 'OK');
});
});
});
100% 覆盖率的单元测试结果:
63065938
#ping
✓ should pass
#health
✓ should pass
2 passing (12ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
我想更好地了解如何使用 mocha 在 Typescript 中测试这些类型的函数。即,我有以下小文件:
import { Request, Response } from 'express';
export const ping = (req: Request, res: Response) => {
res.send('pong');
};
export const health = (req: Request, res: Response) => {
res.send('OK');
};
这个练习可能很简单,因为这里没有太多要测试的,但下面我有一些基本的 mocha 测试:
describe('Health Tests', () => {
it('Check ping', () => {
expect(ping).to.not.be.null;
})
it('Check health', () => {
expect(health).to.not.be.null;
})
})
当我 运行 此测试的代码覆盖率时,我得到:50% Stmts | 100% 分支 | 0% 功能 | 50% 线。特别是因为这是这么一小段代码,如果可能的话,我想对所有类别进行 100% 的覆盖。有人会对如何实现这一目标有任何建议吗?另外,有人可以解释一下为什么我的函数覆盖率为 0% 吗?通过检查 ping
或 health
,我是不是也调用了一个函数,因此也测试了它。
如有任何建议,我们将不胜感激!
您需要使用一些 mock/stub 库,例如 sinonjs。这是覆盖率为 100% 的单元测试解决方案:
index.ts
:
import { Request, Response } from 'express';
export const ping = (req: Request, res: Response) => {
res.send('pong');
};
export const health = (req: Request, res: Response) => {
res.send('OK');
};
index.test.ts
:
import { ping, health } from './';
import sinon from 'sinon';
describe('63065938', () => {
describe('#ping', () => {
it('should pass', () => {
const mReq = {};
const mRes = { send: sinon.stub() };
ping(mReq, mRes as any);
sinon.assert.calledWith(mRes.send, 'pong');
});
});
describe('#health', () => {
it('should pass', () => {
const mReq = {};
const mRes = { send: sinon.stub() };
health(mReq, mRes as any);
sinon.assert.calledWith(mRes.send, 'OK');
});
});
});
100% 覆盖率的单元测试结果:
63065938
#ping
✓ should pass
#health
✓ should pass
2 passing (12ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------