Skip to content

Commit c26555e

Browse files
authored
Update test scripts to match MetaMask/core (#2745)
This updates the test scripts to match the scripts in `MetaMask/core`. Primarily, it introduces a couple new test scripts: - `test`: Runs Jest with `jest-silent-reporter` to only show failed tests. - `test:clean`: Clears the Jest cache before running tests. - `test:verbose`: Runs Jest with verbose logging. - `test:watch`: Runs Jest in watch mode.
1 parent 5913f79 commit c26555e

File tree

107 files changed

+555
-123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+555
-123
lines changed

.github/workflows/build-lint-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ jobs:
217217
run: yarn install-chrome
218218
- run: yarn workspace @metamask/snaps-sdk run build
219219
if: ${{ matrix.package-name == '@metamask/snaps-cli' }}
220-
- run: yarn workspace ${{ matrix.package-name }} run test:ci
220+
- run: yarn workspace ${{ matrix.package-name }} run test
221221
- name: Get coverage folder
222222
id: get-coverage-folder
223223
run: |
@@ -296,7 +296,7 @@ jobs:
296296
- name: Build snap
297297
run: yarn workspace ${{ matrix.package-name }} run build
298298
- name: Run E2E test
299-
run: yarn workspace ${{ matrix.package-name }} run test:e2e
299+
run: yarn workspace ${{ matrix.package-name }} run test
300300
- name: Require clean working directory
301301
shell: bash
302302
run: |
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// @ts-check
2+
3+
/**
4+
* Run an array of tasks in sequence.
5+
*
6+
* @param tasks {Array<() => Promise<number>>} - An array of functions that
7+
* return a promise that resolves to an exit code.
8+
* @returns {Promise<number>} - The exit code of the first task that returns a
9+
* non-zero exit code, or 0 if all tasks return 0.
10+
*/
11+
async function sequence(tasks) {
12+
for (const task of tasks) {
13+
const exitCode = await task();
14+
15+
if (exitCode !== 0) {
16+
return exitCode;
17+
}
18+
}
19+
20+
return 0;
21+
}
22+
23+
/**
24+
* Run a lifecycle script.
25+
*
26+
* @param {import('@yarnpkg/core').Workspace} workspace - The workspace to run
27+
* the script in.
28+
* @param {string} scriptName - The name of the script to run.
29+
* @param {Record<string, *>} extra - Extra options to pass to the execute
30+
* function.
31+
* @param {Function} require - The require function to use.
32+
*/
33+
function getLifecycleTask(workspace, scriptName, { stderr, stdin, stdout }, require) {
34+
const { scriptUtils } = require('@yarnpkg/core');
35+
36+
return async () => {
37+
if (scriptUtils.hasWorkspaceScript(workspace, scriptName)) {
38+
return scriptUtils.executeWorkspaceScript(workspace, scriptName, [], {
39+
cwd: workspace.cwd,
40+
stderr,
41+
stdin,
42+
stdout,
43+
});
44+
}
45+
46+
return 0;
47+
};
48+
}
49+
50+
/**
51+
* A Yarn plugin that calls lifecycle scripts before and after running a script.
52+
* The scripts are expected to be named `${scriptName}:pre` and
53+
* `${scriptName}:post`.
54+
*
55+
* @type {import('@yarnpkg/core').PluginConfiguration}
56+
*/
57+
module.exports = {
58+
name: 'plugin-lifecycle-scripts',
59+
factory: (require) => {
60+
return {
61+
default: {
62+
hooks: {
63+
wrapScriptExecution: async (script, project, locator, scriptName, extra) => {
64+
return async () => {
65+
const workspace = project.getWorkspaceByLocator(locator);
66+
return await sequence([
67+
getLifecycleTask(workspace, `${scriptName}:pre`, extra, require),
68+
script,
69+
getLifecycleTask(workspace, `${scriptName}:post`, extra, require),
70+
])
71+
};
72+
}
73+
}
74+
}
75+
}
76+
}
77+
};

.yarnrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ plugins:
2121
- path: .yarn/plugins/@yarnpkg/plugin-allow-scripts.cjs
2222
spec: 'https://raw.githubusercontent.com/LavaMoat/LavaMoat/main/packages/yarn-plugin-allow-scripts/bundles/@yarnpkg/plugin-allow-scripts.js'
2323
- path: .yarn/plugins/local/plugin-workspaces-filter.js
24+
- path: .yarn/plugins/local/plugin-lifecycle-scripts.js
2425

2526
yarnPath: .yarn/releases/yarn-4.4.1.cjs

constraints.pro

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,41 @@ gen_enforced_field(WorkspaceCwd, 'scripts.changelog:update', ChangelogUpdateScri
255255
gen_enforced_field(WorkspaceCwd, 'scripts.lint:dependencies', 'depcheck') :-
256256
WorkspaceCwd \= '.'.
257257

258+
% The test scripts must be the same for all packages.
259+
gen_enforced_field(WorkspaceCwd, 'scripts.test', 'jest --reporters=jest-silent-reporter') :-
260+
WorkspaceCwd \= '.',
261+
WorkspaceCwd \= 'packages/snaps-controllers',
262+
WorkspaceCwd \= 'packages/snaps-execution-environments',
263+
WorkspaceCwd \= 'packages/snaps-utils',
264+
\+ is_example(WorkspaceCwd).
265+
gen_enforced_field(WorkspaceCwd, 'scripts.test', 'jest --reporters=jest-silent-reporter && yarn test:browser') :-
266+
WorkspaceCwd == 'packages/snaps-controllers'.
267+
gen_enforced_field(WorkspaceCwd, 'scripts.test', 'jest --reporters=jest-silent-reporter && yarn test:browser') :-
268+
WorkspaceCwd == 'packages/snaps-execution-environments'.
269+
gen_enforced_field(WorkspaceCwd, 'scripts.test', 'jest --reporters=jest-silent-reporter && yarn test:browser') :-
270+
WorkspaceCwd == 'packages/snaps-utils'.
271+
gen_enforced_field(WorkspaceCwd, 'scripts.test:clean', 'jest --clearCache') :-
272+
WorkspaceCwd \= '.',
273+
\+ is_example(WorkspaceCwd).
274+
gen_enforced_field(WorkspaceCwd, 'scripts.test:verbose', 'jest --verbose') :-
275+
WorkspaceCwd \= '.',
276+
\+ is_example(WorkspaceCwd).
277+
gen_enforced_field(WorkspaceCwd, 'scripts.test:watch', 'jest --watch') :-
278+
WorkspaceCwd \= '.',
279+
\+ is_example(WorkspaceCwd).
280+
gen_enforced_field(WorkspaceCwd, 'scripts.test:post', 'jest-it-up') :-
281+
WorkspaceCwd \= '.',
282+
WorkspaceCwd \= 'packages/snaps-controllers',
283+
WorkspaceCwd \= 'packages/snaps-execution-environments',
284+
WorkspaceCwd \= 'packages/snaps-utils',
285+
\+ is_example(WorkspaceCwd).
286+
gen_enforced_field(WorkspaceCwd, 'scripts.test:post', 'ts-node scripts/coverage.ts && rimraf coverage/jest coverage/wdio') :-
287+
WorkspaceCwd == 'packages/snaps-controllers'.
288+
gen_enforced_field(WorkspaceCwd, 'scripts.test:post', 'ts-node scripts/coverage.ts && rimraf coverage/jest coverage/wdio') :-
289+
WorkspaceCwd == 'packages/snaps-execution-environments'.
290+
gen_enforced_field(WorkspaceCwd, 'scripts.test:post', 'ts-node scripts/coverage.ts && rimraf coverage/jest coverage/wdio') :-
291+
WorkspaceCwd == 'packages/snaps-utils'.
292+
258293
% The "engines.node" field must be the same for all packages.
259294
gen_enforced_field(WorkspaceCwd, 'engines.node', '^18.16 || >=20').
260295

@@ -276,9 +311,13 @@ gen_enforced_field(WorkspaceCwd, 'scripts.start', 'mm-snap watch') :-
276311
WorkspaceCwd \= 'packages/examples/packages/webpack-plugin'.
277312
gen_enforced_field(WorkspaceCwd, 'scripts.clean', 'rimraf "dist"') :-
278313
is_example(WorkspaceCwd).
279-
gen_enforced_field(WorkspaceCwd, 'scripts.test', 'yarn test:e2e') :-
314+
gen_enforced_field(WorkspaceCwd, 'scripts.test', 'jest --reporters=jest-silent-reporter') :-
315+
is_example(WorkspaceCwd).
316+
gen_enforced_field(WorkspaceCwd, 'scripts.test:clean', 'jest --clearCache') :-
317+
is_example(WorkspaceCwd).
318+
gen_enforced_field(WorkspaceCwd, 'scripts.test:verbose', 'jest --verbose') :-
280319
is_example(WorkspaceCwd).
281-
gen_enforced_field(WorkspaceCwd, 'scripts.test:e2e', 'jest') :-
320+
gen_enforced_field(WorkspaceCwd, 'scripts.test:watch', 'jest --watch') :-
282321
is_example(WorkspaceCwd).
283322
gen_enforced_field(WorkspaceCwd, 'scripts.lint', 'yarn lint:eslint && yarn lint:misc --check && yarn changelog:validate && yarn lint:dependencies') :-
284323
is_example(WorkspaceCwd).

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
"publish-previews": "yarn workspaces foreach --all --parallel --verbose run publish:preview",
3232
"test": "yarn workspaces foreach --all --parallel --verbose run test",
3333
"test:browser": "yarn workspaces foreach --all --verbose run test:browser",
34-
"test:e2e": "yarn workspaces foreach --all --verbose --exclude root run test:e2e",
34+
"test:clean": "yarn workspaces foreach --all --parallel --verbose run test:clean",
35+
"test:verbose": "yarn workspaces foreach --all --parallel --verbose run test:verbose",
36+
"test:watch": "yarn workspaces foreach --all --parallel --verbose run test:watch",
3537
"update-readme-content": "tsx ./scripts/update-readme-content.mts"
3638
},
3739
"simple-git-hooks": {
@@ -95,6 +97,7 @@
9597
"favicons": "^7.1.2",
9698
"geckodriver": "^4.2.0",
9799
"jest": "^29.0.2",
100+
"jest-silent-reporter": "^0.6.0",
98101
"lint-staged": "^12.4.1",
99102
"minimatch": "^7.4.1",
100103
"prettier": "^2.8.8",

packages/create-snap/.depcheckrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@typescript-eslint/*",
1111
"eslint-config-*",
1212
"eslint-plugin-*",
13+
"jest-silent-reporter",
1314
"prettier-plugin-packagejson",
1415
"ts-node",
1516
"typedoc",

packages/create-snap/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@
4343
"lint:misc": "prettier --no-error-on-unmatched-pattern --loglevel warn \"**/*.json\" \"**/*.md\" \"**/*.html\" \"!CHANGELOG.md\" --ignore-path ../../.gitignore",
4444
"publish:package": "../../scripts/publish-package.sh",
4545
"publish:preview": "yarn npm publish --tag preview",
46-
"test": "jest && yarn posttest",
47-
"posttest": "jest-it-up",
48-
"test:ci": "yarn test",
49-
"test:watch": "yarn test --watch"
46+
"test": "jest --reporters=jest-silent-reporter",
47+
"test:clean": "jest --clearCache",
48+
"test:post": "jest-it-up",
49+
"test:verbose": "jest --verbose",
50+
"test:watch": "jest --watch"
5051
},
5152
"dependencies": {
5253
"@metamask/snaps-utils": "workspace:^",
@@ -80,6 +81,7 @@
8081
"eslint-plugin-promise": "^6.1.1",
8182
"jest": "^29.0.2",
8283
"jest-it-up": "^2.0.0",
84+
"jest-silent-reporter": "^0.6.0",
8385
"memfs": "^3.4.13",
8486
"prettier": "^2.8.8",
8587
"prettier-plugin-packagejson": "^2.5.2",

packages/examples/.depcheckrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@typescript-eslint/*",
1010
"eslint-config-*",
1111
"eslint-plugin-*",
12+
"jest-silent-reporter",
1213
"prettier-plugin-packagejson",
1314
"ts-node",
1415
"typedoc",

packages/examples/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
"lint:misc": "prettier --no-error-on-unmatched-pattern --loglevel warn \"**/*.json\" \"**/*.md\" \"**/*.html\" \"!CHANGELOG.md\" \"!packages/**\" --ignore-path ../../.gitignore",
2525
"start": "yarn workspaces foreach --worktree --parallel --verbose --interlaced --no-private --jobs unlimited run start",
2626
"start:test": "yarn start",
27-
"test": "yarn workspaces foreach --worktree --parallel --verbose --interlaced --no-private run test"
27+
"test": "jest --reporters=jest-silent-reporter",
28+
"test:clean": "jest --clearCache",
29+
"test:post": "jest-it-up",
30+
"test:verbose": "jest --verbose",
31+
"test:watch": "jest --watch"
2832
},
2933
"devDependencies": {
3034
"@lavamoat/allow-scripts": "^3.0.4",
@@ -44,6 +48,7 @@
4448
"eslint-plugin-n": "^15.7.0",
4549
"eslint-plugin-prettier": "^4.2.1",
4650
"eslint-plugin-promise": "^6.1.1",
51+
"jest-silent-reporter": "^0.6.0",
4752
"prettier": "^2.8.8",
4853
"prettier-plugin-packagejson": "^2.5.2",
4954
"ts-node": "^10.9.1",

packages/examples/packages/bip32/.depcheckrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@typescript-eslint/*",
1010
"eslint-config-*",
1111
"eslint-plugin-*",
12+
"jest-silent-reporter",
1213
"prettier-plugin-packagejson",
1314
"ts-node",
1415
"typedoc",

0 commit comments

Comments
 (0)