🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis
Socket
Book a DemoInstallSign in
Socket

deep-stub-object

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deep-stub-object

Proxy-backed custom error message instead of `TypeError: x is undefined`

latest
Source
npmnpm
Version
2.0.4
Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

deep-stub-object

npm build publish codecov Type Coverage Libraries.io dependency status for latest release Bundlephobia npm

Proxy-backed custom error message instead of TypeError: x is undefined

Getting started

$ npm i deep-stub-object

Reference

export type DeepPartial<T> = T extends Record<string, unknown>
  ? {
      readonly [P in keyof T]?: DeepPartial<T[P]>
    }
  : T

export type DeepRequired<T> = T extends Record<string, unknown>
  ? {
      readonly [P in keyof T]-?: DeepRequired<T[P]>
    }
  : T

type Func<P extends readonly any[] = readonly any[], R extends any = any> = (...args: P) => R

type Nested = Func | { readonly [prop: string]: Nested }

export function deepStub<T extends Nested>(
  target: DeepPartial<T> | undefined,
  message: (path: readonly string[]) => string
): DeepRequired<T>

Usage

import 'ts-jest'
import { deepStub, DeepPartial } from 'deep-stub-object'

type Obj = {
  x: () => 1
  y: { x: () => 'test' }
  z: { y: { x: () => true } }
}

const deepStubObj = (obj?: DeepPartial<Obj>) => deepStub(obj, (path) => path.join('.'))

describe('index', () => {
  test('deepStub - throw', () => {
    const stub = deepStubObj()

    expect(() => stub.x()).toThrowError('x')
    expect(() => stub.y.x()).toThrowError('y.x')
    expect(() => stub.z.y.x()).toThrowError('z.y.x')
  })

  test('deepStub - no throw', () => {
    const stub = deepStubObj({ x: () => 1, z: { y: { x: () => true } } })

    expect(stub.x()).toEqual(1)
    expect(stub.z.y.x()).toEqual(true)
  })
})

Keywords

mocking

FAQs

Package last updated on 28 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts