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

Configure tests to run in both browser and node env #1341

Merged
merged 9 commits into from
Apr 25, 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
23 changes: 20 additions & 3 deletions packages/accounts/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true
preset: "ts-jest",
collectCoverage: true,
projects: [
{
displayName: "dom",
testEnvironment: "jsdom", // runs tests in a browser-like environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.dom.js?(x)",
],
},
{
displayName: "node",
testEnvironment: "node", // runs tests in a Node.js environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.node.js?(x)",
],
},
],
};
2 changes: 1 addition & 1 deletion packages/accounts/test/providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const base58 = require('bs58');

const testUtils = require('./test-utils');
const { KeyPair } = require('@near-js/crypto');
let ERRORS_JSON = require('../../utils/lib/errors/error_messages.json');
let ERRORS_JSON = require('@near-js/utils/lib/errors/error_messages.json');

jest.setTimeout(60000);

Expand Down
22 changes: 22 additions & 0 deletions packages/biometric-ed25519/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
preset: "ts-jest",
collectCoverage: true,
projects: [
{
displayName: "dom",
testEnvironment: "jsdom", // runs tests in a browser-like environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.dom.js?(x)",
],
},
{
displayName: "node",
testEnvironment: "node", // runs tests in a Node.js environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.node.js?(x)",
],
},
],
};
23 changes: 20 additions & 3 deletions packages/crypto/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true
preset: "ts-jest",
collectCoverage: true,
projects: [
{
displayName: "dom",
testEnvironment: "jsdom", // runs tests in a browser-like environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.dom.js?(x)",
],
},
{
displayName: "node",
testEnvironment: "node", // runs tests in a Node.js environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.node.js?(x)",
],
},
],
};
2 changes: 1 addition & 1 deletion packages/crypto/src/key_pair_ed25519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class KeyPairEd25519 extends KeyPairBase {
super();
const decoded = baseDecode(extendedSecretKey);
const secretKey = new Uint8Array(decoded.slice(0, KeySize.SECRET_KEY));
const publicKey = ed25519.getPublicKey(secretKey);
const publicKey = ed25519.getPublicKey(new Uint8Array(secretKey));
this.publicKey = new PublicKey({ keyType: KeyType.ED25519, data: publicKey });
this.secretKey = baseEncode(secretKey);
this.extendedSecretKey = extendedSecretKey;
Expand Down
4 changes: 3 additions & 1 deletion packages/crypto/test/key_pair.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { baseEncode } = require('@near-js/utils');
const { sha256 } = require('@noble/hashes/sha256');

const { KeyPair, KeyPairEd25519, PublicKey } = require('../lib');

const { TextEncoder } = require('util');
global.TextEncoder = TextEncoder;

test('test sign and verify', async () => {
const keyPair = new KeyPairEd25519('26x56YPzPDro5t2smQfGcYAPy3j7R2jB2NUb7xKbAGK23B6x4WNQPh3twb6oDksFov5X8ts5CtntUNbpQpAKFdbR');
expect(keyPair.publicKey.toString()).toEqual('ed25519:AYWv9RAN1hpSQA4p1DLhCNnpnNXwxhfH9qeHN8B4nJ59');
Expand Down
23 changes: 20 additions & 3 deletions packages/keystores-browser/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true
preset: "ts-jest",
collectCoverage: true,
projects: [
{
displayName: "dom",
testEnvironment: "jsdom", // runs tests in a browser-like environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.dom.js?(x)",
],
},
{
displayName: "node",
testEnvironment: "node", // runs tests in a Node.js environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.node.js?(x)",
],
},
],
};
2 changes: 1 addition & 1 deletion packages/keystores-browser/test/browser_keystore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ describe('Browser keystore', () => {
ctx.keyStore = new BrowserLocalStorageKeyStore(require('localstorage-memory'));
});

