模极社社区 - 制造业全产业链交流平台

    • 登录
    • 版块
    • 最新
    • 话题
    • 热门
    • 用户
    • 群组

    `@sinonjs/fake-timers`——模具行业从业者必备知识手册

    社区公告
    1
    1
    1
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • S
      system 2 最后由 编辑

      <h2>@sinonjs/fake-timers</h2>

      <p><a href="upload://tEz0gCXviVKsqN6M4CWo8X1sH7o.svg"><img src="https://circleci.com/gh/sinonjs/fake-timers" alt="CircleCI</a>"></p>
      <p><a href="upload://46ueXbt2Tr0Qczx21AwRpBtTjap.svg"><img src="https://codecov.io/gh/sinonjs/fake-timers" alt="codecov</a>"></p>
      <p><a href="CODE_OF_CONDUCT.md"><img src="upload://lkHxGIcGo50eOMGCuWB38Cks8lB.svg" alt="Contributor Covenant"></a></p>

      <p>JavaScript implementation of the timer APIs; <code>setTimeout</code>, <code>clearTimeout</code>, <code>setImmediate</code>, <code>clearImmediate</code>, <code>setInterval</code>, <code>clearInterval</code>, <code>requestAnimationFrame</code>, <code>cancelAnimationFrame</code>, <code>requestIdleCallback</code>, and <code>cancelIdleCallback</code>, along with a clock instance that controls the flow of time. FakeTimers also provides a <code>Date</code> implementation that gets its time from the clock.</p>

      <p>In addition in browser environment <code>@sinonjs/fake-timers</code> provides a <code>performance</code> implementation that gets its time from the clock. In Node environments FakeTimers provides a <code>nextTick</code> implementation that is synchronized with the clock - and a <code>process.hrtime</code> shim that works with the clock.</p>

      <p><code>@sinonjs/fake-timers</code> can be used to simulate passing time in automated tests and other</p>
      <p>situations where you want the scheduling semantics, but don't want to actually</p>
      <p>wait.</p>

      <p><code>@sinonjs/fake-timers</code> is extracted from <a href="https://github.com/sinonjs/sinon.js">Sinon.JS</a> and targets the <a href="https://sinonjs.org/releases/latest/#supported-runtimes">same runtimes</a>.</p>

      <h3>Autocomplete, IntelliSense and TypeScript definitions</h3>

      <p>Version 7 introduced JSDoc to the codebase. This should provide autocomplete and type suggestions in supporting IDEs. If you need more elaborate type support, TypeScript definitions for the Sinon projects are independently maintained by the Definitely Types community:</p>

      <pre><code>
      npm install -D @types/sinonjs__fake-timers
      </code></pre>

      <h3>Installation</h3>

      <p><code>@sinonjs/fake-timers</code> can be used in both Node and browser environments. Installation is as easy as</p>

      <pre><code>
      npm install @sinonjs/fake-timers
      </code></pre>

      <p>If you want to use <code>@sinonjs/fake-timers</code> in a browser you can either build your own bundle or use <a href="https://www.skypack.dev">Skypack</a>.</p>

      <h3>Usage</h3>

      <p>To use <code>@sinonjs/fake-timers</code>, create a new clock, schedule events on it using the timer</p>
      <p>functions and pass time using the <code>tick</code> method.</p>

      <pre><code>
      // In the browser distribution, a global FakeTimers is already available
      var FakeTimers = require("@sinonjs/fake-timers");
      var clock = FakeTimers.createClock();

      clock.setTimeout(function () {
      console.log(
      "The poblano is a mild chili pepper originating in the state of Puebla, Mexico."
      );
      }, 15);

      // ...

      clock.tick(15);
      </code></pre>

      <p>Upon executing the last line, an interesting fact about the</p>
      <p><a href="https://en.wikipedia.org/wiki/Poblano">Poblano</a> will be printed synchronously to</p>
      <p>the screen. If you want to simulate asynchronous behavior, you have to use your</p>
      <p>imagination when calling the various functions.</p>

      <p>The <code>next</code>, <code>runAll</code>, <code>runToFrame</code>, and <code>runToLast</code> methods are available to advance the clock. See the</p>
      <p>API Reference for more details.</p>

      <h4>Faking the native timers</h4>

      <p>When using <code>@sinonjs/fake-timers</code> to test timers, you will most likely want to replace the native</p>
      <p>timers such that calling <code>setTimeout</code> actually schedules a callback with your</p>
      <p>clock instance, not the browser's internals.</p>

      <p>Calling <code>install</code> with no arguments achieves this. You can call <code>uninstall</code></p>
      <p>later to restore things as they were again.</p>

      <pre><code>
      // In the browser distribution, a global FakeTimers is already available
      var FakeTimers = require("@sinonjs/fake-timers");

      var clock = FakeTimers.install();


      还有什么补充参数吗?一起完善。
      </code></pre>

      1 条回复 最后回复 回复 引用 0
      • First post
        Last post
      Powered by NodeBB | Contributors