Skip to content
This repository was archived by the owner on Aug 2, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
registry-url: https://registry.npmjs.org/
- run: yarn && yarn test
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn test
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules
__nocks__
.DS_Store
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__nocks__
.github
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 spring-media
Copyright (c) 2020 Sebastian Herrlinger (Port Blue Sky)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
63 changes: 51 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,86 @@
# jest-nock
_Automate request traffic recording and replay for tests._

_Automate request/SSE traffic recording and replay for tests._

![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)
[![npm version](https://badge.fury.io/js/jest-nock.svg)](https://badge.fury.io/js/jest-nock)
![Node.js CI](https://github.com/PortBlueSky/jest-nock/workflows/Node.js%20CI/badge.svg)

Instead of mocking requests or setting up nock for every test manually,
this setup allows to mark tests that make requests to other systems and
this setup allows to flag tests that make requests to other systems and
automatically record and replay requests made during a test run.
After recording the requests for your test suite, it will never make real network requests again, so you don't need these external systems available during a test run and your test suite will run much faster, while still ensuring your project integrates well will the used APIs.

## Example

```js
it.nock('uses some method that makes requests to external services', () => {
const result = await makeSomeRequestsAndCombineThem();
it.nock('uses some method that makes requests to external services', () => async () => {
const result = await makeSomeRequest();
expect(result).toBe(derivate);
});
```

## Install
```

```shell
yarn add jest-nock
```

To use `jest-nock` with your jest setup, add it to your [testFramework file setup](https://jestjs.io/docs/en/configuration.html#setuptestframeworkscriptfile-string) as follows:
To use `jest-nock` with your jest setup, add it to your [testFramework file setup](https://jestjs.io/docs/en/configuration.html#setuptestframeworkscriptfile-string). Based on what test runner you are using, you might have to tweak your setup. Have a look at how the tests are setup in this repository.

### Jasmine (default)

```js
const { upgradeJasmine } = require('jest-nock');

upgradeJasmine(jasmine, global);
upgradeJasmine(global, options);
```

### Circus

Circus does not expose a test file path like `jasmine.testPath`,
using this method `global.__TESTPATH` needs to be set manually.

```js
const { upgradeCircus } = require('jest-nock');

upgradeCircus(global, options);
```

### Global Options

- `nockOptions: Object` - see **API - Nock Options**
- `writeAfterEach: boolean` - writes out the recordings after each test case (used for testing the tool itself).
- `loadAfterEach: boolean` - loads the recordings file before each test case (used for testing the tool itself).

## Usage
- Mark a test to be recorded/replayed with `it.nock(...)`
- Set `JEST_NOCK_RECORD=true` in your environment and run the tests you want to record against some real/mock/test API.

- Mark a test to be recorded/replayed with `it.nock(...)`
- Wrap your test method in another function `it.nock(..., () => () => { ... })`
- Set `JEST_NOCK_RECORD=true` in your environment _and_ run the tests you want to record against some real/mock/test API.
- Run your tests without the env variable or `JEST_NOCK_RECORD=false` and see that no real requests are being made.

Recorded requests will be stored in a folder called `__nocks__` next to the file containing the test/suite.

## Acknowledgements
- @eplawless for djb2 hash [gist](https://gist.github.com/eplawless/52813b1d8ad9af510d85)
For detailed examples, have a look at the [tests for the tool itself](./jest-nock.test.js).

## API

### Nock Options

Options for recording and replay can be set globally via one of the setup methods (_upgradeJasmine_, _upgradeCircus_) or as the last argument to a test runner method like this:

```js
test.nock('should...', () => () => { /* ... */ }, nockOptions);
```

**Options:**

- `removeHeaders: string[]` - removes the specified headers from API recordings and will therefor not replay them.
- `title: string` - overrides the title for the test, which will be used as a key in the recordings (used for testing the tool itself).

---
Copyright 2018 Sebastian Herrlinger (SPRING Axel Springer Digital News Media GmbH)
Copyright 2020 Sebastian Herrlinger ([Port Blue Sky](https://www.portbluesky.com/))

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
21 changes: 21 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import nock from "nock/types";

type FnFn = () => () => void;
type NockTest = (title: string, fn: FnFn, timeout?: number) => void;

declare global {
namespace jest {
interface It {
nock: NockTest;
}
}
}

type Options = {
writeAfterEach?: boolean;
loadAfterEach?: boolean;
nockOptions?: nock.Options;
};

export function upgradeJasmine(global: typeof globalThis, options?: Options): void;
export function upgradeCircus(global: typeof globalThis, options?: Options): void;
Loading