
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
@brika/testing
Advanced tools
Testing utilities for mocking Bun APIs in tests.
bun add -d @brika/testing
useBunMockThe simplest way to mock Bun APIs - lifecycle is handled automatically:
import { useBunMock } from '@brika/testing';
import { describe, test, expect } from 'bun:test';
describe('MyService', () => {
const bun = useBunMock(); // Auto beforeEach/afterEach
test('reads config file', async () => {
bun
.fs({
'/app/config.json': { port: 3000 },
'/app/locales/en/common.json': { greeting: 'Hello' },
})
.apply();
const config = await Bun.file('/app/config.json').json();
expect(config.port).toBe(3000);
});
});
mockBunFor more control over setup/teardown:
import { mockBun, type BunMock } from '@brika/testing';
import { afterEach, beforeEach, describe, test } from 'bun:test';
describe('MyService', () => {
let bun: BunMock;
beforeEach(() => {
bun = mockBun();
});
afterEach(() => {
bun.restore();
});
test('reads config file', async () => {
bun.fs({ '/app/config.json': { port: 3000 } }).apply();
// ...
});
});
The fs() method creates a virtual filesystem. Directory structure is automatically inferred from file paths:
// Just define files - directories are created automatically
bun.fs({
'/app/config.json': { port: 3000 },
'/app/locales/en/common.json': { greeting: 'Hello' },
'/app/locales/fr/common.json': { bonjour: 'Bonjour' },
}).apply();
// Glob scanning works automatically
const locales = await Array.fromAsync(
new Bun.Glob('*/').scan({ cwd: '/app/locales' })
);
// → ['en/', 'fr/']
For ordering or empty directories, use explicit directory syntax (keys ending with /):
bun.fs({
// Explicit order
'/locales/': ['fr/', 'en/', 'de/'],
// Empty directory
'/locales/de/': [],
// Files
'/locales/en/common.json': { hello: 'Hello' },
'/locales/fr/common.json': { bonjour: 'Bonjour' },
}).apply();
For granular control:
// Add single file
bun.file('/config.json', { port: 3000 });
// Add directory with entries
bun.directory('/locales', ['en/', 'fr/']);
// Configure spawn mock
bun.spawn({ exitCode: 0, stderr: 'Success' });
// Mock package resolution
bun.resolve('@test/plugin', '/node_modules/@test/plugin/index.js');
// Apply all mocks
bun.apply();
Mock Bun.spawn() with configurable exit codes and output:
bun
.spawn({ exitCode: 0, stderr: 'Installing packages...' })
.apply();
const proc = Bun.spawn(['bun', 'install']);
await proc.exited; // → 0
// Access recorded calls
expect(bun.spawnCalls).toHaveLength(1);
expect(bun.spawnCalls[0]?.cmd).toEqual(['bun', 'install']);
// Clear call history
bun.clearSpawnCalls();
Mock Bun.resolveSync():
bun
.resolve('@test/plugin', '/node_modules/@test/plugin/index.js')
.apply();
Bun.resolveSync('@test/plugin', '/'); // → '/node_modules/@test/plugin/index.js'
Bun.resolveSync('@unknown/pkg', '/'); // throws 'Cannot resolve'
Check and retrieve files from the virtual filesystem:
bun.file('/data.json', { items: [1, 2, 3] }).apply();
// After writes, verify file state
await Bun.write('/output.json', JSON.stringify({ result: true }));
bun.hasFile('/output.json'); // → true
bun.getFile('/output.json'); // → { result: true }
| Export | Description |
|---|---|
useBunMock() | Hook-style helper with auto lifecycle (recommended) |
mockBun() | Create a new BunMock instance (manual lifecycle) |
proxify(fn) | Create proxy that delegates to lazily-resolved instance |
| Method | Description |
|---|---|
fs(tree) | Define virtual filesystem from object tree |
file(path, content) | Add a single file |
directory(path, entries) | Add a directory with entries |
spawn(config) | Configure spawn mock (exitCode, stdout, stderr) |
resolve(pkg, path) | Mock package resolution |
apply() | Apply all mocks to Bun globals |
restore() | Restore original Bun APIs and clear state |
hasFile(path) | Check if file exists in virtual fs |
getFile(path) | Get file content from virtual fs |
clearSpawnCalls() | Clear recorded spawn calls |
| Property | Description |
|---|---|
spawnCalls | Array of recorded Bun.spawn() calls |
Bun.file() - File reading (exists(), json(), text())Bun.write() - File writing (updates virtual fs)Bun.spawn() - Process spawning with configurable outputBun.resolveSync() - Package resolutionBun.Glob - Directory scanning (scan(), scanSync(), match())proxifyCreate hook-style test helpers with lazy instance resolution:
import { proxify } from '@brika/testing';
import { beforeEach } from 'bun:test';
function useMyService(): MyService {
let current: MyService;
beforeEach(() => {
current = new MyService();
});
return proxify(() => current);
}
// Usage
describe('test', () => {
const service = useMyService();
test('works', () => {
service.doSomething(); // Delegates to current instance
});
});
src/
├── index.ts # Main exports
├── mock-bun.ts # BunMock implementation
└── proxify.ts # Proxy helper for hook-style utilities
FAQs
Unknown package
We found that @brika/testing demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.