1.6.17 • Published 3 months ago

@scaleleap/jest-polly v1.6.17

Weekly downloads
1,222
License
MIT
Repository
github
Last release
3 months ago

@scaleleap/jest-polly

logo

Smoothest Jest integration with PollyJS.


Integrate Jest with PollyJS for a smooth HTTP recording and playback experience for your integration tests.

List of features

Environment Variables

Polly Mode

Can be set via POLLY_MODE environment variable.

Mode can be one of the following:

  • replay: Replay responses from recordings.
  • record: Force Polly to record all requests. This will overwrite recordings that already exist.
  • passthrough: Passes all requests through directly to the server without recording or replaying.

Default: replay

Usage:

POLLY_MODE=record npm t

Record if Missing

If a request's recording is not found, pass-through to the server and record the response.

Can be set via POLLY_RECORD_IF_MISSING environment variable.

Default: false if running in CI environment or true otherwise.

Usage:

POLLY_RECORD_IF_MISSING=true npm t

Secret Sanitization

Sometimes requests and/or responses may contain secret data, such as API keys, or oAuth tokens.

To automatically sanitize the recordings, you may add a list of secrets to the config to be replaced.

See "Change PollyJS default config" section for details.

// use a Record-style config, where keys are secrets,
// and values are what they will be replaced with
jestPollyConfigService.config = {
  secrets: {
    'somepassword': 'x',
    'my-api-key': 'x',
  }
}

// or simply use an array, and everything will be replaced with `x` by default:
jestPollyConfigService.config = {
  secrets: [process.env.MY_SECRET_VALYE]
}

Code Demo

Use in all tests

In your package.json

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

Or in jest.config.js

module.exports = {
  setupFilesAfterEnv: ['@scaleleap/jest-polly'],
};

Use in a single test

In my.test.js

import '@scaleleap/jest-polly';
import fetch from 'node-fetch';

test('is ok', async () => {
  const response = await fetch('https://httpstat.us/200');
  expect(response.ok).toBe(true);
});

Using the Polly instance

Use the polly instance to change default behavior.

import { jestPollyContext } from '@scaleleap/jest-polly';
import fetch from 'node-fetch';

jestPollyContext
  .polly
  .server
  .any('https://httpstat.us/500')
  .intercept((req, res) => res.sendStatus(500));

test('is not ok', async () => {
  const response = await fetch('https://httpstat.us/500');
  expect(response.ok).not.toBe(true);
});

Change PollyJS default config

If you want to change the default config, use the following setter.

Note: the config will be merged with the default config, and not overwritten.

import { jestPollyConfigService } from '@scaleleap/jest-polly';

jestPollyConfigService.config = {
  matchRequestsBy: {
    order: false
  }
}

Download & Installation

npm i -D @scaleleap/jest-polly

Contributing

Keep it simple. Keep it minimal. Don't put every single feature just because you can.

Authors or Acknowledgments

License

This project is licensed under the MIT License

1.6.17

3 months ago

1.6.16

3 months ago

1.6.15

3 months ago

1.6.14

4 months ago

1.6.4

7 months ago

1.6.3

8 months ago

1.6.2

8 months ago

1.6.1

9 months ago

1.6.0

9 months ago

1.6.9

6 months ago

1.6.8

7 months ago

1.6.7

7 months ago

1.6.6

7 months ago

1.6.5

7 months ago

1.6.11

6 months ago

1.6.10

6 months ago

1.6.13

5 months ago

1.6.12

6 months ago

1.5.94

10 months ago

1.5.93

10 months ago

1.5.95

10 months ago

1.5.89

1 year ago

1.5.90

12 months ago

1.5.92

11 months ago

1.5.91

11 months ago

1.5.83

1 year ago

1.5.85

1 year ago

1.5.84

1 year ago

1.5.87

1 year ago

1.5.86

1 year ago

1.5.88

1 year ago

1.5.81

1 year ago

1.5.80

1 year ago

1.5.82

1 year ago

1.5.74

1 year ago

1.5.76

1 year ago

1.5.75

1 year ago

1.5.78

1 year ago

1.5.77

1 year ago

1.5.79

1 year ago

1.5.70

1 year ago

1.5.72

1 year ago

1.5.71

1 year ago

1.5.73

1 year ago

1.5.67

2 years ago

1.5.66

2 years ago

1.5.69

1 year ago

1.5.68

2 years ago

1.5.63

2 years ago

1.5.62

2 years ago

1.5.65

2 years ago

1.5.64

2 years ago

1.5.56

2 years ago

1.5.58

2 years ago

1.5.57

2 years ago

1.5.59

2 years ago

1.5.61

2 years ago

1.5.60

2 years ago

1.5.54

3 years ago

1.5.55

3 years ago

1.5.52

3 years ago

1.5.53

3 years ago

1.5.51

3 years ago

1.5.50

3 years ago

1.5.47

3 years ago

1.5.49

3 years ago

1.5.48

3 years ago

1.5.46

3 years ago

1.5.45

3 years ago

1.5.44

3 years ago

1.5.43

3 years ago

1.5.42

3 years ago

1.5.41

3 years ago

1.5.40

3 years ago

1.5.39

3 years ago

1.5.38

3 years ago

1.5.37

3 years ago

1.5.36

3 years ago

1.5.35

3 years ago

1.5.34

3 years ago

1.5.33

3 years ago

1.5.32

3 years ago

1.5.31

3 years ago

1.5.30

3 years ago

1.5.29

3 years ago

1.5.28

3 years ago

1.5.27

3 years ago

1.5.26

3 years ago

1.5.25

3 years ago

1.5.24

3 years ago

1.5.23

3 years ago

1.5.22

3 years ago

1.5.21

3 years ago

1.5.20

3 years ago

1.5.19

3 years ago

1.5.18

3 years ago

1.5.17

3 years ago

1.5.16

3 years ago

1.5.15

3 years ago

1.5.14

3 years ago

1.5.13

3 years ago

1.5.12

3 years ago

1.5.11

4 years ago

1.5.10

4 years ago

1.5.9

4 years ago

1.5.8

4 years ago

1.5.7

4 years ago

1.5.6

4 years ago

1.5.5

4 years ago

1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.0

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.2.1

4 years ago

1.0.5

4 years ago

1.0.2

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago