Skip to content

Commit

Permalink
feat: improve minimization
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 17, 2022
1 parent 4e53f3a commit ac0efa5
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> minimal fork of nanospy, with more features 🕵🏻‍♂️
A `7KB` package for minimal and easy testing with no dependencies.
A `8KB` package for minimal and easy testing with no dependencies.
This package was created for having a tiny spy library to use in `vitest`, but it can also be used in `jest` and other test environments.

## Installing
Expand Down
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,35 @@
"dev": "tsup --watch",
"build": "tsup",
"prepare": "husky install",
"publish": "clean-publish",
"publish": "npm run build && clean-publish",
"release": "bumpp package.json --commit --push --tag && npm run publish",
"test": "vitest --no-threads"
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
"require": "./dist/index.cjs",
"default": "./dist/index.cjs"
},
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/**"
],
"repository": {
"type": "git",
"url": "git+https://github.com/aslemammad/tinyspy.git"
"url": "git+https://github.com/tinylibs/tinyspy.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/aslemammad/tinyspy/issues"
"url": "https://github.com/tinylibs/tinyspy/issues"
},
"homepage": "https://github.com/aslemammad/tinyspy#readme",
"homepage": "https://github.com/tinylibs/tinyspy#readme",
"devDependencies": {
"@size-limit/preset-small-lib": "^7.0.4",
"@size-limit/time": "^7.0.4",
"bumpp": "^8.2.0",
"clean-publish": "^3.4.4",
"happy-dom": "^2.25.1",
"husky": "^7.0.4",
Expand All @@ -52,4 +56,4 @@
"engines": {
"node": ">=14.0.0"
}
}
}
10 changes: 5 additions & 5 deletions src/spy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, isType } from './utils'
import { assert, define, isType } from './utils'

export let spies = new Set<SpyImpl>()

Expand Down Expand Up @@ -83,14 +83,14 @@ export function spy<A extends any[], R>(cb?: (...args: A) => R): SpyFn<A, R> {
return result
} as SpyFn<A, R>

Object.defineProperty(fn, '_isMockFunction', { get: () => true })
Object.defineProperty(fn, 'length', { value: cb ? cb.length : 0 })
Object.defineProperty(fn, 'returns', {
define(fn, '_isMockFunction', { get: () => true })
define(fn, 'length', { value: cb ? cb.length : 0 })
define(fn, 'returns', {
get(this: SpyFn<A, R>) {
return this.results.map(([, r]) => r)
},
})
Object.defineProperty(fn, 'name', { value: cb ? cb.name || 'spy' : 'spy' })
define(fn, 'name', { value: cb ? cb.name || 'spy' : 'spy' })
const reset = () => {
fn.called = false
fn.callCount = 0
Expand Down
10 changes: 5 additions & 5 deletions src/spyOn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spy, spies, SpyImpl } from './spy'
import { assert, isType } from './utils'
import { assert, define, isType } from './utils'

type Procedure = (...args: any[]) => any

Expand Down Expand Up @@ -107,7 +107,7 @@ export function spyOn<T, K extends string & keyof T>(
if (!mock) mock = origin

let fn = spy(mock) as unknown as SpyImpl
let define = (cb: any) => {
let reassign = (cb: any) => {
let { value, ...desc } = descriptor || {
configurable: true,
writable: true,
Expand All @@ -116,17 +116,17 @@ export function spyOn<T, K extends string & keyof T>(
delete desc.writable // getter/setter can't have writable attribute at all
}
;(desc as PropertyDescriptor)[accessType] = cb
Object.defineProperty(obj, accessName, desc)
define(obj, accessName, desc)
}
let restore = () => define(origin)
let restore = () => reassign(origin)
fn.restore = restore
fn.getOriginal = () => (ssr ? origin() : origin)
fn.willCall = (newCb: Procedure) => {
fn.impl = newCb
return fn
}

define(ssr ? () => fn : fn)
reassign(ssr ? () => fn : fn)

spies.add(fn)
return fn
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ export function assert(condition: any, message: string) {
export function isType(type: string, value: any) {
return typeof value === type
}

export function define(
obj: any,
key: string | number | symbol,
descriptor: PropertyDescriptor
) {
Object.defineProperty(obj, key, descriptor)
}
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"compilerOptions": {
"target": "esnext",
"strict": true,
"jsx": "react-jsx",
"esModuleInterop": true,
"moduleResolution": "node",
"noUncheckedIndexedAccess": true,
Expand Down
5 changes: 4 additions & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ export default defineConfig({
outDir: 'dist',
format: ['esm', 'cjs'],
tsconfig: './tsconfig.json',
target: 'node14',
target: 'es2018',
minify: false,
minifySyntax: true,
minifyWhitespace: false,
minifyIdentifiers: true,
clean: true,
dts: true,
})
75 changes: 74 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
# yarn lockfile v1


"@jsdevtools/ez-spawn@^3.0.4":
version "3.0.4"
resolved "https://registry.yarnpkg.com/@jsdevtools/ez-spawn/-/ez-spawn-3.0.4.tgz#5641eb26fee6d31ec29f6788eba849470c52c7ff"
integrity sha512-f5DRIOZf7wxogefH03RjMPMdBF7ADTWUMoOs9kaJo06EfwF+aFhMZMDZxHg/Xe12hptN9xoZjGso2fdjapBRIA==
dependencies:
call-me-maybe "^1.0.1"
cross-spawn "^7.0.3"
string-argv "^0.3.1"
type-detect "^4.0.8"

"@nodelib/[email protected]":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand Down Expand Up @@ -216,6 +226,18 @@ buffer@^5.2.1, buffer@^5.5.0:
base64-js "^1.3.1"
ieee754 "^1.1.13"

bumpp@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/bumpp/-/bumpp-8.2.0.tgz#0e7684417edcec60d4fd66ed1017a4271c292b8c"
integrity sha512-DhFa4DSREOwnHJQyQ+Qhj9pUJAIZKMEtuHN7BuQij4CoxSBc6n4bBZnlvFWuLXNMr0aCtsMXnkQWC/7GyGypYw==
dependencies:
"@jsdevtools/ez-spawn" "^3.0.4"
cac "^6.7.12"
fast-glob "^3.2.11"
kleur "^4.1.4"
prompts "^2.4.1"
semver "^7.3.5"

bundle-require@^2.1.8:
version "2.1.8"
resolved "https://registry.yarnpkg.com/bundle-require/-/bundle-require-2.1.8.tgz#28f6de9d4468a6b7b76fb5c9bf52e70f5091245d"
Expand All @@ -239,6 +261,11 @@ call-bind@^1.0.0:
function-bind "^1.1.1"
get-intrinsic "^1.0.2"

call-me-maybe@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/call-me-maybe/-/call-me-maybe-1.0.1.tgz#26d208ea89e37b5cbde60250a15f031c16a4d66b"
integrity sha512-wCyFsDQkKPwwF8BDwOiWNx/9K45L/hvggQiDbve+viMNMQnWhrlYIuBk09offfwCRtCO9P6XwUttufzU11WCVw==

caseless@^0.12.0, caseless@~0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
Expand Down Expand Up @@ -678,6 +705,17 @@ fast-glob@^3.1.1, fast-glob@^3.2.7:
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-glob@^3.2.11:
version "3.2.11"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.11.tgz#a1172ad95ceb8a16e20caa5c5e56480e5129c1d9"
integrity sha512-xrO3+1bxSo3ZVHAnqzyuewYT6aMFHRAd4Kcs92MAonjwQZLsK9d0SF1IyQ3k5PoirxTW0Oe/RqFgMQ6TcNE5Ew==
dependencies:
"@nodelib/fs.stat" "^2.0.2"
"@nodelib/fs.walk" "^1.2.3"
glob-parent "^5.1.2"
merge2 "^1.3.0"
micromatch "^4.0.4"

fastq@^1.6.0:
version "1.13.0"
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.13.0.tgz#616760f88a7526bdfc596b7cab8c18938c36b98c"
Expand Down Expand Up @@ -1009,6 +1047,16 @@ jsonfile@^6.0.1:
optionalDependencies:
graceful-fs "^4.1.6"

kleur@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==

kleur@^4.1.4:
version "4.1.4"
resolved "https://registry.yarnpkg.com/kleur/-/kleur-4.1.4.tgz#8c202987d7e577766d039a8cd461934c01cda04d"
integrity sha512-8QADVssbrFjivHWQU7KkMgptGTl6WAcSdlbBPY4uNF+mWr6DGcKrvY2w4FQJoXch7+fKMjj0dRrL75vk3k23OA==

lilconfig@^2.0.3, lilconfig@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/lilconfig/-/lilconfig-2.0.4.tgz#f4507d043d7058b380b6a8f5cb7bcd4b34cee082"
Expand Down Expand Up @@ -1313,6 +1361,14 @@ promise@^8.0.0:
dependencies:
asap "~2.0.6"

prompts@^2.4.1:
version "2.4.2"
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069"
integrity sha512-NxNv/kLguCA7p3jE8oL2aEBsrJWgAakBpgmgK6lpPWV+WuOmY6r2/zbAVnP+T8bQlA0nzHXSJSJW0Hq7ylaD2Q==
dependencies:
kleur "^3.0.3"
sisteransi "^1.0.5"

[email protected]:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
Expand Down Expand Up @@ -1455,6 +1511,13 @@ [email protected]:
dependencies:
lru-cache "^6.0.0"

semver@^7.3.5:
version "7.3.7"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f"
integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==
dependencies:
lru-cache "^6.0.0"

shebang-command@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea"
Expand All @@ -1481,6 +1544,11 @@ signal-exit@^3.0.3:
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.6.tgz#24e630c4b0f03fea446a2bd299e62b4a6ca8d0af"
integrity sha512-sDl4qMFpijcGw22U5w63KmD3cZJfBuFlVNbVMKje2keoKML7X2UzWbc4XrmEbDwg0NXJc3yv4/ox7b+JWb57kQ==

sisteransi@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"
integrity sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==

size-limit@^7.0.4:
version "7.0.4"
resolved "https://registry.yarnpkg.com/size-limit/-/size-limit-7.0.4.tgz#e427c696b63e29096be97a576cd7fd95a3ec7839"
Expand Down Expand Up @@ -1510,6 +1578,11 @@ source-map@^0.7.3:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==

string-argv@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/string-argv/-/string-argv-0.3.1.tgz#95e2fbec0427ae19184935f816d74aaa4c5c19da"
integrity sha512-a1uQGz7IyVy9YwhqjZIZu1c8JO8dNIe20xBmSS6qu9kv++k3JGzCVmprbNN5Kn+BgzD5E7YYwg1CcjuJMRNsvg==

string_decoder@^1.1.1:
version "1.3.0"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.3.0.tgz#42f114594a46cf1a8e30b0a84f56c78c3edac21e"
Expand Down Expand Up @@ -1676,7 +1749,7 @@ tsup@^5.11.7:
sucrase "^3.20.3"
tree-kill "^1.2.2"

type-detect@^4.0.0, type-detect@^4.0.5:
type-detect@^4.0.0, type-detect@^4.0.5, type-detect@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==
Expand Down

0 comments on commit ac0efa5

Please sign in to comment.