
Security News
Scaling Socket from Zero to 10,000+ Organizations
Socket CEO Feross Aboukhadijeh shares lessons from scaling a developer security startup to 10,000+ organizations in this founder interview.
@nightwatch/testdoubles
Advanced tools
This plugin extends Nightwatch.js by exposing Sinon.js test double methods. The plugin allows users to create and work with mocks, spies, and stubs in Nightwatch.js tests.
npm i @nightwatch/testdoubles --save-dev
nightwatch.json (or nightwatch.conf.js) file and add the following:{
"plugins": [
"@nightwatch/testdoubles"
]
}
We also need to turn off the browser session, since we're only doing unit testing. This can be accomplished by setting these properties:
{
"start_session": false,
"webdriver": {
"start_process": false
}
}
Once @nightwatch/testdoubles is installed and added to your configuration file, you can use the sinon object in your test cases to create test doubles. Here are some examples:
A spy is a function that records some metadata about its calls, such as the number of times it was called, the arguments it was called with, etc. Spies are useful for verifying that a function was called, or for inspecting the arguments it was called with.
describe('use spies in nightwatch', function() {
it('should log message when called', function({sinon}) {
const obj = {
hello: () => console.log('Hello!')
}
const sayHello = () => obj.hello();
const spy = sinon.spy(obj, 'hello'); //create a spy on hello
sayHello();
assert(spy.calledOnce); //assert that the spy was called once
spy.restore(); //restore original hello function
})
})
This example creates a spy on the hello method of an object, and then calls the sayHello function. The assertion checks whether the spy was called once. Finally, the spy is restored to its original state.
A stub is a function that replaces the original function with a "dummy" implementation. This is useful when you need to control the behavior of a function during a test, for example to simulate an error condition.
describe('use stubs in nightwatch', function() {
it('stub hello', function({sinon}) {
const obj = {
hello: () => console.log('Hello!')
}
const sayHello = () => obj.hello();
const stub = sinon.stub(obj, 'hello').returns('hi'); // replace hello with a dummy implementation that returns 'hi'
const result = sayHello();
assert.strictEqual(result, 'hi'); // check that the stubbed function returned 'hi'
})
})
This example creates a stub on the console.log method and then calls it with the argument 'Hello!'. The assertion checks whether the stub was called once with the expected argument. Finally, the stub is restored to its original state.
A mock is a function that "mocks" an object, i.e. it creates a fake version of the object with the same interface as the real object. You can set expectations on the mock object, i.e. specify which methods should be called and with what arguments, and the mock will verify that these expectations are met during the test.
Note: creating a mock automatically attaches a Nightwatch assertion to it. mock.verify() runs the checks and reports errors if the checks fail.
describe('use mocks in nightwatch', function() {
it('mock hello obj', function({sinon}) {
const obj = {
hello: () => console.log('Hello!')
}
const sayHello = () => obj.hello();
const mock = sinon.mock(obj).expects('hello').atLeast(1).returns(null); //set a mock on hello
sayHello();
mock.verify(); // mocks comes with inbuilt assertion
})
})
This example creates a mock on the hello method of an object, and then calls the sayHello function. The mock.verify() method checks whether the mock was called at least once. Finally, the mock is restored to its original state.
For more information on how to use spy, stub, and mock, see the Sinon.js documentation.
We hope these examples help you get started with using @nightwatch/testdoubles in your Nightwatch tests!
MIT
FAQs
nightwatch plugin bring sinon methods to nightwatch
The npm package @nightwatch/testdoubles receives a total of 953 weekly downloads. As such, @nightwatch/testdoubles popularity was classified as not popular.
We found that @nightwatch/testdoubles demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 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.

Security News
Socket CEO Feross Aboukhadijeh shares lessons from scaling a developer security startup to 10,000+ organizations in this founder interview.

Research
Socket Threat Research maps a rare inside look at OtterCookie’s npm-Vercel-GitHub chain, adding 197 malicious packages and evidence of North Korean operators.

Research
Socket researchers identified a malicious Chrome extension that manipulates Raydium swaps to inject an undisclosed SOL transfer, quietly routing fees to an attacker wallet.