require('./keystore_common').shouldStoreAndRetriveKeys(ctx);
require('./keystore_common').shouldStoreAndRetrieveKeys(ctx);
});
2 changes: 1 addition & 1 deletion packages/keystores-browser/test/keystore_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const NETWORK_ID_SINGLE_KEY = 'singlekeynetworkid';
const ACCOUNT_ID_SINGLE_KEY = 'singlekey_accountid';
const KEYPAIR_SINGLE_KEY = new KeyPairEd25519('2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw');

module.exports.shouldStoreAndRetriveKeys = ctx => {
module.exports.shouldStoreAndRetrieveKeys = ctx => {
beforeEach(async () => {
await ctx.keyStore.clear();
await ctx.keyStore.setKey(NETWORK_ID_SINGLE_KEY, ACCOUNT_ID_SINGLE_KEY, KEYPAIR_SINGLE_KEY);
Expand Down
23 changes: 20 additions & 3 deletions packages/keystores-node/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true
preset: "ts-jest",
collectCoverage: true,
projects: [
{
displayName: "dom",
testEnvironment: "jsdom", // runs tests in a browser-like environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.dom.js?(x)",
],
},
{
displayName: "node",
testEnvironment: "node", // runs tests in a Node.js environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.node.js?(x)",
],
},
],
};
23 changes: 20 additions & 3 deletions packages/keystores/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true
preset: "ts-jest",
collectCoverage: true,
projects: [
{
displayName: "dom",
testEnvironment: "jsdom", // runs tests in a browser-like environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.dom.js?(x)",
],
},
{
displayName: "node",
testEnvironment: "node", // runs tests in a Node.js environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.node.js?(x)",
],
},
],
};
2 changes: 1 addition & 1 deletion packages/keystores/test/in_memory_keystore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ describe('In-memory keystore', () => {
ctx.keyStore = new InMemoryKeyStore();
});

require('./keystore_common').shouldStoreAndRetriveKeys(ctx);
require('./keystore_common').shouldStoreAndRetrieveKeys(ctx);
});
2 changes: 1 addition & 1 deletion packages/keystores/test/keystore_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const NETWORK_ID_SINGLE_KEY = 'singlekeynetworkid';
const ACCOUNT_ID_SINGLE_KEY = 'singlekey_accountid';
const KEYPAIR_SINGLE_KEY = new KeyPairEd25519('2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw');

