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

feat: add e2e tests in template #794

Merged
merged 5 commits into from
Apr 14, 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
9 changes: 3 additions & 6 deletions packages/templates/api/assemblyscript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ A simple starter template that uses a SimpleStorage.sol contract ethereum. For m
`nvm install && nvm use`
`yarn`

## Start Test Environment
`yarn test:env:up`
## Build
`yarn build`

## Build & Deploy Web3API
`yarn deploy`

## Run Test Query Recipe
## Test
`yarn test`
17 changes: 17 additions & 0 deletions packages/templates/api/assemblyscript/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
collectCoverage: false,
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["**/__tests__/e2e/**/?(*.)+(spec|test).[jt]s?(x)"],
modulePathIgnorePatterns: [
"./src/__tests__/mutation",
"./src/__tests__/query",
"./src/__tests__/utils",
],
globals: {
"ts-jest": {
tsconfig: "tsconfig.json",
diagnostics: false,
},
},
};
13 changes: 11 additions & 2 deletions packages/templates/api/assemblyscript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@
"deploy": "yarn deploy:contract && yarn deploy:web3api",
"deploy:web3api": "npx w3 build --ipfs http://localhost:5001 --test-ens simplestorage.eth",
"deploy:contract": "node ./scripts/deploy-contract.js",
"test": "npx w3 query ./recipes/e2e.json"
"test": "yarn test:e2e && yarn test:env:up && yarn deploy && yarn test:recipe && yarn test:env:down",
"test:e2e": "yarn build && yarn test:e2e:codegen && jest --passWithNoTests --runInBand --verbose",
"test:e2e:codegen": "npx w3 app codegen -m ./src/__tests__/types/web3api.app.yaml -c ./src/__tests__/types/w3",
"test:recipe": "npx w3 query ./recipes/e2e.json"
},
"devDependencies": {
"@types/jest": "27.0.3",
"@types/node": "16.11.11",
"@web3api/cli": "0.0.1-prealpha.71",
"@web3api/ethereum-plugin-js": "0.0.1-prealpha.71",
"@web3api/wasm-as": "0.0.1-prealpha.71",
"assemblyscript": "0.19.1",
"ethers": "5.0.7",
"solc": "0.8.3"
"solc": "0.8.3",
"jest": "27.4.2",
"ts-jest": "27.0.7",
"ts-node": "10.4.0",
"typescript": "4.4.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Web3ApiClient } from "@web3api/client-js";
import {
buildAndDeployApi,
initTestEnvironment,
stopTestEnvironment,
} from "@web3api/test-env-js";
import * as App from "../types/w3";
import path from "path";

import { getPlugins } from "../utils";

jest.setTimeout(500000);

describe("SimpleStorage", () => {
const CONNECTION = { node: "http://localhost:8545" };

let client: Web3ApiClient;
let ensUri: string;

beforeAll(async () => {
const {
ethereum: testEnvEtherem,
ensAddress,
ipfs,
} = await initTestEnvironment();
// deploy api
const apiPath: string = path.join(
path.resolve(__dirname),
"..",
"..",
".."
);
const api = await buildAndDeployApi(apiPath, ipfs, ensAddress);
ensUri = `ens/testnet/${api.ensDomain}`;

// get client
const config = getPlugins(testEnvEtherem, ipfs, ensAddress);
client = new Web3ApiClient(config);
});

afterAll(async () => {
await stopTestEnvironment();
});

const getData = async (contractAddr: string): Promise<number> => {
const response = await App.SimpleStorage_Query.getData(
{
address: contractAddr,
connection: CONNECTION,
},
client,
ensUri
);

expect(response).toBeTruthy();
expect(response.error).toBeFalsy();
expect(response.data).not.toBeNull();

return response.data as number;
}

const setData = async (contractAddr: string, value: number): Promise<string> => {
const response = await App.SimpleStorage_Mutation.setData(
{
address: contractAddr,
connection: CONNECTION,
value: value,
},
client,
ensUri
);

expect(response).toBeTruthy();
expect(response.error).toBeFalsy();
expect(response.data).not.toBeNull();

return response.data as string;
}

test("sanity", async () => {
// Deploy contract
const deployContractResponse = await App.SimpleStorage_Mutation.deployContract({connection: CONNECTION}, client, ensUri);
expect(deployContractResponse).toBeTruthy();
expect(deployContractResponse.error).toBeFalsy();
expect(deployContractResponse.data).toBeTruthy();

const contractAddress = deployContractResponse.data as string;

// Get data
let data = await getData(contractAddress);
expect(data).toBe(0);

// Set data
const tx = await setData(contractAddress, 10);
expect(tx).toBeTruthy();

// Get data
data = await getData(contractAddress);
expect(data).toBe(10);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import * into SimpleStorage from "ens/simple-storage.eth"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
format: 0.0.1-prealpha.1
language: app/typescript
schema: ./schema.graphql
import_redirects:
- uri: "w3://ens/simple-storage.eth"
schema: "../../../build/schema.graphql"
64 changes: 64 additions & 0 deletions packages/templates/api/assemblyscript/src/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ClientConfig } from "@web3api/client-js";
import { ensPlugin } from "@web3api/ens-plugin-js";
import { ethereumPlugin } from "@web3api/ethereum-plugin-js";
import { ipfsPlugin } from "@web3api/ipfs-plugin-js";
import { buildAndDeployApi } from "@web3api/test-env-js";
import axios from "axios";
import path from "path";

interface TestEnvironment {
ipfs: string;
ethereum: string;
ensAddress: string;
clientConfig: Partial<ClientConfig>;
}

export function getPlugins(
ethereum: string,
ipfs: string,
ensAddress: string,
): Partial<ClientConfig> {
return {
redirects: [],
plugins: [
{
uri: "w3://ens/ipfs.web3api.eth",
plugin: ipfsPlugin({ provider: ipfs }),
},
{
uri: "w3://ens/ens.web3api.eth",
plugin: ensPlugin({ addresses: { testnet: ensAddress } }),
},
{
uri: "w3://ens/ethereum.web3api.eth",
plugin: ethereumPlugin({
networks: {
testnet: {
provider: ethereum,
},
MAINNET: {
provider: "http://localhost:8546",
},
},
defaultNetwork: "testnet",
}),
},
],
};
}

export async function getProviders(): Promise<TestEnvironment> {
const {
data: { ipfs, ethereum },
} = await axios.get("http://localhost:4040/providers");
const { data } = await axios.get("http://localhost:4040/deploy-ens");
const clientConfig = getPlugins(ethereum, ipfs, data.ensAddress);
return { ipfs, ethereum, ensAddress: data.ensAddress, clientConfig };
}

export async function getEnsUri(): Promise<string> {
const { ensAddress, ipfs } = await getProviders();
const apiPath: string = path.resolve(__dirname + "/../../");
const api = await buildAndDeployApi(apiPath, ipfs, ensAddress);
return `ens/testnet/${api.ensDomain}`;
}
12 changes: 12 additions & 0 deletions packages/templates/api/assemblyscript/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "assemblyscript/std/assembly.json",
"compilerOptions": {
"outDir": "build"
},
"include": [
"./src/**/*.ts",
],
"exclude": [
"./src/__tests__/e2e/**/*.ts"
]
}
28 changes: 28 additions & 0 deletions packages/templates/api/assemblyscript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compilerOptions": {
"lib": [
"es2020",
"es2015",
"es5",
"dom"
],
"esModuleInterop": true,
"outDir": "build",
"moduleResolution": "node",
"declaration": true,
"preserveSymlinks": true,
"preserveWatchOutput": true,
"pretty": false,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"module": "commonjs",
"sourceMap": true,
"target": "es5",
"resolveJsonModule": true,
"strictNullChecks": true
},
"typeAcquisition": { "include": ["jest"] },
}