Gathering detailed insights and metrics for @nerdwallet/jest-nock-fixtures
Gathering detailed insights and metrics for @nerdwallet/jest-nock-fixtures
Gathering detailed insights and metrics for @nerdwallet/jest-nock-fixtures
Gathering detailed insights and metrics for @nerdwallet/jest-nock-fixtures
npm install @nerdwallet/jest-nock-fixtures
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2 Stars
22 Commits
4 Watching
9 Branches
5 Contributors
Updated on 13 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
16.8%
132
Compared to previous day
Last week
29.2%
995
Compared to previous week
Last month
16.2%
4,528
Compared to previous month
Last year
117%
44,292
Compared to previous year
jest-nock-fixtures is a wrapper for a jest testing environment. It uses nock
to record and playback requests during test runs. It is heavily inspired by https://github.com/nock/nock#nock-back
npm install @nerdwallet/jest-nock-fixtures
Configure jest
to setup this wrapper before the tests in each test file are executed. In jest@24
, this can be achieved by configuring setupFilesAfterEnv
(https://jestjs.io/docs/en/configuration#setupfilesafterenv-array)
Create a file to import and activate @nerdwallet/jest-nock-fixtures
, in this example named setupAfterEvvJestNockFixtures.js
activating the test wrapper
1/* setupAfterEvvJestNockFixtures.js */ 2 3const createJestNockFixturesTestWrapper = require('@nerdwallet/jest-nock-fixtures'); 4 5createJestNockFixturesTestWrapper();
optionally, the error message that is thrown in lockdown
mode can be configured. This allows you to hint at ways to fix that might be specific to the repo @nerdwallet/jest-nock-fixtures
is used in, ex:
1/* setupAfterEvvJestNockFixtures.js */ 2 3const createJestNockFixturesTestWrapper = require('@nerdwallet/jest-nock-fixtures'); 4 5createJestNockFixturesTestWrapper({ 6 unmatchedErrorMessage: (reqs, { fixtureFilepath }) => 7 `unmatched requests not allowed (found ${ 8 reqs.length 9 }).\n\nRun \`npm run test:record\` to update fixtures, and try again.` 10});
then configure jest to activate @nerdwallet/jest-nock-fixtures
and wrap each test file in nock fixture recording behavior
1// in jest config 2{ 3 // ... the rest of the jest config 4 5 // run the setup file created in the examples above 6 setupFilesAfterEnv: ['<rootDir>/setupAfterEvvJestNockFixtures.js'], 7 // ignore the folder where the fixtures are saved 8 // so they don't endlessly trigger re-runs in record mode 9 watchPathIgnorePatterns: ['__nocks__'], 10 // add the watch plugin to change modes while in --watch mode 11 // press 'r' to cycle through jest modes between runs 12 watchPlugins: ['@nerdwallet/jest-nock-fixtures/JestWatchPlugin'] 13}
Available modes:
dryrun
: The default, use recorded nocks, allow new http calls, doesn't record anything, useful for writing new testsrecord
: record new nockslockdown
: use recorded nocks, disables all http calls even when not nocked, doesn't recordwild
: all requests go out to the internet, don't replay anything, don't record anything@nerdwallet/jest-nock-fixtures
reads process.env.JEST_NOCK_FIXTURES_MODE
to control its behavior, allowing script aliases to be created, for example:
1 "scripts": { 2 "jest": "jest --coverage", 3 "test": "npm run jest --", 4 "test:wild": "JEST_NOCK_FIXTURES_MODE=wild npm run test --", 5 "test:record": "JEST_NOCK_FIXTURES_MODE=record npm run test --", 6 "test:lockdown": "JEST_NOCK_FIXTURES_MODE=lockdown npm run test --" 7 },
lockdown
mode is always used in CI environments (e.g. process.env.CI === true
).
An example workflow:
1# while developing 2npm run test -- --watch 3# when ready to push 4npm run test:record 5# commit and push the added/changed `__nocks__/*.json` fixture files 6 7# and then in CI enjoy peace of mind for consistent and reproducable test runs in the context of network requests
By default, minimal logs will be printed. To increase the verbosity of the logs, set JEST_NOCK_FIXTURES_VERBOSE
when running tests. For example:
1JEST_NOCK_FIXTURES_VERBOSE=1 npm run test
Main commands:
yarn install
: Install all dependenciesyarn test
: Run unit tests and generate coverage reportsOther commands you might care about:
yarn lint
: Run lintyarn format
: Automatically fix code issuespackage.json
. Take care to follow semantic versioning.CHANGELOG.md
to reflect the changes in the new version.master
branch.No vulnerabilities found.
No security vulnerabilities found.