如何改善反应测试库速度?
我注意到我的第一个测试需要大约6秒钟才能运行,但是这很简单。它检查了卡组件是否成功地使经过的孩子成功:
describe('Card component', () => {
test('renders children', () => {
const testString = 'TEST';
const TestCardChild: React.FC = () => {
return <p>{testString}</p>;
};
render(
<Card>
<TestCardChild />
</Card>
);
expect(screen.getByText(testString));
});
});
我在另一台机器上进行了几乎相同规格的测试,并且在几个miliseconds上运行。您是否有这样的提示?我应该将更多的RAM分配给VS代码,还是应该为React测试库申请任何设置?
谢谢,问候
I noticed that my first test take like 6 seconds to run, however, it is very simple. It checks whether the Card component renders the passed children successfully:
describe('Card component', () => {
test('renders children', () => {
const testString = 'TEST';
const TestCardChild: React.FC = () => {
return <p>{testString}</p>;
};
render(
<Card>
<TestCardChild />
</Card>
);
expect(screen.getByText(testString));
});
});
I ran the test on another machine with almost the same specs and it runs in a few miliseconds. Do you have a tip on why this happens? Should I allocate more RAM to VS code, or are there any settings that I should apply for React testing library?
Thanks and regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
玩笑使用 babel-jest 插件以使用babel来编译Javascript代码。
您可以找到
ts-jest
process 在这里它还将在最后使用babel-jest
处理器。babel
由JavaScript编写,其性能比GO
和Rust
等系统级语言慢。转换过程很慢(与上述系统级语言相比),这就是为什么您的测试套件缓慢运行的原因。
现在,我将使用 esbuild-jest 作为开玩笑的变压器。创建两个开玩笑的配置文件,并比较时间成本。
jest.config.esbuild.js
:jest.config.rtl.js
:使用
Esbuild-jest-jest
:时间:1.457 S < /strong>
使用
TS-JEST
:时间:11.786 S
Esbuild比Babel更快,因为它是由 去。 参见为什么Esbuild快速?
有关更多信息,请 文件以加快测试执行。为了不影响测试结果,我们使用
- no-Cache
选项将其禁用。PS
ts-jest
可能会添加esbuild
,请参阅问题。而且,看看这个评论:Jest uses babel-jest plugin to compile JavaScript code using Babel.
You can find the
ts-jest
process here, it will also use thebabel-jest
processor at the end.Babel
is written by JavaScript whose performance is slower than system-level languages such asGo
, andRust
.The transform process is slow(compared to system-level languages mentioned above), That's why your test suites running slowly.
Now I will use esbuild-jest as jest's transformer. Create two jest config files and compare the time cost.
jest.config.esbuild.js
:jest.config.rtl.js
:Using
esbuild-jest
:Time: 1.457 s
Using
ts-jest
:Time: 11.786 s
esbuild is faster than babel because it's written by Go. For more info, see Why is esbuild fast?
Jest caches transformed module files to speed up test execution. In order not to affect the test results, we use
--no-cache
option to disable it.P.S.
ts-jest
may addesbuild
, see issue. And, take a look at this comment:我本人观察到的性能通过@swc/jest
ts-jest 。
只需运行
npm install @swc/jest
,然后更新您的jest.config.js
文件,如下所示:不利的一面是它会转换打字稿代码,但不做任何类型检查因此,您将必须这样做。我发现单独进行的速度要快得多。
I myself observed performance improved by 4x using @swc/jest
ts-jest
.Just run
npm install @swc/jest
and then update yourjest.config.js
file as follow :The downside is that it transforms Typescript code but doesn't do type checking so you will have to do that aside. I found it much faster to do both separately.