最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can jestjs test a function that uses performance.now()? - Stack Overflow

programmeradmin2浏览0评论

I'm writing a jestjs test for a ES6 class that uses performance.now(). But it doesn't seem to work.

Is there a way to user perf-hooks globally from the jest.config.js? Or a way to mock performance and override it with eg Date?

I've tried overriding performance on classToTest.js with Date but since it uses performancealready on import that doesn't work.

Simplified examples:

classToTest.test.js

import ClassToTest from "./classToTest";

test("constructor works", () => {
  expect(new ClassToTest()).not.toBeNull();
});

classToTest.js

const timer = performance.now();

class ClassToTest {
...

The output from jest is ReferenceError: performance is not defined.

I'm writing a jestjs test for a ES6 class that uses performance.now(). But it doesn't seem to work.

Is there a way to user perf-hooks globally from the jest.config.js? Or a way to mock performance and override it with eg Date?

I've tried overriding performance on classToTest.js with Date but since it uses performancealready on import that doesn't work.

Simplified examples:

classToTest.test.js

import ClassToTest from "./classToTest";

test("constructor works", () => {
  expect(new ClassToTest()).not.toBeNull();
});

classToTest.js

const timer = performance.now();

class ClassToTest {
...

The output from jest is ReferenceError: performance is not defined.

Share Improve this question edited Mar 2, 2023 at 12:38 sideshowbarker 88.2k29 gold badges215 silver badges211 bronze badges asked Sep 5, 2019 at 19:22 JonasJonas 731 gold badge1 silver badge3 bronze badges 3
  • did you import performance in your classToTest.js? const { performance } = require('perf_hooks') – shkaper Commented Sep 5, 2019 at 23:32
  • Yes that works. Also changing to Date.now() works. But I'd like to be able to test the source code as it is and runs in it's environment, not adapt it to the test environment. – Jonas Commented Sep 6, 2019 at 13:14
  • I'm sorry, I still don't get it. Let's forget about your test for a moment. Assuming ClassToTest code works and is a black box for us, how can it use performance without importing it in the first place? – shkaper Commented Sep 6, 2019 at 16:18
Add a ment  | 

2 Answers 2

Reset to default 12

If your Jest test environment is jsdom (the default) then it provides a browser-like environment that includes a mock for performance on the global Window object, so performance.now will be defined automatically.

If your Jest test environment is node then you will need to provide your own performance global.

That can be done by adding a setup file to the setupFilesAfterEnv array:

jest.config.js

module.exports = {
  setupFilesAfterEnv: [ './setup.js' ]
}

...and within the setup file defining a global performance:

setup.js

global.performance = require('perf_hooks').performance;

Update 2024

With the latest jest version performance.now() can be tweaked using fake timers.

Here is an example:

jest.useFakeTimers({
    now: new Date("2024-02-07T00:00:01.123Z"),
});
const start = performance.now();

jest.advanceTimersByTime(1234.5678);
const end = performance.now();

expect(end - start).toBe(1234.5678);
发布评论

评论列表(0)

  1. 暂无评论