Skip to content

Commit

Permalink
update hybrid canister to use jest
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Jun 17, 2024
1 parent 3b0e1d7 commit 12d5a13
Show file tree
Hide file tree
Showing 9 changed files with 8,484 additions and 2,455 deletions.
10 changes: 10 additions & 0 deletions examples/hybrid_canister/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
};
10,497 changes: 8,311 additions & 2,186 deletions examples/hybrid_canister/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion examples/hybrid_canister/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"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",
"express": "^4.18.2"
},
"devDependencies": {
"@types/express": "^4.17.21",
"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/hybrid_canister/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runTests } from 'azle/test';
import { runTests } from 'azle/test/jest';

import { getTests } from './tests';

Expand Down
55 changes: 18 additions & 37 deletions examples/hybrid_canister/test/tests/canister.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getCanisterId } from 'azle/dfx';
import { Test } from 'azle/test';
import { expect, it, Test } from 'azle/test/jest';

import { createActor } from '../dfx_generated/canister';

export function getTests(): Test[] {
export function getTests(): Test {
const canisterId = getCanisterId('canister');
const origin = `http://${canisterId}.localhost:8000`;
const actor = createActor(canisterId, {
Expand All @@ -12,42 +12,23 @@ export function getTests(): Test[] {
}
});

return [
{
name: 'canister',
test: async () => {
try {
const httpQueryResponse = await fetch(
`${origin}/http-query`
);
const httpQueryResponseText =
await httpQueryResponse.text();
return () => {
it('handles a canister with additional http functionality', async () => {
const httpQueryResponse = await fetch(`${origin}/http-query`);
const httpQueryResponseText = await httpQueryResponse.text();

const httpUpdateResponse = await fetch(
`${origin}/http-update`,
{
method: 'POST'
}
);
const httpUpdateResponseText =
await httpUpdateResponse.text();
const httpUpdateResponse = await fetch(`${origin}/http-update`, {
method: 'POST'
});
const httpUpdateResponseText = await httpUpdateResponse.text();

const candidQueryText = await actor.candidQuery();
const candidUpdateText = await actor.candidUpdate();
const candidQueryText = await actor.candidQuery();
const candidUpdateText = await actor.candidUpdate();

return {
Ok:
httpQueryResponseText === 'http-query-canister' &&
httpUpdateResponseText === 'http-update-canister' &&
candidQueryText === 'candidQueryCanister' &&
candidUpdateText === 'candidUpdateCanister'
};
} catch (error: any) {
return {
Err: error
};
}
}
}
];
expect(httpQueryResponseText).toBe('http-query-canister');
expect(httpUpdateResponseText).toBe('http-update-canister');
expect(candidQueryText).toBe('candidQueryCanister');
expect(candidUpdateText).toBe('candidUpdateCanister');
});
};
}
145 changes: 57 additions & 88 deletions examples/hybrid_canister/test/tests/canister_init_and_post_upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getCanisterId } from 'azle/dfx';
import { Test } from 'azle/test';
import { expect, it, please, Test } from 'azle/test/jest';
import { execSync } from 'child_process';

import { createActor } from '../dfx_generated/canister_init_and_post_upgrade';

