Skip to content

Commit

Permalink
Merge pull request #1870 from demergent-labs/jest_r
Browse files Browse the repository at this point in the history
jest examples r
  • Loading branch information
lastmjs authored Jul 9, 2024
2 parents 2359a25 + 84f44b1 commit 09f6200
Show file tree
Hide file tree
Showing 36 changed files with 39,528 additions and 9,775 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# Restore when https://github.com/demergent-labs/azle/issues/1799 is resolved
# "examples/run_time_errors",

# TODO http_counter tests are being skipped until this is resolved: https://forum.dfinity.org/t/lookuppathabsent/23461

name: Azle Tests
on:
push:
Expand Down
1 change: 1 addition & 0 deletions examples/motoko_examples/factorial/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function getTests(factorialCanister: ActorSubclass<_SERVICE>): Test {
});

// TODO: Fails with `Canister trapped explicitly: Panicked at 'TODO needs error info'`
// https://forum.dfinity.org/t/error-while-trying-to-run-idl-encode-on-values-around-1-8x10-308-or-larger/32924
it.skip('calculates 1000 factorial', async () => {
const result = await factorialCanister.fac(1000n);

Expand Down
10 changes: 10 additions & 0 deletions examples/randomness/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'ts-jest'
},
transformIgnorePatterns: ['/node_modules/(?!(azle)/)'] // Make sure azle is transformed
};
9,436 changes: 7,697 additions & 1,739 deletions examples/randomness/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion examples/randomness/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"scripts": {
"pretest": "ts-node --transpile-only --ignore=false test/pretest.ts",
"test": "ts-node --transpile-only --ignore=false test/test.ts"
"test": "jest"
},
"dependencies": {
"azle": "0.22.0"
},
"devDependencies": {
"@dfinity/agent": "0.11.1",
"jest": "^29.7.0",
"ts-jest": "^29.1.5",
"ts-node": "10.7.0",
"typescript": "^5.2.2"
}
Expand Down
133 changes: 59 additions & 74 deletions examples/randomness/test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,75 @@
import { ActorSubclass } from '@dfinity/agent';
import { Test } from 'azle/test';
import { expect, it, please, Test } from 'azle/test';
import { execSync } from 'child_process';

// @ts-ignore this path may not exist when these tests are imported into other test projects
import { _SERVICE } from './dfx_generated/randomness/randomness.did';

let globalResults: Set<string> = new Set();

export function getTests(randomnessCanister: ActorSubclass<_SERVICE>): Test[] {
return [
{
name: 'first round',
test: async () => {
const randomNumberCall_0Result =
await randomnessCanister.randomNumber();
const randomNumberCall_1Result =
await randomnessCanister.randomNumber();
const randomNumberCall_2Result =
await randomnessCanister.randomNumber();
const randomNumberCall_3Result =
await randomnessCanister.randomNumber();
const randomNumberCall_4Result =
await randomnessCanister.randomNumber();
export function getTests(randomnessCanister: ActorSubclass<_SERVICE>): Test {
return () => {
it('first round', async () => {
const randomNumberCall_0Result =
await randomnessCanister.randomNumber();
const randomNumberCall_1Result =
await randomnessCanister.randomNumber();
const randomNumberCall_2Result =
await randomnessCanister.randomNumber();
const randomNumberCall_3Result =
await randomnessCanister.randomNumber();
const randomNumberCall_4Result =
await randomnessCanister.randomNumber();

const results = [
randomNumberCall_0Result.toString(),
randomNumberCall_1Result.toString(),
randomNumberCall_2Result.toString(),
randomNumberCall_3Result.toString(),
randomNumberCall_4Result.toString()
];
const results = [
randomNumberCall_0Result.toString(),
randomNumberCall_1Result.toString(),
randomNumberCall_2Result.toString(),
randomNumberCall_3Result.toString(),
randomNumberCall_4Result.toString()
];

for (const result of results) {
globalResults.add(result);
}

return {
Ok: globalResults.size === 5
};
}
},
{
name: 'dfx deploy',
prep: async () => {
execSync('dfx deploy --upgrade-unchanged');
for (const result of results) {
globalResults.add(result);
}
},
{
name: 'getRedeployed',
test: async () => {
const result = await randomnessCanister.getRedeployed();
return {
Ok: result === true
};
}
},
{
name: 'second round',
test: async () => {
const randomNumberCall_0Result =
await randomnessCanister.randomNumber();
const randomNumberCall_1Result =
await randomnessCanister.randomNumber();
const randomNumberCall_2Result =
await randomnessCanister.randomNumber();
const randomNumberCall_3Result =
await randomnessCanister.randomNumber();
const randomNumberCall_4Result =
await randomnessCanister.randomNumber();

const results = [
randomNumberCall_0Result.toString(),
randomNumberCall_1Result.toString(),
randomNumberCall_2Result.toString(),
randomNumberCall_3Result.toString(),
randomNumberCall_4Result.toString()
];
expect(globalResults.size).toBe(5);
}, 20_000);

please('dfx deploy', async () => {
execSync('dfx deploy --upgrade-unchanged');
});

for (const result of results) {
globalResults.add(result);
}
it('getRedeployed', async () => {
const result = await randomnessCanister.getRedeployed();
expect(result).toBe(true);
});

return {
Ok: globalResults.size === 10
};
it('second round', async () => {
const randomNumberCall_0Result =
await randomnessCanister.randomNumber();
const randomNumberCall_1Result =
await randomnessCanister.randomNumber();
const randomNumberCall_2Result =
await randomnessCanister.randomNumber();
const randomNumberCall_3Result =
await randomnessCanister.randomNumber();
const randomNumberCall_4Result =
await randomnessCanister.randomNumber();

const results = [
randomNumberCall_0Result.toString(),
randomNumberCall_1Result.toString(),
randomNumberCall_2Result.toString(),
randomNumberCall_3Result.toString(),
randomNumberCall_4Result.toString()
];

for (const result of results) {
globalResults.add(result);
}
}
];

expect(globalResults.size).toBe(10);
}, 20_000);
};
}
10 changes: 10 additions & 0 deletions examples/recursion/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'ts-jest'
},
transformIgnorePatterns: ['/node_modules/(?!(azle)/)'] // Make sure azle is transformed
};
Loading

0 comments on commit 09f6200

Please sign in to comment.