module.exports.shouldStoreAndRetriveKeys = ctx => {
module.exports.shouldStoreAndRetrieveKeys = ctx => {
beforeEach(async () => {
await ctx.keyStore.clear();
await ctx.keyStore.setKey(NETWORK_ID_SINGLE_KEY, ACCOUNT_ID_SINGLE_KEY, KEYPAIR_SINGLE_KEY);
Expand Down
2 changes: 1 addition & 1 deletion packages/keystores/test/merge_keystore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ describe('Merge keystore', () => {
expect(await ctx.stores[1].getAccounts('network')).toHaveLength(0);
});

require('./keystore_common').shouldStoreAndRetriveKeys(ctx);
require('./keystore_common').shouldStoreAndRetrieveKeys(ctx);
});
89 changes: 45 additions & 44 deletions packages/near-api-js/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,50 @@ const fs = require('fs');
let worker;
module.exports = async function getConfig(env) {
switch (env) {
case 'production':
case 'mainnet':
return {
networkId: 'mainnet',
nodeUrl: 'https://rpc.mainnet.near.org',
walletUrl: 'https://wallet.near.org',
helperUrl: 'https://helper.mainnet.near.org',
};
case 'development':
case 'testnet':
return {
networkId: 'default',
nodeUrl: 'https://rpc.testnet.near.org',
walletUrl: 'https://wallet.testnet.near.org',
helperUrl: 'https://helper.testnet.near.org',
masterAccount: 'test.near',
};
case 'betanet':
return {
networkId: 'betanet',
nodeUrl: 'https://rpc.betanet.near.org',
walletUrl: 'https://wallet.betanet.near.org',
helperUrl: 'https://helper.betanet.near.org',
};
case 'local':
return {
networkId: 'local',
nodeUrl: 'http://localhost:3030',
keyPath: `${process.env.HOME}/.near/validator_key.json`,
walletUrl: 'http://localhost:4000/wallet',
};
case 'test':
case 'ci':
if (!worker) worker = await Worker.init();
const keyFile = fs.readFileSync(`${worker.rootAccount.manager.config.homeDir}/validator_key.json`);
const keyPair = JSON.parse(keyFile.toString());
return {
networkId: worker.config.network,
nodeUrl: worker.manager.config.rpcAddr,
masterAccount: worker.rootAccount._accountId,
secretKey: keyPair.secret_key || keyPair.private_key
};
default:
throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`);
case 'production':
case 'mainnet':
return {
networkId: 'mainnet',
nodeUrl: 'https://rpc.mainnet.near.org',
walletUrl: 'https://wallet.near.org',
helperUrl: 'https://helper.mainnet.near.org',
};
case 'development':
case 'testnet':
return {
networkId: 'default',
nodeUrl: 'https://rpc.testnet.near.org',
walletUrl: 'https://wallet.testnet.near.org',
helperUrl: 'https://helper.testnet.near.org',
masterAccount: 'test.near',
};
case 'betanet':
return {
networkId: 'betanet',
nodeUrl: 'https://rpc.betanet.near.org',
walletUrl: 'https://wallet.betanet.near.org',
helperUrl: 'https://helper.betanet.near.org',
};
case 'local':
return {
networkId: 'local',
nodeUrl: 'http://localhost:3030',
keyPath: `${process.env.HOME}/.near/validator_key.json`,
walletUrl: 'http://localhost:4000/wallet',
};
case 'test':
case 'ci': {
if (!worker) worker = await Worker.init();
const keyFile = fs.readFileSync(`${worker.rootAccount.manager.config.homeDir}/validator_key.json`);
const keyPair = JSON.parse(keyFile.toString());
return {
networkId: worker.config.network,
nodeUrl: worker.manager.config.rpcAddr,
masterAccount: worker.rootAccount._accountId,
secretKey: keyPair.secret_key || keyPair.private_key
};
}
default:
throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`);
}
};
23 changes: 20 additions & 3 deletions packages/providers/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true
preset: "ts-jest",
collectCoverage: true,
projects: [
{
displayName: "dom",
testEnvironment: "jsdom", // runs tests in a browser-like environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.dom.js?(x)",
],
},
{
displayName: "node",
testEnvironment: "node", // runs tests in a Node.js environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.node.js?(x)",
],
},
],
};
3 changes: 3 additions & 0 deletions packages/providers/test/providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const { getTransactionLastResult } = require('@near-js/utils');
const { Worker } = require('near-workspaces');
const { JsonRpcProvider, FailoverRpcProvider } = require('../lib');

const { TextEncoder } = require('util');
global.TextEncoder = TextEncoder;

jest.setTimeout(20000);

['json provider', 'fallback provider'].forEach((name) => {
Expand Down
23 changes: 20 additions & 3 deletions packages/signers/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true
preset: "ts-jest",
collectCoverage: true,
projects: [
{
displayName: "dom",
testEnvironment: "jsdom", // runs tests in a browser-like environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.dom.js?(x)",
],
},
{
displayName: "node",
testEnvironment: "node", // runs tests in a Node.js environment
testMatch: [
"**/test/**/*.test.js?(x)",
"**/test/**/*.test.node.js?(x)",
],
},
],
};
3 changes: 3 additions & 0 deletions packages/signers/test/signer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const { InMemoryKeyStore } = require('@near-js/keystores');

const { InMemorySigner } = require('../lib');

const { TextEncoder } = require('util');
global.TextEncoder = TextEncoder;

test('test no key', async() => {
const signer = new InMemorySigner(new InMemoryKeyStore());
await expect(signer.signMessage('message', 'user', 'network'))
Expand Down
Loading
Loading