分析基于TS的Jest测试

rjee0c15  于 2022-12-08  发布在  Jest
关注(0)|答案(1)|浏览(167)

我有一个基于Typescript的React项目,我正在其中运行jest测试(也在TS中)。我可以很好地运行测试,但我试图分析一些需要相当长时间运行的性能。我试过使用Chrome Devtools to attach to the tests,它确实如此,但是它失败了,因为它是TS而不是普通的Js。有没有什么方法可以单独分析我的测试,以查看性能问题发生在哪里?使用VS程式码。

kmpatx3s

kmpatx3s1#

它不是一个React项目,对我来说只是一个普通的TypeScript库,但我敢打赌它也适用于您的用例。
我发现唯一有效的解决方案是手动设置分析器v8-profiler-next

import v8Profiler from 'v8-profiler-next';
v8Profiler.setGenerateType(1);
const title = 'good-name';

describe('Should be able to generate with inputs', () => {
  v8Profiler.startProfiling(title, true);
  afterAll(() => {
    const profile = v8Profiler.stopProfiling(title);
    profile.export(function (error, result: any) {
      // if it doesn't have the extension .cpuprofile then
      // chrome's profiler tool won't like it.
      // examine the profile:
      //   Navigate to chrome://inspect
      //   Click Open dedicated DevTools for Node
      //   Select the profiler tab
      //   Load your file
      fs.writeFileSync(`${title}.cpuprofile`, result);
      profile.delete();
    });
  });
  test('....', async () => {
    // Add test
  });
});

然后,这将为您提供这样的CPU配置文件,它可以很好地与TypeScript配合使用。

相关问题