Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jest-package-audit

xeroxinteractive695MIT5.0.4TypeScript support: included

Filter and retry yarn/npm audit command with Jest.

jest, matcher, package, audit, npm audit, yarn audit, testing, assertion

readme

jest-package-audit

Filter and retry yarn/npm audit command with Jest.

ci status npm package license MIT auto tested with jest

The yarn audit, and npm audit commands are useful for detecting packages in use that have vulnerabilites. But they don't allow specific package filtering. For example you may have a vulnerability in a package you are only using in development, and the nature of that vulnerability is more often than not only unsafe when used in production. Updating the dependency to fix the vulnerability may break things. That is where jest-package-audit comes in, it wraps the yarn audit and npm audit commands and checks each vulnerabilty they flag against an array of allowed vulnerability names e.g. ['puppeteer'].

Another added benefit of jest-package-audit is the ability to retry tests if they fail. This is useful as the audit endpoints can sometimes timeout out or randomly give 503 HTTP Status codes back. Using jest.retryTimes you can overcome this by retrying say 5 times.

Usage

Important: jest-package-audit only works with Jest >= 23 as it depends on async matchers.

  1. Install jest-package-audit:
    yarn add jest-package-audit --dev
    # or
    npm install jest-package-audit --save-dev
  2. Create a new test file for package auditing: `javascript // audit.test.js import { toPassPackageAudit } from 'jest-package-audit'; expect.extend({ toPassPackageAudit });

jest.retryTimes(5); // Optional jest.setTimeout(15000); // The audit command can take a while...

test('packages do not have vunerabilities', async () => { await expect({/* Input options /}).toPassPackageAudit({ allow: ['puppeteer'] / Output options */ }); });

test('packages do not have vunerabilities using predicate function', async () => { await expect({/* Input options /}).toPassPackageAudit({ allow: (options) => { if (options.packageName === 'puppeteer' && options.packageSeverity === 'low') { return true; } else { return false; } } / Output options */ }); }); `

Options

Input Options

Input options should be passed to the expect function when using toPassPackageAudit, they define how the actual yarn audit or npm audit command is run.

Name Description Default
cwd: (String) Current working directory to run the audit command in. The closest folder with a package.json above jest-package-audit.
packageManager ('npm' or 'yarn' or 'pnpm') Which package manager to use, this will be auto-detected if not specified, also it will determine how the audit output is parsed when using command. Based on relevant lockfile existance
level: ('info' or 'low' or 'moderate' or 'high' or 'critical') Limit the vulnerabilities to the given level and above. (Note: npm and pnpm does not support info, so it will not be passed forward)
dependencyType: ('dependencies' or 'devDependencies') Limit the vulnerabilities to the projects development or production dependencies.
command (String) Custom command to use. This will override the yarn, level and dependencyType options. Use this with caution!

Note: level and dependencyType are passed forward to yarn and npm in their respective formats. Unless, command is specified,

Output Options

Output options should be passed to the toPassPackageAudit function, they define how the output of yarn audit or npm audit is processed.

Name Description Default
allow: (String[] | (vulnerability: {packageName: string; packageSeverity: string; packageData: Object}) => boolean) An array of package names to allow if they have vulnerabilities or a single callback predicate function. []

Disclaimer

Please be aware that we provide no liability for any security issues, or any other issues for that matter, encountered when using this package. It is provided as open-source software under the MIT license. So please read the source code and make sure you understand the implications of allowing vulnerable modules to pass through the audit commands!


LICENSE | CHANGELOG | ISSUES | CONTRIBUTING

changelog

v5.0.4 (Fri Nov 10 2023)

Fix

Dependencies

Authors: 2


v5.0.3 (Wed Jan 04 2023)

Fix

Dependencies

Authors: 2


v5.0.2 (Thu Dec 01 2022)

Fix

Authors: 1


v5.0.1 (Thu Dec 01 2022)

Fix

Dependencies

Authors: 2


v5.0.0 (Wed Jul 20 2022)

Breaking Change

Feature

Fix

Dependencies

Authors: 3


4.0.1 (2022-01-05)

Bug Fixes

  • allow checking pass condition (5011f91)

4.0.0 (2022-01-05)

  • Open up node version range + dependency updates + allow function + Node 14/16 + JSON output parsing (#523) (43a1edb), closes #523 #588 #589

Bug Fixes

Features

BREAKING CHANGES

  • Node.js > 14 now required.

Co-authored-by: luxtron rafael.almeria@xerox.com

  • Node.js > 14 now required.

Co-authored-by: luxtron rafael.almeria@xerox.com

3.2.0 (2020-10-27)

Features

3.1.2 (2020-07-31)

3.1.1 (2020-07-03)

Bug Fixes

  • manually filter severity levels (8da14ae)

3.1.0 (2020-07-02)

Bug Fixes

  • make toPassPackageAudit type asynchronous (72c1b0c)

Features

  • add yarn, level and dependencyType options (d493199)
  • automatically switch between yarn and npm (c9bf19b)

3.0.2 (2020-06-30)

Bug Fixes

  • add regex support for plain report tables (9316d9d)
  • clamp whitespace checking (cba4a37)

3.0.1 (2020-05-16)

Bug Fixes

  • add jest as peer dependency + jest 26 (38e31ca)

3.0.0 (2020-03-24)

chore

BREAKING CHANGES

  • @xerox/ prefix removed from npm package going forward

2.0.5 (2019-12-13)

2.0.4 (2019-11-18)

2.0.3 (2019-11-05)

Bug Fixes

  • types: match new jest typings (7a64df3)

2.0.2 (2019-08-28)

Bug Fixes

2.0.1 (2019-07-17)

Bug Fixes

2.0.0 (2019-07-11)

Features

BREAKING CHANGES

  • npm module name changed from browserslist-browserstack to @xerox/browserslist-browserstack

1.0.4 (2019-06-19)

Bug Fixes

  • remove circleci-bin due to windows compat (7160cbc)

1.0.3 (2019-05-24)

Bug Fixes

  • allow filtering + better exit code handling (85406d7)

1.0.2 (2019-05-23)

Code Refactoring

  • single file exports matcher (5ac17da)

BREAKING CHANGES

  • toPassPackageAudit is no longer automatically extended onto jest, using setupFilesAfterEnv will no longer work.

1.0.1 (2019-05-22)

Bug Fixes

  • use cross-spawn for windows compat (0cf82c3)
  • windows cross-spawn mocking (219c576)

1.0.0 (2019-05-21)

Bug Fixes

Features

  • initial implementation (7de24fc)
  • log error if jest can’t be extended (9e2503d)