如何改善反应测试库速度?

发布于 2025-02-13 00:15:26 字数 545 浏览 0 评论 0原文

我注意到我的第一个测试需要大约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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可爱咩 2025-02-20 00:15:27

玩笑使用 babel-jest 插件以使用babel来编译Javascript代码。

您可以找到ts-jest process 在这里它还将在最后使用babel-jest处理器。

babel由JavaScript编写,其性能比GORust等系统级语言慢。

转换过程很慢(与上述系统级语言相比),这就是为什么您的测试套件缓慢运行的原因。

现在,我将使用 esbuild-jest 作为开玩笑的变压器。创建两个开玩笑的配置文件,并比较时间成本。

jest.config.esbuild.js

module.exports = {
  testEnvironment: 'jsdom',
  transform: {
    '^.+\\.tsx?

jest.config.rtl.js

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ['jest-extended'],
  setupFiles: ['./jest.setup.js'],
};

使用 Esbuild-jest-jest

> jest --config jest.config.esbuild.js "--no-cache" "/workspaces/jest-v26-codelab/stackoverflow/72897761/routes.test.tsx"

 PASS  stackoverflow/72897761/routes.test.tsx
  first
    ✓ Should test 404 route (32 ms)
    ✓ should render home page (10 ms)
    ✓ should render account detail page (3 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.457 s

时间:1.457 S < /strong>

使用 TS-JEST

> jest --config jest.config.rtl.js "--no-cache" "/workspaces/jest-v26-codelab/stackoverflow/72897761/routes.test.tsx"

 PASS  stackoverflow/72897761/routes.test.tsx (11.246 s)
  first
    ✓ Should test 404 route (32 ms)
    ✓ should render home page (8 ms)
    ✓ should render account detail page (4 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        11.786 s

时间:11.786 S

Esbuild比Babel更快,因为它是由 去。 参见为什么Esbuild快速?

有关更多信息,请 文件以加快测试执行。为了不影响测试结果,我们使用 - no-Cache 选项将其禁用。

PS ts-jest 可能会添加 esbuild ,请参阅问题。而且,看看这个评论:

还不存在,它将被选为实验特征。 Esbuild不支持某些我们需要谨慎采用的特定特定功能。

: 'esbuild-jest', }, setupFilesAfterEnv: ['jest-extended'], // coverageProvider: 'v8', setupFilesAfterEnv: ['./jest.setup.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,请参阅问题。而且,看看这个评论:

还不存在,它将被选为实验特征。 Esbuild不支持某些我们需要谨慎采用的特定特定功能。

Jest uses babel-jest plugin to compile JavaScript code using Babel.

You can find the ts-jest process here, it will also use the babel-jest processor at the end.

Babel is written by JavaScript whose performance is slower than system-level languages such as Go, and Rust.

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:

module.exports = {
  testEnvironment: 'jsdom',
  transform: {
    '^.+\\.tsx?

jest.config.rtl.js:

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ['jest-extended'],
  setupFiles: ['./jest.setup.js'],
};

Using esbuild-jest:

> jest --config jest.config.esbuild.js "--no-cache" "/workspaces/jest-v26-codelab/stackoverflow/72897761/routes.test.tsx"

 PASS  stackoverflow/72897761/routes.test.tsx
  first
    ✓ Should test 404 route (32 ms)
    ✓ should render home page (10 ms)
    ✓ should render account detail page (3 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.457 s

Time: 1.457 s

Using ts-jest:

> jest --config jest.config.rtl.js "--no-cache" "/workspaces/jest-v26-codelab/stackoverflow/72897761/routes.test.tsx"

 PASS  stackoverflow/72897761/routes.test.tsx (11.246 s)
  first
    ✓ Should test 404 route (32 ms)
    ✓ should render home page (8 ms)
    ✓ should render account detail page (4 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        11.786 s

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 add esbuild, see issue. And, take a look at this comment:

Not there yet, it will be opted in as experimental feature. esbuild doesn't support some TypeScript specific features which we need to be careful with adoption.

: 'esbuild-jest', }, setupFilesAfterEnv: ['jest-extended'], // coverageProvider: 'v8', setupFilesAfterEnv: ['./jest.setup.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 add esbuild, see issue. And, take a look at this comment:

Not there yet, it will be opted in as experimental feature. esbuild doesn't support some TypeScript specific features which we need to be careful with adoption.

许一世地老天荒 2025-02-20 00:15:27

我本人观察到的性能通过@swc/jest ts-jest 。
只需运行npm install @swc/jest,然后更新您的jest.config.js文件,如下所示:

transform: {
  "^.+\\.(j|t)sx?$": '@swc/jest',
}

不利的一面是它会转换打字稿代码,但不做任何类型检查因此,您将必须这样做。我发现单独进行的速度要快得多。

I myself observed performance improved by 4x using @swc/jest ts-jest.
Just run npm install @swc/jest and then update your jest.config.js file as follow :

transform: {
  "^.+\\.(j|t)sx?
quot;: '@swc/jest',
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文