Mocha - 在测试环境之外导入 moch
Mocha - moch import outside of test environment
不确定这是否可行,我有一个在 100 多个文件中使用的辅助方法:
// helpers.ts
// exports a json object using export default
import STATIC_DATA from '@data/static-data';
export function getData(key: StaticDataKey) {
return STATIC_DATA[key].include ? 'foo' : 'bar';
}
此辅助方法的功能远不止于此,但我简化了我的示例
测试环境中
import assert from 'assert';
it('should return foo when include is true', () => {
const isFoo = getData('someKey');
assert.equal(isFoo, 'foo');
});
it('should return bar when include is false', () => {
const isBar = getData('someOtherKey');
assert.equal(isBar, 'bar');
});
我想做的是,在每个测试中模拟在 helpers.ts 文件中导入的“STATIC_DATA”是什么,所以我可以强制数据成为我想要的测试始终如一地保持真实,例如(我知道它不起作用)
it('should return bar when include is false', () => {
proxyquire('@data/static-data', {
someOtherKey:{
include: false
}
});
const isBar = getData('someOtherKey');
assert.equal(isBar, 'bar');
});
我试过使用 nock、sinon、proxyrequire 但没有成功:(
只需在每个测试用例中将测试数据设置为STATIC_DATA
,并在每个测试用例结束时将其清除即可。
static-data.ts
:
export default {};
helper.ts
import STATIC_DATA from './static-data';
export function getData(key) {
return STATIC_DATA[key].include ? 'foo' : 'bar';
}
helper.test.ts
:
import { getData } from './helper';
import sinon from 'sinon';
import STATIC_DATA from './static-data';
describe('72312542', () => {
it('should return bar when include is false', () => {
STATIC_DATA['someOtherKey'] = { include: false };
const isBar = getData('someOtherKey');
sinon.assert.match(isBar, 'bar');
delete STATIC_DATA['someOtherKey'];
});
it('should return foo when include is true', () => {
STATIC_DATA['someKey'] = { include: true };
const isFoo = getData('someKey');
sinon.assert.match(isFoo, 'foo');
delete STATIC_DATA['someKey'];
});
});
测试结果:
72312542
✓ should return bar when include is false
✓ should return foo when include is true
2 passing (4ms)
----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
helper.ts | 100 | 100 | 100 | 100 |
static-data.ts | 100 | 100 | 100 | 100 |
----------------|---------|----------|---------|---------|-------------------
不确定这是否可行,我有一个在 100 多个文件中使用的辅助方法:
// helpers.ts
// exports a json object using export default
import STATIC_DATA from '@data/static-data';
export function getData(key: StaticDataKey) {
return STATIC_DATA[key].include ? 'foo' : 'bar';
}
此辅助方法的功能远不止于此,但我简化了我的示例
测试环境中
import assert from 'assert';
it('should return foo when include is true', () => {
const isFoo = getData('someKey');
assert.equal(isFoo, 'foo');
});
it('should return bar when include is false', () => {
const isBar = getData('someOtherKey');
assert.equal(isBar, 'bar');
});
我想做的是,在每个测试中模拟在 helpers.ts 文件中导入的“STATIC_DATA”是什么,所以我可以强制数据成为我想要的测试始终如一地保持真实,例如(我知道它不起作用)
it('should return bar when include is false', () => {
proxyquire('@data/static-data', {
someOtherKey:{
include: false
}
});
const isBar = getData('someOtherKey');
assert.equal(isBar, 'bar');
});
我试过使用 nock、sinon、proxyrequire 但没有成功:(
只需在每个测试用例中将测试数据设置为STATIC_DATA
,并在每个测试用例结束时将其清除即可。
static-data.ts
:
export default {};
helper.ts
import STATIC_DATA from './static-data';
export function getData(key) {
return STATIC_DATA[key].include ? 'foo' : 'bar';
}
helper.test.ts
:
import { getData } from './helper';
import sinon from 'sinon';
import STATIC_DATA from './static-data';
describe('72312542', () => {
it('should return bar when include is false', () => {
STATIC_DATA['someOtherKey'] = { include: false };
const isBar = getData('someOtherKey');
sinon.assert.match(isBar, 'bar');
delete STATIC_DATA['someOtherKey'];
});
it('should return foo when include is true', () => {
STATIC_DATA['someKey'] = { include: true };
const isFoo = getData('someKey');
sinon.assert.match(isFoo, 'foo');
delete STATIC_DATA['someKey'];
});
});
测试结果:
72312542
✓ should return bar when include is false
✓ should return foo when include is true
2 passing (4ms)
----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
helper.ts | 100 | 100 | 100 | 100 |
static-data.ts | 100 | 100 | 100 | 100 |
----------------|---------|----------|---------|---------|-------------------