如何用 sinon 编写配置 winston 的单元测试?
How write unit test for configure winston with sinon?
我为 winstin 写了配置:
import {createLogger, format, Logger, transports} from "winston";
export const logger: Logger = createLogger({
level: 'debug',
format: format.combine(
format.timestamp(),
format.json()
),
transports: [
new transports.Console()
]
});
但是我不知道如何为这个配置编写单元测试。
这里是使用proxyquire的单元测试解决方案:
logger.ts
:
import { createLogger, format, Logger, transports } from 'winston';
export const logger: Logger = createLogger({
level: 'debug',
format: format.combine(format.timestamp(), format.json()),
transports: [new transports.Console()],
});
logger.test.ts
:
import proxyquire from 'proxyquire';
import sinon from 'sinon';
describe('61027051', () => {
it('should pass', () => {
const winstonStub = {
createLogger: sinon.stub(),
format: {
combine: sinon.stub(),
timestamp: sinon.stub(),
json: sinon.stub(),
},
transports: {
Console: sinon.stub(),
},
};
proxyquire('./logger', {
winston: winstonStub,
});
sinon.assert.calledWith(winstonStub.createLogger, {
level: 'debug',
format: sinon.match.any,
transports: sinon.match.array,
});
});
});
100% 覆盖率的单元测试结果:
61027051
✓ should pass (1999ms)
1 passing (2s)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
logger.ts | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
我为 winstin 写了配置:
import {createLogger, format, Logger, transports} from "winston";
export const logger: Logger = createLogger({
level: 'debug',
format: format.combine(
format.timestamp(),
format.json()
),
transports: [
new transports.Console()
]
});
但是我不知道如何为这个配置编写单元测试。
这里是使用proxyquire的单元测试解决方案:
logger.ts
:
import { createLogger, format, Logger, transports } from 'winston';
export const logger: Logger = createLogger({
level: 'debug',
format: format.combine(format.timestamp(), format.json()),
transports: [new transports.Console()],
});
logger.test.ts
:
import proxyquire from 'proxyquire';
import sinon from 'sinon';
describe('61027051', () => {
it('should pass', () => {
const winstonStub = {
createLogger: sinon.stub(),
format: {
combine: sinon.stub(),
timestamp: sinon.stub(),
json: sinon.stub(),
},
transports: {
Console: sinon.stub(),
},
};
proxyquire('./logger', {
winston: winstonStub,
});
sinon.assert.calledWith(winstonStub.createLogger, {
level: 'debug',
format: sinon.match.any,
transports: sinon.match.array,
});
});
});
100% 覆盖率的单元测试结果:
61027051
✓ should pass (1999ms)
1 passing (2s)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
logger.ts | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------