export function getTests(): Test[] {
export function getTests(): Test {
const canisterId = getCanisterId('canister_init_and_post_upgrade');
const origin = `http://${canisterId}.localhost:8000`;
const actor = createActor(canisterId, {
Expand All @@ -13,97 +13,66 @@ export function getTests(): Test[] {
}
});

return [
{
name: 'canister_init_and_post_upgrade init',
test: async () => {
try {
const httpQueryResponse = await fetch(
`${origin}/http-query`
);
const httpQueryResponseText =
await httpQueryResponse.text();
return () => {
it('properly hooks up http functionality to a canister with a developer defined init method', async () => {
const httpQueryResponse = await fetch(`${origin}/http-query`);
const httpQueryResponseText = await httpQueryResponse.text();

const httpUpdateResponse = await fetch(
`${origin}/http-update`,
{
method: 'POST'
}
);
const httpUpdateResponseText =
await httpUpdateResponse.text();
const httpUpdateResponse = await fetch(`${origin}/http-update`, {
method: 'POST'
});
const httpUpdateResponseText = await httpUpdateResponse.text();

const candidQueryText = await actor.candidQuery();
const candidUpdateText = await actor.candidUpdate();
const candidQueryText = await actor.candidQuery();
const candidUpdateText = await actor.candidUpdate();

return {
Ok:
httpQueryResponseText ===
'http-query-canister-init-and-post-upgrade-init' &&
httpUpdateResponseText ===
'http-update-canister-init-and-post-upgrade-init' &&
candidQueryText ===
'candidQueryCanisterInitAndPostUpgrade-init' &&
candidUpdateText ===
'candidUpdateCanisterInitAndPostUpgrade-init'
};
} catch (error: any) {
return {
Err: error
};
expect(httpQueryResponseText).toBe(
'http-query-canister-init-and-post-upgrade-init'
);
expect(httpUpdateResponseText).toBe(
'http-update-canister-init-and-post-upgrade-init'
);
expect(candidQueryText).toBe(
'candidQueryCanisterInitAndPostUpgrade-init'
);
expect(candidUpdateText).toBe(
'candidUpdateCanisterInitAndPostUpgrade-init'
);
});

please('deploy canister_init_and_post_upgrade', async () => {
execSync(
`dfx deploy --upgrade-unchanged canister_init_and_post_upgrade`,
{
stdio: 'inherit'
}
}
},
{
name: 'deploy canister_init_and_post_upgrade',
prep: async () => {
execSync(
`dfx deploy --upgrade-unchanged canister_init_and_post_upgrade`,
{
stdio: 'inherit'
}
);
}
},
{
name: 'canister_init_and_post_upgrade postUpgrade',
test: async () => {
try {
const httpQueryResponse = await fetch(
`${origin}/http-query`
);
const httpQueryResponseText =
await httpQueryResponse.text();
);
});

const httpUpdateResponse = await fetch(
`${origin}/http-update`,
{
method: 'POST'
}
);
const httpUpdateResponseText =
await httpUpdateResponse.text();
it('properly hooks up http functionality to a canister with a developer defined postUpgrade method', async () => {
const httpQueryResponse = await fetch(`${origin}/http-query`);
const httpQueryResponseText = await httpQueryResponse.text();

const candidQueryText = await actor.candidQuery();
const candidUpdateText = await actor.candidUpdate();
const httpUpdateResponse = await fetch(`${origin}/http-update`, {
method: 'POST'
});
const httpUpdateResponseText = await httpUpdateResponse.text();

return {
Ok:
httpQueryResponseText ===
'http-query-canister-init-and-post-upgrade-postUpgrade' &&
httpUpdateResponseText ===
'http-update-canister-init-and-post-upgrade-postUpgrade' &&
candidQueryText ===
'candidQueryCanisterInitAndPostUpgrade-postUpgrade' &&
candidUpdateText ===
'candidUpdateCanisterInitAndPostUpgrade-postUpgrade'
};
} catch (error: any) {
return {
Err: error
};
}
}
}
];
const candidQueryText = await actor.candidQuery();
const candidUpdateText = await actor.candidUpdate();

expect(httpQueryResponseText).toBe(
'http-query-canister-init-and-post-upgrade-postUpgrade'
);
expect(httpUpdateResponseText).toBe(
'http-update-canister-init-and-post-upgrade-postUpgrade'
);
expect(candidQueryText).toBe(
'candidQueryCanisterInitAndPostUpgrade-postUpgrade'
);
expect(candidUpdateText).toBe(
'candidUpdateCanisterInitAndPostUpgrade-postUpgrade'
);
});
};
}
26 changes: 9 additions & 17 deletions examples/hybrid_canister/test/tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import * as dns from 'node:dns';
dns.setDefaultResultOrder('ipv4first');

import { Test } from 'azle/test';
import { describe } from '@jest/globals';
import { Test } from 'azle/test/jest';

import { getTests as getTestsCanister } from './canister';
import { getTests as getTestsCanisterInitAndPostUpgrade } from './canister_init_and_post_upgrade';
import { getTests as getTestsServer } from './server';
import { getTests as getTestsServerInitAndPostUpgrade } from './server_init_and_post_upgrade';

export function getTests(): Test[] {
const canisterInitAndPostUpgradeTests =
getTestsCanisterInitAndPostUpgrade();
const canisterTests = getTestsCanister();
const serverInitAndPostUpgradeTests = getTestsServerInitAndPostUpgrade();
const serverTests = getTestsServer();

return [
...canisterInitAndPostUpgradeTests,
...canisterTests,
...serverInitAndPostUpgradeTests,
...serverTests
];
export function getTests(): Test {
return () => {
describe('', getTestsCanister());
describe('', getTestsCanisterInitAndPostUpgrade());
describe('', getTestsServer());
describe('', getTestsServerInitAndPostUpgrade());
};
}
55 changes: 18 additions & 37 deletions examples/hybrid_canister/test/tests/server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getCanisterId } from 'azle/dfx';
import { Test } from 'azle/test';
import { expect, it, Test } from 'azle/test/jest';

import { createActor } from '../dfx_generated/server';

export function getTests(): Test[] {
export function getTests(): Test {
const canisterId = getCanisterId('server');
const origin = `http://${canisterId}.localhost:8000`;
const actor = createActor(canisterId, {
Expand All @@ -12,42 +12,23 @@ export function getTests(): Test[] {
}
});

return [
{
name: 'server',
test: async () => {
try {
const httpQueryResponse = await fetch(
`${origin}/http-query`
);
const httpQueryResponseText =
await httpQueryResponse.text();
return () => {
it('handles a http server canister with additional query and update methods', async () => {
const httpQueryResponse = await fetch(`${origin}/http-query`);
const httpQueryResponseText = await httpQueryResponse.text();

const httpUpdateResponse = await fetch(
`${origin}/http-update`,
{
method: 'POST'
}
);
const httpUpdateResponseText =
await httpUpdateResponse.text();
const httpUpdateResponse = await fetch(`${origin}/http-update`, {
method: 'POST'
});
const httpUpdateResponseText = await httpUpdateResponse.text();

const candidQueryText = await actor.candidQuery();
const candidUpdateText = await actor.candidUpdate();
const candidQueryText = await actor.candidQuery();
const candidUpdateText = await actor.candidUpdate();

return {
Ok:
httpQueryResponseText === 'http-query-server' &&
httpUpdateResponseText === 'http-update-server' &&
candidQueryText === 'candidQueryServer' &&
candidUpdateText === 'candidUpdateServer'
};
} catch (error: any) {
return {
Err: error
};
}
}
}
];
expect(httpQueryResponseText).toBe('http-query-server');
expect(httpUpdateResponseText).toBe('http-update-server');
expect(candidQueryText).toBe('candidQueryServer');
expect(candidUpdateText).toBe('candidUpdateServer');
});
};
}
Loading

0 comments on commit 12d5a13

Please sign in to comment.