{"id":15374645,"url":"https://github.com/nktnet1/jewire","last_synced_at":"2026-03-14T20:10:00.976Z","repository":{"id":199614195,"uuid":"702447792","full_name":"nktnet1/jewire","owner":"nktnet1","description":"Access private functions, variables and classes from CommonJS modules . Enable named exports with relative paths identical to CommonJS require. Clone objects at runtime to remove false negatives in expect.toStrictEqual","archived":false,"fork":false,"pushed_at":"2025-01-28T14:48:17.000Z","size":809,"stargazers_count":2,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-15T14:14:24.804Z","etag":null,"topics":["dependency","fake","hidden","import","jest","mock","modules","private","require","rewire","test","unit"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/jewire","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nktnet1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-10-09T10:36:27.000Z","updated_at":"2024-11-27T04:58:24.000Z","dependencies_parsed_at":"2023-10-24T22:45:07.219Z","dependency_job_id":"1493d30a-3962-4ee0-aa82-ea970b925496","html_url":"https://github.com/nktnet1/jewire","commit_stats":null,"previous_names":["nktnet1/jewire"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/nktnet1/jewire","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nktnet1%2Fjewire","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nktnet1%2Fjewire/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nktnet1%2Fjewire/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nktnet1%2Fjewire/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nktnet1","download_url":"https://codeload.github.com/nktnet1/jewire/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nktnet1%2Fjewire/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260427400,"owners_count":23007503,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency","fake","hidden","import","jest","mock","modules","private","require","rewire","test","unit"],"created_at":"2024-10-01T13:59:19.247Z","updated_at":"2026-03-14T20:10:00.970Z","avatar_url":"https://github.com/nktnet1.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# [![Jewire](logo.svg)](https://github.com/nktnet1/jewire)\n\n[![pipeline](https://github.com/nktnet1/jewire/actions/workflows/pipeline.yml/badge.svg)](https://github.com/nktnet1/jewire/actions/workflows/pipeline.yml)\n\u0026nbsp;\n[![codecov](https://codecov.io/gh/nktnet1/jewire/branch/main/graph/badge.svg?token=RAC7SKJTGU)](https://codecov.io/gh/nktnet1/jewire)\n\u0026nbsp;\n[![Maintainability](https://api.codeclimate.com/v1/badges/2cc2478a1b5a2f293149/maintainability)](https://codeclimate.com/github/nktnet1/jewire/maintainability)\n\u0026nbsp;\n[![Snyk Security](https://snyk.io/test/github/nktnet1/jewire/badge.svg)](https://snyk.io/test/github/nktnet1/jewire)\n\u0026nbsp;\n[![GitHub top language](https://img.shields.io/github/languages/top/nktnet1/jewire)](https://github.com/search?q=repo%3Anktnet1%2Fjewire++language%3ATypeScript\u0026type=code)\n\n[![NPM Version](https://img.shields.io/npm/v/jewire?logo=npm)](https://www.npmjs.com/package/jewire?activeTab=versions)\n\u0026nbsp;\n[![install size](https://packagephobia.com/badge?p=jewire)](https://packagephobia.com/result?p=jewire)\n\u0026nbsp;\n[![Depfu Dependencies](https://badges.depfu.com/badges/6c4074c4d23ad57ee2bfd9ff90456090/overview.svg)](https://depfu.com/github/nktnet1/jewire?project_id=39032)\n\u0026nbsp;\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fnktnet1%2Fjewire.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fnktnet1%2Fjewire?ref=badge_shield)\n\u0026nbsp;\n[![NPM License](https://img.shields.io/npm/l/jewire)](https://opensource.org/license/mit/)\n\u0026nbsp;\n[![GitHub issues](https://img.shields.io/github/issues/nktnet1/jewire.svg?style=social)](https://github.com/nktnet1/jewire/issues)\n\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nktnet1_jewire\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=nktnet1_jewire)\n\u0026nbsp;\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/65161ae4d1c646ed83c9ef47b0a11473)](https://app.codacy.com/gh/nktnet1/jewire/dashboard?utm_source=gh\u0026utm_medium=referral\u0026utm_content=\u0026utm_campaign=Badge_grade)\n\u0026nbsp;\n[![DeepSource](https://app.deepsource.com/gh/nktnet1/jewire.svg/?label=active+issues\u0026show_trend=true\u0026token=OTP6tE2be4X1kvxZRsxRh25e)](https://app.deepsource.com/gh/nktnet1/jewire/)\n\u0026nbsp;\n[![codebeat badge](https://codebeat.co/badges/8bdb4562-0492-4c1c-8b02-e69c94373d60)](https://codebeat.co/projects/github-com-nktnet1-jewire-main)\n\u0026nbsp;\n[![GitHub stars](https://img.shields.io/github/stars/nktnet1/jewire.svg?style=social)](https://github.com/nktnet1/jewire/stargazers)\n\n[![Downloads Total](https://badgen.net/npm/dt/jewire)](https://moiva.io/?npm=jewire)\n\u0026nbsp;\n[![Downloads Yearly](https://badgen.net/npm/dy/jewire)](https://moiva.io/?npm=jewire)\n\u0026nbsp;\n[![Downloads Monthly](https://badgen.net/npm/dm/jewire)](https://moiva.io/?npm=jewire)\n\u0026nbsp;\n[![Downloads Weekly](https://badgen.net/npm/dw/jewire)](https://moiva.io/?npm=jewire)\n\u0026nbsp;\n[![Downloads Daily](https://badgen.net/npm/dd/jewire)](https://moiva.io/?npm=jewire)\n\n---\n\nAccess private functions, variables and classes from CommonJS modules\n\nEnable named exports with relative paths identical to CommonJS [require](https://nodejs.org/api/modules.html#requireid)\n\nClone objects at runtime to remove false negatives in expect[.toStrictEqual](https://jestjs.io/docs/expect#tostrictequalvalue)\n\n[![Try with Replit](https://replit.com/badge?caption=Try%20with%20Replit)](https://replit.com/@nktnet1/jewire-example#index.js)\n\n\u003c/div\u003e\n\n---\n\n- [1. Installation](#1-installation)\n- [2. Usage](#2-usage)\n    - [2.1. relativePath](#21-relativepath)\n    - [2.2. options](#22-options)\n    - [2.3. return](#23-return)\n- [3. License](#3-license)\n- [4. Limitations](#4-limitations)\n- [5. Caveats](#5-caveats)\n    - [5.1. Purpose](#51-purpose)\n    - [5.2. Rationale](#52-rationale)\n    - [5.3. Rewire and Jest](#53-rewire-and-jest)\n\n## 1. Installation\n\n```\nnpm install jewire\n```\n\n## 2. Usage\n\nTry with [Replit](https://replit.com/@nktnet1/jewire-example#index.js).\n\n```\njewire(relativePath, options);\n```\n\n\u003cdetails closed\u003e\n\u003csummary\u003eExamples (click to view)\u003c/summary\u003e\n\n\u003cbr/\u003e\n\nImporting from the same directory\n\n```javascript\nconst { privateVariable, privateFunction } = jewire('private-module');\n```\n\nImporting `.cjs` file from a different directory\n\n```javascript\nconst { privateFunction  } = jewire('../src/private-module.cjs');\n```\n\nUsing a different basePath\n\n```javascript\nconst { privateFunction } = jewire(\n  'private-module',\n  { basePath: process.cwd() }\n);\n```\n\nUsing a different clone function from the default `clone.objectClone`:\n\n```javascript\nconst { privateFunction } = jewire(\n  'private-module',\n  { objectClone: (obj) =\u003e JSON.parse(JSON.stringify(obj)) }\n);\n```\n\n\u003c/details\u003e\n\n\u003cbr/\u003e\n\n### 2.1. Parameter: relativePath\n\nPath to the module relative to the current file, similar to CommonJS [require](https://nodejs.org/api/modules.html#requireid). For example,\n- `'../animals/cats.js'`\n- `'./common.cjs'`\n- `'minimal'`\n    -  `jewire` will look for `'./minimal.js'` before `'./minimal.cjs'`\n\nNote that `option.basePath` can be provided to alter this behaviour.\n\n### 2.2. Parameter: options\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003eOption\u003c/th\u003e\n    \u003cth\u003eDescription\u003c/th\u003e\n    \u003cth\u003eExample\u003c/th\u003e\n    \u003cth\u003eDefault\u003c/th\u003e\n  \u003c/tr\u003e\n\n  \u003ctr\u003e\n    \u003ctd\u003ebasePath\u003c/td\u003e\n    \u003ctd\u003eAbsolute path to the module's directory\u003c/td\u003e\n    \u003ctd\u003e\n\u003cpre\u003e\nprocess.cwd()\n\u003c/pre\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e__dirname\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\n  \u003ctd\u003e\n    objectClone\n  \u003c/td\u003e\n  \u003ctd\u003e\n      Cloning function to apply to any objects or arrays that are global symbols, function return values or class method return values.\n  \u003c/td\u003e\n    \u003ctd\u003e\n\u003cpre\u003e\no =\u003e JSON.parse(\n  JSON.stringify(\n    o\n  )\n)\n\u003c/pre\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eobjectClone\u003c/code\u003e\n      \u003cbr /\u003e\n      (see \u003ca href='src/clone.ts'\u003eclone.ts\u003c/a\u003e)\n    \u003c/td\u003e\n\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n### 2.3. Return Object\n\nAll named exports, including globally defined variables, functions and classes are returned as keys within an object.\n\nAdditionally, the returned object contains the key `__jewireContext__` with the values being another object with the following properties:\n\n1. `rewire`: the return value of `rewire(modulePath)`. Please refer to the documentation for [rewire](https://github.com/jhnns/rewire) for further details.\n2. `hiddenExportInfo`: An object containing information about all hidden exports, of the form:\n\n    ```javascript\n    {\n      symbols: {\n        variables: string[],\n        functions: string[],\n        classes: string[],\n      }\n      ast: ASTProgram, // Abstract Syntax Tree from Meriyah parser\n      code: string, // return value of fs.readFileSync(filePath)\n    }\n    ```\n3. `jewireGetter`: the internal function used by jewire to retrieve objects. It has the same prototype as [`rewireModule.__get__`](https://github.com/jhnns/rewire?tab=readme-ov-file#rewiredmodule__get__name-string-), although objects are deep cloned using either jewire's default clone function or, if provided, `options.objectClone`.\n\n### 2.4. Errors\n\nIf an unknown file path is provided, the given file is empty or the module contains invalid code such as syntax errors, a default [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error) object is thrown.\n\n## 3. License\n\n\u003cdetails closed\u003e\n\u003csummary\u003e\n  Massachusetts Institute of Technology\n  (\u003ca href=\"https://opensource.org/license/mit\" target=\"_blank\"\u003eMIT\u003c/a\u003e)\n\u003c/summary\u003e\n\n\u003cbr/\u003e\n\n```\nCopyright (c) 2023 Khiet Tam Nguyen\n\nPermission is hereby granted, free of charge, to any person obtaining a\ncopy of this software and associated documentation files (the “Software”),\nto deal in the Software without restriction, including without limitation\nthe rights to use, copy, modify, merge, publish, distribute, sublicense,\nand/or sell copies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL\nTHE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\nDEALINGS IN THE SOFTWARE.\n```\n\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fnktnet1%2Fjewire.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fnktnet1%2Fjewire?ref=badge_large)\n\n\u003c/details\u003e\n\n## 4. Limitations\n\nAs this library uses [rewire](https://github.com/jhnns/rewire) under the hood,\nit has the same limitations in that it can only import CommonJS modules. Please\nsee the [limitations](https://github.com/jhnns/rewire#limitations) as described\nin the rewire module.\n\n## 5. Caveats\n\n### 5.1. Purpose\n\n**jewire** is a niche library designed to automark private functions in the\nsubmitted code of students using the Jest testing framework during the first\n2 weeks of their studies in\n[COMP1531 Software Engineering Fundamentals](https://webcms3.cse.unsw.edu.au/COMP1531/23T2/outline).\n\nThis is because `npm` and module imports/exports are not introduced until week\n3, when students are considered to be more familiar with JavaScript as a\nprogramming language.\n\n### 5.2. Rationale\n\n**jewire** aims to simplify the process of using\n[rewire](https://github.com/jhnns/rewire)\nby removing the need to provide a file extension and absolute path, abstracting\ngetter and setter methods and enabling relative module imports similar to CommonJS\n[require](https://nodejs.org/api/modules.html).\n\nThis process requires reading the module twice - once to parse into an [Abstract Syntax Tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) using the [Meriyah](https://github.com/meriyah/meriyah) parser, and another from rewire as rewire's interface does not enable reusing the file content.\n\n### 5.3. Rewire and Jest\n\n**Jewire** reclones objects at runtime to allow the return values of rewired\nfunctions and class methods to be compared using\n[Jest](https://jestjs.io)'s\nexpect[.toStrictEqual](https://jestjs.io/docs/expect#tostrictequalvalue) matcher,\nwhich in the rewire module would yield *\"Received: serializes to the same\nstring\"*.\n\nThe cause is Jest's utilisation of `node:vm` under the hood, which creates its own temporary context that overrides global classes such as `Array`, `Error` and `Date` to extend functionalities. These global classes differ from those that are returned from [rewire](https://github.com/jhnns/rewire)'s private functions, as depicted in the following GitHub issues made by [@geogezlei](https://github.com/georgezlei):\n- https://github.com/jhnns/rewire/issues/164\n- https://github.com/jestjs/jest/issues/8446\n\nFor further details and explanation about this problem, please visit Manuel Spigolon's [article](https://backend.cafe/should-you-use-jest-as-a-testing-library) about the use of the `instanceof` operator in Jest.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnktnet1%2Fjewire","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnktnet1%2Fjewire","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnktnet1%2Fjewire/lists"}