Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
assert-element
Advanced tools
Assertions that can be used when working with Deku/React and JSX.
Checks the given node
to make sure it looks like a virtual node. If the type
is specified, it must match strictly.
assert.isNode(<div />);
assert.isNode(<b>Hello World</b>, 'b');
assert.isNode(<Button>Log In</Button>, Button);
Checks the given node
to make sure it has the given attr
attribute. If the
value
is specified, it must match that value strictly.
assert.hasAttribute(<a href="http://example.com/">Home</a>, 'href');
assert.hasAttribute(<button type="submit">Submit</button>, 'type', 'submit');
NOTE: this allows for falsy values, as an attribute can be present but intentionally
false, such as checked={false}
.
Checks the given node
to make sure it does not have the given attr
attribute.
assert.notHasAttribute(<div />, 'id');
NOTE: this will not throw for falsy values, as an attribute can be present but
intentionally false, such as checked={false}
.
Checks that the given node
has the given CSS class name
. This is largely a helper
for HTML elements, although any component that uses class
in the same fashion can be
checked.
assert.hasClass(<div class="a b c" />, 'b');
Checks that the given node
does not have the given CSS class name
. This is largely
a helper for HTML elements, although any component that uses class
in the same fashion
can be checked.
assert.notHasClass(<div class="a" />, 'b');
Checks that the given node
has child nodes matching the children
argument:
Number
, it will ensure node
has that many child nodesFunction
, it will run the function against each child node (which should
throw if they are invalid)Array
, it will check for loose/deep equalitynode
has at least 1 childvar node = (
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
);
// make sure there are any children
assert.hasChildren(node);
// make sure there are 3 children
assert.hasChildren(node, 3);
// our fn just runs other assertions
assert.hasChildren(node, function (child) {
assert.isNode(child, 'li');
assert.hasChildren(child);
});
Checks that the given node
does not have any child nodes.
assert.notHasChildren(<div />);
Check if the given node
at a given zero-indexed index
has the corresponding
child
, using the following criteria
:
Function
, it will run criteria
, passing the child node as an
argument. criteria
is expected to throw an error if the node is invalid.var node = (
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
);
// make sure a child at index 0 exists
assert.hasChild(node, 0);
// do a deep comparison on the child at index 0
assert.hasChild(node, 0, 'div');
// run other assertions on the child node
assert.hasChild(node, 0, function (child) {
assert.isNode(child, 'li);
});
When unit-testing deku components, you'll typically run the render()
function and
make assertions against the virtual element it returns.
let Button = {
render({ props }) {
return <button type={props.type}>{props.children}</button>
}
};
var component = {
props: {
type: 'submit',
children: 'Hello World'
}
};
assert.isNode(Button.render(component), 'button');
assert.hasAttribute(Button.render(component), 'type', 'submit');
assert.hasChildren(Button.render(component), [ 'Hello World' ]);
This is a trivial example of course, but you can easily introduce variables and
other dynamic code in order to test that your components properly understand the
various props
and state
that they will receive.
FAQs
Assertions for checking virtual nodes used by Deku/React/etc
The npm package assert-element receives a total of 6 weekly downloads. As such, assert-element popularity was classified as not popular.
We found that assert-element 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.