
Product
Introducing GitHub Actions Scanning Support
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.
United js test tools
Mocha is great but having asserts, assert plugins and spies spread out as separate projects is a bit frustrating, especially when you have to jump around all of the different documentations or setup test environment.
Mochaccino goals:
npm install --save-dev mochaccino mocha babel-core babel-register babel-preset-es2015
add the following to your package.json :
{
...
"scripts": {
"test": "mocha --compilers js:babel-register"
}
"babel": {
"presets": ["es2015"]
}
...
}
create a directory test with a file index.js:
import {expect, spy, dom} from 'mochaccino';
describe('mochaccino', () => {
it('do basic expectations', () => {
expect(true).toBeTruthy();
});
it('do spy expectations', () => {
let s = spy();
s(1, 2);
expect(s).toHaveBeenCalledWith(1, 2);
});
it('do DOM testing', () => {
dom.create();
document.body.appendChild(document.createElement('p'));
expect(document.getElementsByTagName('p').length).toEqual(1);
dom.destroy();
});
});
and then just run in the command line:
npm test
for spies:
each expectation can be combined with not flag:
expect(a).not.toBe(b)
import {spy} from 'mochaccino';
spy(obj, 'funcName'); - spy on objects
spy(obj, 'funcName');
obj.funcName();
expect(obj.funcName).toHaveBeenCalled();
spy(); - create standalone spy
let s = spy();
s();
expect(s).toHaveBeenCalled();
spy(obj, 'funcName').and.callFake(func); - spy on object and call given function instead original one
spy(obj, 'funcName').and.callFake(() => {
return 123;
});
expect(obj.funcName()).toEqual(123);
expect(obj.funcName).toHaveBeenCalled();
spy(obj, 'funcName').restore(); - restore original behaviour by removing the spy
spy(obj, 'funcName');
obj.funcName.restore();
spy(obj, 'funcName').and.callThrough(); - spy on object but call original method
spy(obj, 'funcName').and.returnValue(5); - spy will return a value when called
Mochaccino dom helper lets you run a dom implementation (jsdom) inside the node enviroment. No browser required.
import {expect, dom} from 'mochaccino';
describe('dom testing', () => {
beforeEach(() => {
dom.create();
});
afterEach(() => {
dom.destroy();
});
it('should append a child to the body', () => {
let par = document.createElement('P');
let text = document.createTextNode('some text');
par.appendChild(text);
document.body.appendChild(par);
let parCount = document.getElementsByTagName('P');
expect(document.body.innerHTML).toBeDefined();
expect(parCount.length).toEqual(1);
});
it('should not find the previously appended child', () => {
let parCount = document.getElementsByTagName('P');
expect(document.body.innerHTML).toEqual('');
expect(parCount.length).toEqual(0);
});
});
Unfortunately approach above is not recommended for testing React
component as React's source code makes several assumptions about
the environment it is running in, and one of them is that the
document that is found at "require time" is going to be
the one and only document it ever needs to worry about.
As a result, this type of "reloading" ends up causing more pain
than it prevents.
In that case document needs to be created only once before
requiring React. This can be done by:
mocha --require mochaccino/dom-setup --compilers js:babel-register
and then "clearing" existing document between the tests to make sure that components do not leak:
import {expect, dom} from 'mochaccino';
describe('dom testing', () => {
beforeEach(() => {
dom.clear();
});
afterEach(() => {
dom.clear();
});
it('should append a child to the body', () => {
let par = document.createElement('P');
let text = document.createTextNode('some text');
par.appendChild(text);
document.body.appendChild(par);
let parCount = document.getElementsByTagName('P');
expect(document.body.innerHTML).toBeDefined();
expect(parCount.length).toEqual(1);
});
it('should not find the previously appended child', () => {
let parCount = document.getElementsByTagName('P');
expect(document.body.innerHTML).toEqual('');
expect(parCount.length).toEqual(0);
});
});
FAQs
United js test tools
We found that mochaccino demonstrated a not healthy version release cadence and project activity because the last version was released 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.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.

Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.

Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.