https://github.com/drdxk/testonly
mark files as testonly by importing this package
https://github.com/drdxk/testonly
node nodejs testing typescript
Last synced: 6 months ago
JSON representation
mark files as testonly by importing this package
- Host: GitHub
- URL: https://github.com/drdxk/testonly
- Owner: drdxk
- Created: 2025-07-29T23:28:45.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-07-30T00:54:18.000Z (11 months ago)
- Last Synced: 2025-09-08T01:42:09.105Z (10 months ago)
- Topics: node, nodejs, testing, typescript
- Language: TypeScript
- Homepage:
- Size: 138 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# mark-testonly
Importing this file when `NODE_ENV !== 'test'` throws an error.
This allows marking `.js` / `.ts` files as "test only" by importing this
package. Import should be done as a side effect import.
Recommendation: make it the first import in your files.
## Installation
```shell
npm install mark-testonly --save
```
> _**Why not `--save-dev`?** Because this package might get lost in production,
> and instead of throwing it would produce a missing import error, which might
> be a bit more confusing._
## Usage
### Mark a file as test-only
Simply import as a side effect in a test file to mark that file as test-only:
```typescript
// my-test-util.ts
import 'mark-testonly'; // This throws in non-test environment
// .. rest of the file
```
```javascript
// my-legacy-test-util.cjs
require('mark-testonly'); // This throws in non-test environment
// .. rest of the file
```
It doesn't matter where exactly it is imported, however it is recommended to
import it as the first import in the file.
> _**Reminder**: import declarations are
> [hoisted](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#hoisting)._
### Mark a function as test-only
```typescript
// my-lib.ts
import {setTestOnly} from 'mark-testonly/fn';
export function setTestingHooks() {
setTestOnly(); // This throws in non-test environment
// ...function logic...
}
```