Socket
Socket
Sign inDemoInstall

truffle-assertions

Package Overview
Dependencies
322
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.4.0 to 0.5.0

47

index.js
const AssertionError = require('assertion-error');
/* Creates a new assertion message, containing the passedAssertionMessage and
* the defaultAssertion message when passedAssertionMessage exists, otherwise
* just the default.
*/
* the defaultAssertion message when passedAssertionMessage exists, otherwise
* just the default.
*/
createAssertionMessage = (passedMessage, defaultMessage) => {

@@ -12,3 +12,3 @@ let assertionMessage = defaultMessage;

}
return assertionMessage
return assertionMessage;
}

@@ -41,4 +41,4 @@

/* Returns a list of all emitted events in a transaction,
* using the format of getPrettyEventString
*/
* using the format of getPrettyEventString
*/
getPrettyEmittedEventsString = (result) => {

@@ -105,3 +105,3 @@ if (result.logs.length === 0) {

if (error !== null)
reject(error);
reject(error);
resolve({

@@ -112,6 +112,28 @@ tx: transactionHash,

});
})
});
});
}
fails = async (asyncFn, errorType, reason, message) => {
return asyncFn.then(() => {
const assertionMessage = createAssertionMessage(message, 'Did not fail');
throw new AssertionError(assertionMessage);
}).catch(error => {
if (
errorType !== undefined && errorType !== null && !error.message.includes(errorType) ||
reason !== undefined && reason !== null && !error.message.includes(reason)
) {
const assertionMessage = createAssertionMessage(message, `Expected to fail with ${errorType}, but failed with: ${error}`);
throw new AssertionError(assertionMessage);
}
});
}
ErrorType = {
REVERT: "revert",
INVALID_OPCODE: "invalid opcode",
OUT_OF_GAS: "out of gas",
INVALID_JUMP: "invalid JUMP"
}
module.exports = {

@@ -129,3 +151,10 @@ eventEmitted: (result, eventType, filter, message) => {

return createTransactionResult(contract, transactionHash);
}
},
fails: async (asyncFn, errorType, reason, message) => {
return fails(asyncFn, errorType, reason, message);
},
reverts: async (asyncFn, reason, message) => {
return fails(asyncFn, ErrorType.REVERT, reason, message);
},
ErrorType: ErrorType
}

4

package.json
{
"name": "truffle-assertions",
"version": "0.4.0",
"description": "Additional assertions and utilities for testing Ethereum smart contracts Truffle unit tests",
"version": "0.5.0",
"description": "Additional assertions and utilities for testing Ethereum smart contracts in Truffle unit tests",
"main": "index.js",

@@ -6,0 +6,0 @@ "scripts": {},

@@ -23,3 +23,3 @@ # truffle-assertions

### truffleAssert.eventEmitted(result, eventType, filter, message)
### truffleAssert.eventEmitted(result, eventType[, filter][, message])
The `eventEmitted` assertion checks that an event with type `eventType` has been emitted by the transaction with result `result`. A filter function can be passed along to further specify requirements for the event arguments:

@@ -54,3 +54,3 @@

### truffleAssert.eventNotEmitted(result, eventType, filter, message)
### truffleAssert.eventNotEmitted(result, eventType[, filter][, message])
The `eventNotEmitted` assertion checks that an event with type `eventType` has not been emitted by the transaction with result `result`. A filter function can be passed along to further specify requirements for the event arguments:

@@ -84,3 +84,2 @@

### truffleAssert.prettyPrintEmittedEvents(result)
Pretty prints the full list of events with their parameters, that were emitted in transaction with result `result`

@@ -99,3 +98,2 @@

### truffleAssert.createTransactionResult(contract, transactionHash)
There can be times where we only have access to a transaction hash, and not to a transaction result object, such as with the deployment of a new contract instance using `Contract.new();`. In these cases we still want to be able to assert that certain events are or aren't emitted.

@@ -111,1 +109,64 @@

```
### truffleAssert.fails(asyncFn[, errorType][, reason][, message])
Asserts that the passed async contract function fails with a certain ErrorType and reason.
The different error types are defined as follows:
```
ErrorType = {
REVERT: "revert",
INVALID_OPCODE: "invalid opcode",
OUT_OF_GAS: "out of gas",
INVALID_JUMP: "invalid JUMP"
}
```
```javascript
await truffleAssert.fails(
contractInstance.methodThatShouldFail(),
truffleAssert.ErrorType.OUT_OF_GAS
);
```
A reason can be passed to the assertion, which functions as an extra filter on the revert reason (note that this is only relevant in the case of revert, not for the other ErrorTypes). This functionality requires at least Truffle v0.5.
```javascript
await truffleAssert.fails(
contractInstance.methodThatShouldFail(),
truffleAssert.ErrorType.REVERT,
"only owner"
);
```
If the errorType parameter is omitted or set to null, the function just checks for failure, regardless of cause.
```javascript
await truffleAssert.fails(contractInstance.methodThatShouldFail());
```
Optionally, a custom message can be passed to the assertion, which will be displayed alongside the default one:
```javascript
await truffleAssert.fails(
contractInstance.methodThatShouldFail(),
truffleAssert.ErrorType.OUT_OF_GAS,
null,
'This method should run out of gas'
);
```
The default messages are
```javascript
'Did not fail'
`Expected to fail with ${errorType}, but failed with: ${error}`
```
### truffleAssert.reverts(asyncFn[, reason][, message])
This is an alias for `truffleAssert.fails(asyncFn, truffleAssert.ErrorType.REVERT[, reason][, message])`.
```javascript
await truffleAssert.reverts(
contractInstance.methodThatShouldRevert(),
"only owner"
);
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc