Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix: jest config (#131)
Browse files Browse the repository at this point in the history
* fix jest config issue

* update jest readme
  • Loading branch information
BeroBurny committed Sep 14, 2022
1 parent 1855a8a commit 2151b67
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 15 deletions.
20 changes: 20 additions & 0 deletions docs/JEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ describe('Ethereum', () => {
});
```

### Configuring preset
To configure Dappeteer to use custom config values as `metamaskVersion` or own `seed` phrase you need to create file `dappeteer.config.js` that exports config object

**example of `dappeteer.config.js`**
``` js
/** @type {import('@chainsafe/dappeteer').DappateerJestConfig} */

const config = {
dappeteer: {
metamaskVersion: 'v10.14.0',
},
metamask: {
seed: 'already turtle birth enroll since owner keep patch skirt drift any dinner',
password: 'password1234',
},
};

module.exports = config;
```

## Custom example without preset

In case you need more customisable use case you can rebuild it from scratch.
Expand Down
2 changes: 1 addition & 1 deletion jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
globalSetup: `@chainsafe/dappeteer/dist/jest/setup.js`,
globalTeardown: `@chainsafe/dappeteer/dist/jest/teardown.js`,
testEnvironment: `@chainsafe/dappeteer/dist/jest//DappeteerEnvironment.js`,
testEnvironment: `@chainsafe/dappeteer/dist/jest/DappeteerEnvironment.js`,
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export { getMetamask, getMetamaskWindow } from './metamask';
export * from './types';
export * from './setup';
export { DappateerJestConfig } from './jest/global';

// default constants
export const RECOMMENDED_METAMASK_VERSION = 'v10.15.0';
35 changes: 35 additions & 0 deletions src/jest/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import path from 'path';

import { existsSync } from 'node:fs';
import { cwd } from 'node:process';

import { RECOMMENDED_METAMASK_VERSION } from '../index';
import { LaunchOptions } from '../types';

import { DappateerJestConfig } from './global';

export const DAPPETEER_DEFAULT_CONFIG: LaunchOptions = { metamaskVersion: RECOMMENDED_METAMASK_VERSION };

export async function getDappeteerConfig(): Promise<DappateerJestConfig> {
const configPath = 'dappeteer.config.js';
const filePath = path.resolve(cwd(), configPath);

if (!existsSync(filePath))
return {
dappeteer: DAPPETEER_DEFAULT_CONFIG,
metamask: {},
};

// eslint-disable-next-line @typescript-eslint/no-require-imports
const config = await require(filePath);

return {
dappeteer: {
...DAPPETEER_DEFAULT_CONFIG,
...config.dappeteer,
},
metamask: {
...config.metamask,
},
};
}
11 changes: 5 additions & 6 deletions src/jest/global.d.ts → src/jest/global.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Config } from '@jest/types';
import { Browser, Page } from 'puppeteer';

import { Dappeteer, LaunchOptions, MetamaskOptions } from '..';

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
interface Global {
page: Page;
Expand All @@ -13,8 +13,7 @@ declare global {
}
}

export type DappateerConfig = Config.InitialOptions &
Partial<{
dappeteer: LaunchOptions;
metamask: MetamaskOptions;
}>;
export type DappateerJestConfig = Partial<{
dappeteer: LaunchOptions;
metamask: MetamaskOptions;
}>;
12 changes: 6 additions & 6 deletions src/jest/setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import puppeteer from 'puppeteer';

import { launch, LaunchOptions, setupMetamask } from '../index';
import { launch, setupMetamask } from '../index';

import { DappateerConfig } from './global';
import { getDappeteerConfig } from './config';

export const DAPPETEER_DEFAULT_CONFIG: LaunchOptions = { metamaskVersion: 'latest' };
export default async function (): Promise<void> {
const { dappeteer, metamask } = await getDappeteerConfig();

export default async function (jestConfig: DappateerConfig = { dappeteer: DAPPETEER_DEFAULT_CONFIG }): Promise<void> {
const browser = await launch(puppeteer, jestConfig.dappeteer || DAPPETEER_DEFAULT_CONFIG);
const browser = await launch(puppeteer, dappeteer);
try {
await setupMetamask(browser, jestConfig.metamask);
await setupMetamask(browser, metamask);
global.browser = browser;
} catch (error) {
// eslint-disable-next-line no-console
Expand Down
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { RECOMMENDED_METAMASK_VERSION } from './index';

export type LaunchOptions = OfficialOptions | CustomOptions;

export type OfficialOptions = BrowserLaunchArgumentOptions & {
type DappaterBrowserLaunchArgumentOptions = Omit<BrowserLaunchArgumentOptions, 'headless'>;

export type OfficialOptions = DappaterBrowserLaunchArgumentOptions & {
metamaskVersion: typeof RECOMMENDED_METAMASK_VERSION | 'latest' | string;
metamaskLocation?: Path;
};

export type CustomOptions = BrowserLaunchArgumentOptions & {
export type CustomOptions = DappaterBrowserLaunchArgumentOptions & {
metamaskVersion?: string;
metamaskPath: string;
};
Expand Down

0 comments on commit 2151b67

Please sign in to comment.