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 motoko examples to use jest #1833

Merged
merged 3 commits into from
Jun 17, 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
9 changes: 9 additions & 0 deletions examples/motoko_examples/calc/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'ts-jest'
}
};
6,092 changes: 4,783 additions & 1,309 deletions examples/motoko_examples/calc/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion examples/motoko_examples/calc/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.19.2",
"jest": "^29.7.0",
"ts-jest": "^29.1.4",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/motoko_examples/calc/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getCanisterId } from 'azle/dfx';
import { runTests } from 'azle/test';
import { runTests } from 'azle/test/jest';

// @ts-ignore
import { createActor } from './dfx_generated/calc';
Expand Down
91 changes: 33 additions & 58 deletions examples/motoko_examples/calc/test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,45 @@
import { ActorSubclass } from '@dfinity/agent';
import { Test } from 'azle/test';
import { expect, it, Test } from 'azle/test/jest';

// @ts-ignore
import { _SERVICE } from './dfx_generated/calc/calc.did';

export function getTests(calcCanister: ActorSubclass<_SERVICE>): Test[] {
return [
{
name: 'add 5',
test: async () => {
const result = await calcCanister.add(5n);
export function getTests(calcCanister: ActorSubclass<_SERVICE>): Test {
return () => {
it('adds 5', async () => {
const result = await calcCanister.add(5n);

return {
Ok: result === 5n
};
}
},
{
name: 'sub 2',
test: async () => {
const result = await calcCanister.sub(2n);
expect(result).toBe(5n);
});

return {
Ok: result === 3n
};
}
},
{
name: 'mul 6',
test: async () => {
const result = await calcCanister.mul(6n);
it('subtracts 2', async () => {
const result = await calcCanister.sub(2n);

return {
Ok: result === 18n
};
}
},
{
name: 'div 2',
test: async () => {
const result = await calcCanister.div(2n);
expect(result).toBe(3n);
});

return {
Ok: result.length === 1 && result[0] === 9n
};
}
},
{
name: 'clearall',
test: async () => {
const result = await calcCanister.clearall();
it('multiplies by 6', async () => {
const result = await calcCanister.mul(6n);

return {
Ok: result === undefined
};
}
},
{
name: 'add 0',
test: async () => {
const result = await calcCanister.add(0n);
expect(result).toBe(18n);
});

return {
Ok: result === 0n
};
}
}
];
it('divides by 2', async () => {
const result = await calcCanister.div(2n);

expect(result).toStrictEqual([9n]);
});

it('clears state', async () => {
const result = await calcCanister.clearall();

expect(result).toBeUndefined();
});

it('adds 0', async () => {
const result = await calcCanister.add(0n);

expect(result).toBe(0n);
});
};
}
9 changes: 9 additions & 0 deletions examples/motoko_examples/counter/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'ts-jest'
}
};
Loading
Loading