@spotify/polly-jest-presets

Presets for Jest that make Polly.js plug-and-play

polly-jest-presets

An opinionated configuration and wrapper around Polly and setup-polly-jest to have automatic recording and playback of network requests made during your Jest tests.

Note: Polly Jest Presets bundles in all necessary Polly packages to make the setup as easy as possible for a typical Node-based web app. It uses File persister to store recordings locally. Therefore, there's no need to install Polly or Polly Jest bindings separately in your project after including this preset.

Packages included:

"@pollyjs/adapter-node-http": "^2.6.0",
"@pollyjs/core": "^2.6.0",
"@pollyjs/persister-fs": "^2.6.0",
"setup-polly-jest": "^0.5.2",

Usage

Install the preset as a dev dependency:

yarn add @spotify/polly-jest-presets -D

Add the preset to your Jest config (by default jest.config.js), in the setupFilesAfterEnv:

{
  "setupFilesAfterEnv": ["@spotify/polly-jest-presets"]
}

Getting Started

To test it out, make a network request in one of your tests.

// `yarn add -D node-fetch` for this demo
const fetch = require('node-fetch');

describe('a dummy test', () => {
  it('fetches something', async () => {
    const resp = await fetch('https://reqres.in/api/users?page=2');
    const payload = await resp.json();
    expect(payload.data.length).toBeGreaterThan(1);
  });
});

First, you need to run the tests with the POLLY_MODE environment variable set to record. This will tell Polly that you intend for all of the requests to record in this test run.

POLLY_MODE="record" jest

You should now see a .polly_recordings directory at the root of your project. It should contain a .har file which shows the request we made within the it block.

To test that playback works, disconnect your internet on your machine and run:

POLLY_MODE="replay" jest

The test still passes! Note: the default POLLY_MODE is replay.

Configuration and API usage

If you want to override Polly configuration, you can add configuration to globals.pollyConfig in the Jest config:

{
  "globals": {
    "pollyConfig": {
      "expiresIn": "3 months"
    }
  }
}

See all of the valid Polly options in the Polly documentation.

You may also want to get at the global Polly instance. You can grab it from the global object in Node:

global.pollyContext.polly.server.get('/series')
  .intercept((req, res) => res.sendStatus(200));

See all of the Polly API methods in the Polly documentation.

Opinions

This preset has a few opinions baked in. All of these are overridable by setting the globals.pollyConfig in your Jest config.

Expire recordings often

We think it's safer to expire recordings frequently. The default expiresIn is set to "14d" in this preset, and we plan to update Polly to support throwing errors when encountering expired recordings in replay mode.

Enabling warning for recordings expiring soon

We offer a script that can be included in pre-commit hook to warn users about recordings that will expire soon. This is to encourage timely re-recording and prevent people from getting blocked because of tests failing on CI due to expired recordings.

As this script does not have access to Polly config, default values are provided and can be overridden via following environment variables:

# after how many days from last recording are recordings considered expired
POLLY_DAYS_EXPIRY=14

# how many days prior to recording expiry we should warn user about it
POLLY_DAYS_TO_WARN=3

# directory with recordings
RECORDINGS_DIR=.polly_recordings

Example package.json extract, using yarn and husky:

"husky": {
    "hooks": {
      "pre-commit": "POLLY_DAYS_EXPIRY=21 node ./node_modules/@spotify/polly-jest-presets/cjs/expiryWarning.js",
    }
}

This will yield following output in console:

Explicit recording only (no recordIf*)

We think it makes more sense to avoid recording in the background for test authoring to avoid unexpected changes to checked in .har files. Because of this, we have set all of the recordIf* config values to false. This means that your tests will fail when you first write them if you don't override POLLY_MODE.

Contributing

See CONTRIBUTING guidelines.

Repository

https://github.com/spotify/polly-jest-presets.git


上一篇:@hharnisc/turtle-race
下一篇:@jomaxx/jest-polly

相关推荐

  • 顶级测试框架Jest指南:跑通一个完美的程序,就是教出一群像样的学生

    facebook三大项目:yarn jest metro,有横扫宇宙之势。 这个项目的宗旨为:减少测试一个项目所花费的时间成本和认知成本。 ——其实,它在让你当一个好老师。

    2 年前
  • 详解如何在项目中使用jest测试react nati

    目前Javascript的测试工具很多,但是针对React的测试策略,Facebook推出的ReactJs标配测试工具是Jest.Jest的官网地址:https://facebook.github.i...

    3 年前
  • 自动化测试工具Jest的使用(持续更新)

    入门 官方文档 安装 npm init -y npm i @babel/core @babel/preset-env jest@24.8.0 -D配置 //添加.babelrc { ...

    7 个月前
  • 第十二集: 从零开始实现一套pc端vue的ui组件库( jest单元测试 )

    第十二集: 从零开始实现( jest单元测试 ) 1.聊聊测试     本次我会与大家分享一下我学测试时候记的笔记知识以及本次项目里面做的几个测试. 前端代码的单元测试与集成测试属于雷声大雨点小, 很...

    1 年前
  • 用 Jest 进行 JavaScript 测试(2019)

    作者:Valentino Gagliardi 翻译:疯狂的技术宅 原文:https://www.valentinog.com/bl... 未经允许严禁转载 测试是什么意思? 在技​​术术语中测试意...

    1 年前
  • 用 Jest 和 Enzyme 测试 React I

    原文地址: medium.com/codeclan/te… 译文地址:github.com/xiao-T/note… 本文版权归原作者所有,翻译仅用于学习。 这篇文章将会介绍如何设置并使用 J...

    5 个月前
  • 提高代码质量——使用Jest和Sinon给已有的代码添加单元测试

    概述 在日常的功能开发中,我们的代码测试都依赖于自己或者QA进行测试。这些操作不仅费时费力,而且还依赖开发者自身的驱动。在开发一些第三方依赖的库时,我们也没有办法给第三方提供完整的代码质量报告。

    2 年前
  • 在 ts + Jest 单元测试中 debugging

    1、背景 代码是 TS 写的 所测功能无 UI 界面,且出现 bug 初步定位到是循环体内部问题,功能较为复杂 用 console 式 debug 效率太低,需要打断点式调试 在 Jest 单测中...

    1 年前
  • 在 Angular 中引入 Jest 进行单元测试

    为什么要从 Karma 迁移到 Jest 用 Karma 在项目中遇到了坑 最近新换了一个项目,去的时候项目已经做了两个月了,因为前期赶功能,没有对单元测试做要求,CI/CD 的时候也没有强制跑单元测...

    1 年前
  • 单元测试与单元测试框架 Jest

    什么是单元测试? 测试是一种验证我们的代码是否可以按预期工作的手段。 被测试的对象可以是我们程序的任何一个组成部分。大到一个分为多步骤的下单流程,小到代码中的一个函数。

    2 年前

官方社区

扫码加入 JavaScript 社区