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

chore: convert ava tests to mocha #975

Merged
merged 9 commits into from
Jun 5, 2024
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
19 changes: 5 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"clean": "rm -rf lib/ dist/ coverage/ .nyc_output/ jsdoc/ test/e2e/.soroban",
"docs": "yarn build:docs && jsdoc -c ./config/.jsdoc.json --verbose",
"test": "yarn build:test && yarn test:node && yarn test:integration && yarn test:browser",
"test:e2e": "./test/e2e/initialize.sh && ava",
"test:e2e": "./test/e2e/initialize.sh && yarn _nyc mocha --recursive 'test/e2e/src/test-*.js'",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try running them in parallel? That's the feature that originally made Willem and I reach for AVA when we created near-workspaces, which is what we based these tests on, back when we first added them to soroban-cli. Seems like Mocha also has this now:

Suggested change
"test:e2e": "./test/e2e/initialize.sh && yarn _nyc mocha --recursive 'test/e2e/src/test-*.js'",
"test:e2e": "./test/e2e/initialize.sh && yarn _nyc mocha --recursive 'test/e2e/src/test-*.js'",
Suggested change
"test:e2e": "./test/e2e/initialize.sh && yarn _nyc mocha --recursive 'test/e2e/src/test-*.js'",
"test:e2e": "./test/e2e/initialize.sh && yarn _nyc mocha --recursive --parallel 'test/e2e/src/test-*.js'",

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if you tried this

Copy link
Contributor Author

@BlaineHeffron BlaineHeffron May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✖ ERROR: --parallel runs test files in a non-deterministic order, and is mutually exclusive with --sort

Must be the config that is loaded with the nyc module

"test:node": "yarn _nyc mocha --recursive 'test/unit/**/*.js'",
"test:integration": "yarn _nyc mocha --recursive 'test/integration/**/*.js'",
"test:browser": "karma start config/karma.conf.js",
Expand All @@ -75,14 +75,15 @@
"reporter": "spec",
"require": [
"@babel/register",
"test/test-nodejs.js"
"test/test-nodejs.js",
"dotenv/config"
],
"exclude": [
"test/test-browser.js"
],
"sort": true,
"recursive": true,
"timeout": 30000
"timeout": 120000
BlaineHeffron marked this conversation as resolved.
Show resolved Hide resolved
},
"nyc": {
"instrument": false,
Expand Down Expand Up @@ -111,8 +112,7 @@
"@types/randombytes": "^2.0.1",
"@types/sinon": "^17.0.2",
"@types/urijs": "^1.19.20",
"@typescript-eslint/parser": "^7.11.0",
"ava": "^5.3.1",
"@typescript-eslint/parser": "^7.7.1",
"axios-mock-adapter": "^1.22.0",
"babel-loader": "^9.1.3",
"babel-plugin-istanbul": "^6.1.1",
Expand Down Expand Up @@ -169,14 +169,5 @@
"randombytes": "^2.1.0",
"toml": "^3.0.0",
"urijs": "^1.19.1"
},
"ava": {
"files": [
"./test/e2e/src/test-*"
],
"require": [
"dotenv/config"
],
"timeout": "2m"
}
}
75 changes: 31 additions & 44 deletions test/e2e/src/test-contract-client-constructor.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
const test = require("ava");
const { expect } = require("chai");
const { spawnSync } = require("node:child_process");
const {
contracts,
networkPassphrase,
rpcUrl,
friendbotUrl,
generateFundedKeypair,
} = require("./util");
const { Address, contract, Keypair } = require("../../..");
const { Address, contract } = require("../../..");

async function generateFundedKeypair() {
const keypair = Keypair.random();
await fetch(`${friendbotUrl}/friendbot?addr=${keypair.publicKey()}`);
return keypair;
}

/**
* Generates a Client for the contract with the given name.
* Also generates a new account to use as as the keypair of this contract. This
* account is funded by friendbot. You can pass in an account to re-use the
* same account with multiple contract clients.
*
* By default, will re-deploy the contract every time. Pass in the same
* `contractId` again if you want to re-use the a contract instance.
*/
async function clientFromConstructor(
name,
{ keypair = generateFundedKeypair(), contractId } = {},
Expand Down Expand Up @@ -104,33 +89,35 @@ async function clientForFromTest(contractId, publicKey, keypair) {
return contract.Client.from(options);
}

test.before(async (t) => {
const { client, keypair, contractId } =
await clientFromConstructor("customTypes");
const publicKey = keypair.publicKey();
const addr = Address.fromString(publicKey);
t.context = { client, publicKey, addr, contractId, keypair }; // eslint-disable-line no-param-reassign
});
describe('Client', function() {

test("hello from constructor", async (t) => {
const { result } = await t.context.client.hello({ hello: "tests" });
t.is(result, "tests");
});
before(async function() {
const { client, keypair, contractId } =
await clientFromConstructor("customTypes");
const publicKey = keypair.publicKey();
const addr = Address.fromString(publicKey);
this.context = { client, publicKey, addr, contractId, keypair };
});

test("from", async (t) => {
// objects with different constructors will not pass deepEqual check
function constructorWorkaround(object) {
return JSON.parse(JSON.stringify(object));
}
it("can be constructed with `new Client`", async function() {
const { result } = await this.context.client.hello({ hello: "tests" });
expect(result).to.equal("tests");
});

const clientFromFrom = await clientForFromTest(
t.context.contractId,
t.context.publicKey,
t.context.keypair,
);
t.deepEqual(
constructorWorkaround(clientFromFrom),
constructorWorkaround(t.context.client),
);
t.deepEqual(t.context.client.spec.entries, clientFromFrom.spec.entries);
it("can be constructed with `from`", async function() {
// objects with different constructors will not pass deepEqual check
function constructorWorkaround(object) {
return JSON.parse(JSON.stringify(object));
}

const clientFromFrom = await clientForFromTest(
this.context.contractId,
this.context.publicKey,
this.context.keypair,
);
expect(constructorWorkaround(clientFromFrom)).to.deep.equal(
constructorWorkaround(this.context.client),
);
expect(this.context.client.spec.entries).to.deep.equal(clientFromFrom.spec.entries);
});
});
Loading
Loading