问题描述
我正在使用React,并且有一个像这样的文件树:
src/
common/
common-comp1/
common-comp1.js
common-comp1.test.js
index.js
common-comp2/
common-comp2.js
common-comp2.test.js
index.js
index.js
app/
app-comp1/
app-comp1.js
app-comp1.test.js
index.js
app-comp2/
app-comp2.js
app-comp2.test.js
index.js
index.js
所有index.js文件都是export * from '...';
的桶文件。我设置了路径别名,以便可以在import CommonComp1 from 'common';
中使用src/app/app-comp1/app-comp1.js
。
不幸的是,即使src/common/common-comp2/*
不会导入,它也会从app-comp1.js
加载/转换代码。当我使用webpack构建应用程序时,这很好,因为无论如何我都需要导入和转换所有内容。
但是,对于具有约350个组件的测试,这会使启动时运行单个测试套件的速度非常慢。我只想导入/转换运行测试所需的文件。有没有一种方法可以延迟加载每个导出/导入?我想也许可以通过模拟我的桶文件并导出Proxy
来实现此目的,该jest.requireActual()
仅在直接导入组件时通过调用require()
才需要/存储组件。我开始这样做,但是后来意识到我必须解析整个文件树中的导入和导出名称,这听起来很乏味。 (至少我认为我必须这样做。)
我想我也可以尝试代理 struct DXVertex
{
FLOAT x,y,z,rhw;
D3DCOLOR color;
};
LPDIRECT3DVERTEXBUFFER9 v_buffer{ nullptr };
void initMyStuff(IDirect3DDevice9* dxDevice)
{
DXVertex vertexArray[] =
{
{350.0f,50.0f,0.5f,0.0f,D3DCOLOR_XRGB(255,0)},// All colors end up being white
{520.0f,400.0f,D3DCOLOR_XRGB(0,255,{120.0f,255)},// This doesn't make a difference either
};
dxDevice->CreateVertexBuffer(3 * sizeof(DXVertex),NULL,D3DFVF_XYZRHW | D3DFVF_DIFFUSE,D3DPOOL_DEFAULT,&v_buffer,NULL);
void* bData;
v_buffer->Lock(NULL,&bData,NULL);
memcpy(bData,vertexArray,sizeof(vertexArray));
v_buffer->Unlock();
}
void drawTriangle(IDirect3DDevice9* dxDevice,float x0,float y0,float width,float height,D3DCOLOR color)
{
dxDevice->SetTexture(0,NULL);
dxDevice->SetPixelShader(NULL); // If this isn't here,the triangle doesn't draw
// dxDevice->SetVertexShader(NULL); // If this is here,the triangle doesn't draw either
// Settings I tried to mess with to no avail,I've tried all independently as well
dxDevice->SetRenderState(D3DRS_COLORVERTEX,TRUE);
dxDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
dxDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
dxDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
dxDevice->SetRenderState(D3DRS_SRCBLENDALPHA,D3DRS_DESTBLENDALPHA);
dxDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
dxDevice->SetStreamSource(0,v_buffer,sizeof(DXVertex));
dxDevice->DrawPrimitive(D3DPT_TRIANGLELIST,1);
}
// The overlay hook
HRESULT STDMETHODCALLTYPE present(IDirect3DDevice9* thisptr,const RECT* src,const RECT* dest,HWND wnd_override,const RGNDATA* dirty_region)
{
if (!v_buffer)
initMyStuff(thisptr);
thisptr->BeginScene();
// I am not even the parameters as of now for testing.
drawTriangle(thisptr,0);
thisptr->EndScene();
// Call original
return original_present(thisptr,src,dest,wnd_override,dirty_region);
}
本身(我几周前尝试了另一个问题),但是iirc,它是常量或声明为不可配置/不可写。
有想法吗?
解决方法
您可以在测试中使用模块工厂参数模拟桶文件,并仅设置您想要的导出。
如果 common-comp1.js 导出为默认值,请执行以下操作:
app-comp1.test.js:
jest.mock('common',()=>{
const CommonComp1 = jest.requireActual('common/common-comp1').default;
return {__esModule: true,CommonComp1}
})
如果 common-comp1.js 导出被命名,那么这样做:
app-comp1.test.js:
jest.mock('common',()=>{
const allNamedExports = jest.requireActual('common/common-comp1');
return {__esModule: true,...allNamedExports}
})
对于自动化程度更高的版本,您可以使用脚本生成 __mocks__/index.js
文件。
该脚本将通过每个 index.js 文件导入它,获取导出的名称并将它们放入 module.exports 对象 getter 中。
像这样:
__mocks__/common.js
:
module.exports={
get CommonComp1() {
return jest.requireActual('../common/common-comp1').CommonComp1;
}
get OtherExportFromCommonComp1() {
return jest.requireActual('../common/common-comp1').OtherExportFromCommonComp1;
}
get CommonComp2() {
return jest.requireActual('../common/common-comp2').CommonComp2;
}
}