
Research
/Security News
Weaponizing Discord for Command and Control Across npm, PyPI, and RubyGems.org
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
@libj/tbench
Advanced tools
A "test bench" tools set for testing JavaScript / TypeScript code with comfort.
Based on sinon.js library.
All mocks are sinon stubs.
ModuleMock
Mocks any JavaScript module.
function ModuleMock(
module: string | Object,
sinon: SinonSandbox = sinon.createSandbox(),
)
module
: npm module name or custom modulesinon
: custom instance of SinonSandbox
import * as fs from 'fs'
import { ModuleMock } from '@libj/tbench'
const mock = ModuleMock('fs')
mock.existsSync.withArgs('/foo/bar.json').returns(true)
fs.existsSync('/foo/bar.json') // => true
// myFoo.js
export const myFoo = () => 'foo'
// myBar.js
import { myFoo } from './myFoo.js'
export const myBar = () => myFoo()
// myBar.test.js
import { ModuleMock } from '@libj/tbench'
import * as MyFooModule from './myFoo.js'
import { myBar } from './myBar.js'
describe('myBar()', () => {
it('calls myFoo()', () => {
const mock = ModuleMock(MyFooModule).myFoo
mock.returns('The Foooo')
expect(myBar()).toBe('The Foooo')
})
})
See in specs.
ClassMock
Mocks instances of classes.
function ClassMock(module: Object)
function ClassMock(module: Object, spec: Object)
function ClassMock(module: Object, spec: Object, sinon: SinonSandbox)
function ClassMock(module: Object, sinon: SinonSandbox)
module
: Custom module which holds class exportspec
: Specification object on how to mock the instance of classsinon
: custom instance of SinonSandbox
// MyFooClass.js
export class MyFooClass {}
// myBar.js
import { MyFooClass } from './MyFooClass'
export const myBar = arg => new MyFooClass(arg)
// myBar.test.js
import { ClassMock } from '@libj/tbench'
import * as MyFooClassModule from './MyFooClass'
import { myBar } from './myBar'
describe('myBar()', () => {
it('constructs class with arg', () => {
const mock = ClassMock(MyFooClassModule)
mock.$constructor
myBar('Foxy Lady')
expect(mock.$constructor.calledOnce).toBeTruthy()
expect(mock.$constructor.getCall(0).args).toEqual(['Foxy Lady'])
})
})
(!) CAUTION: This will not differentiate if class has been called with or without new
!
// MyFooClass.js
export class MyFooClass {
constructor() {
this.foo = 'Initial Foo'
this.bar = 'Initial Bar'
}
}
// myBar.js
import { MyFooClass } from './MyFooClass'
export const myBar = () => new MyFooClass()
// myBar.test.js
import { ClassMock } from '@libj/tbench'
import * as MyFooClassModule from './MyFooClass'
import { myBar } from './myBar'
describe('myBar()', () => {
it('proxies class instance properties', () => {
const mock = ClassMock(MyFooClassModule, {
foo: 'The Foo',
bar: null,
})
mock.foo
mock.bar.value('At the Bar')
const res = myBar()
expect(res.foo).toBe('The Foo')
expect(res.bar).toBe('At the Bar')
})
})
(i) Mind how mock values are initialized for foo
and bar
props
(i) Mock property has to be accessed even if initialized to trigger mocking
(i) It is always possible to overwrite initialized values at any time with any conditions (see sinon stubs)
(i) See respective specs for more examples
// MyFooClass.js
export class MyFooClass {
foo() {}
bar() {}
}
// myBar.js
import { MyFooClass } from './MyFooClass'
export const myBar = () => new MyFooClass()
// myBar.test.js
import { ClassMock } from '@libj/tbench'
import * as MyFooClassModule from './MyFooClass'
import { myBar } from './myBar'
describe('myBar()', () => {
it('proxies class instance methods', () => {
const mock = ClassMock(MyFooClassModule, {
'foo()': null,
'bar()': 'The Bar from Method',
})
mock.foo.returns('And The Foo')
mock.bar
const o = myBar()
expect(o.foo()).toBe('And The Foo')
expect(o.bar()).toBe('The Bar from Method')
})
})
(i) Method mocks are denoted using parenthesis
(i) Mind how mock values are initialized for foo()
and bar()
methods
(i) It is always possible to overwrite initialized values at any time with any constraints
(i) See respective specs for more examples
// test.js
import { ClassMock } from '@libj/tbench'
describe('FooTest', () => {
let mock
beforeEach(() => {
mock = ClassMock(FooClassModule)
})
afterEach(() => {
mock.$restore()
})
});
(i) Applies to both ModuleMock
/ ClassMock
This can be controlled via custom sinon instance
// test.js
import * as sinonLib from 'sinon'
import { ModuleMock, ClassMock } from '@libj/tbench'
describe('FooTest', () => {
let sinon, moduleMock, classMock
beforeEach(() => {
sinon = sinonLib.createSandbox()
moduleMock = ModuleMock('fs', sinon)
classMock = ClassMock(FooClassModule, sinon)
})
afterEach(() => {
sinon.restore()
})
});
In order for mocks to be working in raw nodejs code imports should be done using module as whole.
// foo.js
const foo = () => {}
module.exports = { foo }
// bar.js
const FooModule = require('./foo.js')
const bar = () => FooModule.foo()
module.exports = { bar }
(i) Notice how import is done in bar.js
(i) This is applied to both ModuleMock
and ClassMock
FAQs
Toolset for testing JavaScript code with ease
The npm package @libj/tbench receives a total of 375 weekly downloads. As such, @libj/tbench popularity was classified as not popular.
We found that @libj/tbench demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers 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.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
Security News
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
Research
The Socket Threat Research Team is tracking weekly intrusions into the npm registry that follow a repeatable adversarial playbook used by North Korean state-sponsored actors.