Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Replace arrow function with normal function in unit test #804

Merged
merged 7 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Features, Bug Fixes, API Breaking, Deprecated, Infrastructure, Template Updates
- Fix `txn AssetSender` should return zero address by default.
- Add unit tests for all transaction types in runtime executeTx.
- Add support loadLogic for *.teal program and SCParam.
- Replace arrow functions with normal functions in all unit test which is recommended by `mocha`

#### Examples

Expand Down
1 change: 1 addition & 0 deletions docs/guide/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ layout: splash

- Use [boilerplate-stateful-smart-contract](https://developer.algorand.org/docs/features/asc1/stateful/#boilerplate-stateful-smart-contract) as a template for new smart-contracts.
- Use [zero address](https://developer.algorand.org/docs/features/accounts/#special-accounts) to prevent future updates or deletion of a smart-contract.
- It is highly recommended not to use arrow functions in tests due to the problem of scope of `this` keyword in javascript. Use only regular function expressions. For more details go to [mocha](ttps://mochajs.org/#arrow-functions).

### Entry points

Expand Down
8 changes: 4 additions & 4 deletions docs/guide/testing-teal.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ describe("use-case", function() {
let variable1;
// ...

this.beforeAll(() => { ... });
this.afterAll(() => { ... });
this.beforeAll(function() { ... });
this.afterAll(function() { ... });

it("test case 1", () => {
it("test case 1", function() {
// preparation
// execution
// checks
});

it("test case 2", () => { ... });
it("test case 2", function() { ... });
});
```

Expand Down
8 changes: 4 additions & 4 deletions docs/tutorials/t-04.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ describe("use-case", function() {
let variable1;
// ...

this.beforeAll(() => { ... });
this.afterAll(() => { ... });
this.beforeAll(function() { ... });
this.afterAll(function() { ... });

it("test case 1", () => {
it("test case 1", function() {
// preparation
// execution
// checks
});

it("test case 2", () => { ... });
it("test case 2", function() { ... });
});
```

Expand Down
4 changes: 2 additions & 2 deletions examples/bond/test/bond-token-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("Bond token Tests", function () {
let issuerLsigAddress;
let lsig;

this.beforeAll(() => {
this.beforeAll(function () {
appStorageConfig = {
localInts: 1,
localBytes: 1,
Expand All @@ -61,7 +61,7 @@ describe("Bond token Tests", function () {
const maxIssuance = "int:1000000";
const bondCreator = convert.addressToPk(bondTokenCreator.address);

it("Bond token application", () => {
it("Bond token application", function () {
/**
* Issue initial bond tokens to the issuer
* In epoch_0 elon buys 10 bonds
Expand Down
24 changes: 12 additions & 12 deletions examples/bond/test/failing-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe("Bond token failing tests", function () {
try {
runtime.optInToApp(elon.address, applicationId, {}, {});
// eslint-disable-next-line no-empty
} catch (e) {} // can be already opted-in
} catch (e) { } // can be already opted-in
const amount = 10;
const algoAmount = amount * 1000;

Expand All @@ -170,7 +170,7 @@ describe("Bond token failing tests", function () {
syncAccounts();
}

it("Random user should not be able to update issuer's address", () => {
it("Random user should not be able to update issuer's address", function () {
// update application with correct issuer account address
const appArgs = [updateIssuer, convert.addressToPk(issuerLsigAddress)]; // converts algorand address to Uint8Array

Expand All @@ -186,7 +186,7 @@ describe("Bond token failing tests", function () {
assert.throws(() => runtime.executeTx([appCallParams]), RUNTIME_ERR1009);
});

it("Issuer should not be able to send asa without calling bond-dapp", () => {
it("Issuer should not be able to send asa without calling bond-dapp", function () {
const params = {
type: types.TransactionType.TransferAsset,
sign: types.SignType.LogicSignature,
Expand All @@ -201,7 +201,7 @@ describe("Bond token failing tests", function () {
assert.throws(() => runtime.executeTx([params]), REJECTED_BY_LOGIC);
});

it("Opt-In to issuer lsig with single transaction should fail", () => {
it("Opt-In to issuer lsig with single transaction should fail", function () {
const optInTx = {
type: types.TransactionType.OptInASA,
sign: types.SignType.LogicSignature,
Expand All @@ -215,7 +215,7 @@ describe("Bond token failing tests", function () {
});

// Avoid spamming of asset id's in bond-dapp
it("Opt-In to issuer lsig without store manager signature should fail", () => {
it("Opt-In to issuer lsig without store manager signature should fail", function () {
const optInTx = [
{
type: types.TransactionType.TransferAlgo,
Expand All @@ -238,7 +238,7 @@ describe("Bond token failing tests", function () {
assert.throws(() => runtime.executeTx(optInTx), REJECTED_BY_LOGIC);
});

it("Random user should not be able to update issue price", () => {
it("Random user should not be able to update issue price", function () {
const appArgs = ["str:update_issue_price", "int:0"];

const appCallParams = {
Expand All @@ -253,7 +253,7 @@ describe("Bond token failing tests", function () {
assert.throws(() => runtime.executeTx([appCallParams]), RUNTIME_ERR1009);
});

it("should not issue shares to address other than issuer's address", () => {
it("should not issue shares to address other than issuer's address", function () {
const appArgs = [updateIssuer, convert.addressToPk(issuerLsigAddress)];

const appCallParams = {
Expand All @@ -274,7 +274,7 @@ describe("Bond token failing tests", function () {
assert.throws(() => runtime.executeTx(groupTx), RUNTIME_ERR1009);
});

it("User should not be able to buy for less amount than specified", () => {
it("User should not be able to buy for less amount than specified", function () {
issue();

// Buy tokens from issuer
Expand All @@ -287,7 +287,7 @@ describe("Bond token failing tests", function () {
assert.throws(() => runtime.executeTx(groupTx), RUNTIME_ERR1009);
});

it("Only store manager can create dex", () => {
it("Only store manager can create dex", function () {
issue();
buy();

Expand All @@ -297,7 +297,7 @@ describe("Bond token failing tests", function () {
);
});

it("Buyer cannot redeem more than they have", () => {
it("Buyer cannot redeem more than they have", function () {
issue();
buy();
// manager starts epoch 1 (create dex)
Expand All @@ -309,7 +309,7 @@ describe("Bond token failing tests", function () {
assert.throws(() => redeem(runtime, elon, 1, 20, dexLsig1), RUNTIME_ERR1402);
});

it("Buyer tries to buy bonds without paying fees", () => {
it("Buyer tries to buy bonds without paying fees", function () {
issue();
// Buy tokens from issuer
runtime.optInToASA(initialBond, elon.address, {});
Expand All @@ -332,7 +332,7 @@ describe("Bond token failing tests", function () {
assert.throws(() => runtime.executeTx(groupTx), RUNTIME_ERR1506);
});

it("Buyer tries to exchange bonds without paying fees", () => {
it("Buyer tries to exchange bonds without paying fees", function () {
issue();
buy();
// manager starts epoch 1 (create dex)
Expand Down
4 changes: 2 additions & 2 deletions examples/crowdfunding/test/crowdfundingTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("Crowdfunding Tests", function () {
convert.uint64ToBigEndian(fundCloseDate.getTime()),
];

it("crowdfunding application", () => {
it("crowdfunding application", function () {
/**
* This test demonstrates how to create a Crowdfunding Stateful Smart Contract Application
* and interact with it. there are following operations that are performed:
Expand Down Expand Up @@ -309,7 +309,7 @@ describe("Crowdfunding Tests", function () {
}
});

it("should be rejected by logic when claiming funds if goal is not met", () => {
it("should be rejected by logic when claiming funds if goal is not met", function () {
const applicationId = runtime.deployApp(
creator.account,
{ ...appDefinition, appArgs: creationArgs },
Expand Down
84 changes: 42 additions & 42 deletions examples/crowdfunding/test/failing-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
const approvalProgram = getProgram(approvalProgramFilename);
const clearProgram = getProgram(clearProgramFilename);
// Create new runtime and application before each test.
this.beforeEach(() => {
this.beforeEach(function () {
runtime = new Runtime([master, creator, donor]);

// Get begin date to pass in
Expand Down Expand Up @@ -133,7 +133,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
runtime = new Runtime([master, creator, escrow, donor]);
});

it("should fail donation if donor has insufficient balance", () => {
it("should fail donation if donor has insufficient balance", function () {
updateAndOptIn();
appArgs = [convert.stringToBytes("donate")];
const donationAmount = initialDonorBalance + 1000;
Expand All @@ -160,23 +160,23 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
assert.throws(() => runtime.executeTx(txGroup), "RUNTIME_ERR1401");
});

it("should fail donation if timestamp is after endDate", () => {
it("should fail donation if timestamp is after endDate", function () {
updateAndOptIn();
// set timestamp to after of endDate
runtime.setRoundAndTimestamp(5, endDate.getSeconds() + 100);

assert.throws(() => runtime.executeTx(donateTxGroup), rejectMsg);
});

it("should fail donation if timestamp is before of beginDate", () => {
it("should fail donation if timestamp is before of beginDate", function () {
updateAndOptIn();
// set timestamp to out of endDate
runtime.setRoundAndTimestamp(5, beginDate.getSeconds() - 100);

assert.throws(() => runtime.executeTx(donateTxGroup), rejectMsg);
});

it("should fail if goal is met, and donor tries to reclaim funds", () => {
it("should fail if goal is met, and donor tries to reclaim funds", function () {
updateAndOptIn();
// set donation to greater than goal
donateTxGroup[1].amountMicroAlgos = goal + 1000;
Expand Down Expand Up @@ -209,7 +209,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
assert.throws(() => runtime.executeTx(txGroup), rejectMsg);
});

it("should fail if goal is not met, but donor tries to reclaim funds before fund close date", () => {
it("should fail if goal is not met, but donor tries to reclaim funds before fund close date", function () {
updateAndOptIn();
runtime.executeTx(donateTxGroup);

Expand Down Expand Up @@ -239,7 +239,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
assert.throws(() => runtime.executeTx(txGroup), rejectMsg);
});

it("should fail if creator tries to claim funds before fund end date", () => {
it("should fail if creator tries to claim funds before fund end date", function () {
updateAndOptIn();
// set donation to greater than goal
donateTxGroup[1].amountMicroAlgos = goal + 1000;
Expand Down Expand Up @@ -268,7 +268,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
assert.throws(() => runtime.executeTx(txGroup), rejectMsg);
});

it("should fail if a transaction is missing in group transaction while donating", () => {
it("should fail if a transaction is missing in group transaction while donating", function () {
updateAndOptIn();
appArgs = [convert.stringToBytes("donate")];
const txGroup = [
Expand All @@ -285,7 +285,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
assert.throws(() => runtime.executeTx(txGroup), "RUNTIME_ERR1008: Index out of bound");
});

it("should fail if transaction is signed by wrong lsig", () => {
it("should fail if transaction is signed by wrong lsig", function () {
updateAndOptIn();
// set donation to greater than goal
donateTxGroup[1].amountMicroAlgos = goal + 1000;
Expand Down Expand Up @@ -319,7 +319,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
);
});

it("should fail if escrow address is not updated in app", () => {
it("should fail if escrow address is not updated in app", function () {
// opt-in to app
runtime.optInToApp(creator.address, applicationId, {}, {});
runtime.optInToApp(donor.address, applicationId, {}, {});
Expand All @@ -332,7 +332,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
);
});

it("should fail transaction if logic signature is not passed", () => {
it("should fail transaction if logic signature is not passed", function () {
updateAndOptIn();
// set donation to greater than goal
donateTxGroup[1].amountMicroAlgos = goal + 1000;
Expand Down Expand Up @@ -364,7 +364,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
);
});

it("should fail to delete app because escrow account balance is not empty", () => {
it("should fail to delete app because escrow account balance is not empty", function () {
updateAndOptIn();
runtime.setRoundAndTimestamp(5, fundCloseDate.getTime() + 12);
const deleteTx = {
Expand All @@ -380,7 +380,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
assert.throws(() => runtime.executeTx([deleteTx]), "RUNTIME_ERR1008: Index out of bound");
});

it("should fail on trying to update application where sender is not creator", () => {
it("should fail on trying to update application where sender is not creator", function () {
appArgs = [convert.addressToPk(escrowAddress)]; // converts algorand address to Uint8Array

assert.throws(
Expand All @@ -401,7 +401,7 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
);
});

it("should fail if closing the funds in escrow but closeRemainderTo is not fundReceiver", () => {
it("should fail if closing the funds in escrow but closeRemainderTo is not fundReceiver", function () {
updateAndOptIn();
// set donation to greater than goal
donateTxGroup[1].amountMicroAlgos = goal + 1000;
Expand Down Expand Up @@ -434,33 +434,33 @@ describe("Crowdfunding Test - Failing Scenarios", function () {
// uncomment when
// https://github.com/algorand/js-algorand-sdk/commit/b18e3beab8004d7e53a5370334b8e9f5c7699146#diff-75520b02c557ab3f0b89e5f03029db31af2f0dc79e5215d3e221ed9ea59fe441
// commit is released
/* it('should fail if ReKeyTo is not ZERO_ADDRESS in transaction', () => {
updateAndOptIn();
// set donation to greater than goal
donateTxGroup[1].amountMicroAlgos = goal + 1000;
runtime.executeTx(donateTxGroup);
runtime.setRoundAndTimestamp(5, endDate.getTime() + 122);
appArgs = [convert.stringToBytes('claim')];
const txGroup = [
{
type: types.TransactionType.CallApp,
sign: types.SignType.SecretKey,
fromAccount: creator.account,
appID: applicationId,
payFlags: { totalFee: 1000, ReKeyTo: donor.address },
appArgs: appArgs
},
{
type: types.TransactionType.TransferAlgo,
sign: types.SignType.LogicSignature,
fromAccountAddr: escrow.account.addr,
toAccountAddr: creator.address,
amountMicroAlgos: 0,
lsig: lsig,
payFlags: { totalFee: 1000, closeRemainderTo: creator.address, ReKeyTo: donor.address }
}
];

assert.throws(() => runtime.executeTx(txGroup), rejectMsg);
/* it('should fail if ReKeyTo is not ZERO_ADDRESS in transaction', function() {
updateAndOptIn();
// set donation to greater than goal
donateTxGroup[1].amountMicroAlgos = goal + 1000;
runtime.executeTx(donateTxGroup);
runtime.setRoundAndTimestamp(5, endDate.getTime() + 122);
appArgs = [convert.stringToBytes('claim')];
const txGroup = [
{
type: types.TransactionType.CallApp,
sign: types.SignType.SecretKey,
fromAccount: creator.account,
appID: applicationId,
payFlags: { totalFee: 1000, ReKeyTo: donor.address },
appArgs: appArgs
},
{
type: types.TransactionType.TransferAlgo,
sign: types.SignType.LogicSignature,
fromAccountAddr: escrow.account.addr,
toAccountAddr: creator.address,
amountMicroAlgos: 0,
lsig: lsig,
payFlags: { totalFee: 1000, closeRemainderTo: creator.address, ReKeyTo: donor.address }
}
];

assert.throws(() => runtime.executeTx(txGroup), rejectMsg);
}); */
});
Loading