Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
use-yarn-hydrate: true

- name: test:integration:coverage
run: yarn test:integration:coverage --silent
run: yarn test:integration:coverage
Copy link
Contributor Author

@gauthierpetetin gauthierpetetin Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to remove the --silent flag to be able to detect new warnings/errors


- name: Rename coverage
run: mv coverage/integration/coverage-final.json coverage/integration/coverage-integration.json
Expand Down
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ module.exports = {
// Jest doesn't support Prettier 3 yet, so we use Prettier 2
prettierPath: require.resolve('prettier-2'),
reporters: [
// Console baseline reporter MUST be first to capture raw console messages
// before jest-clean-console-reporter processes them
[
'<rootDir>/test/jest/console-baseline-reporter.js',
{
testType: 'unit',
},
],
[
'jest-clean-console-reporter',
{
Expand Down Expand Up @@ -66,4 +74,7 @@ module.exports = {
customExportConditions: ['node', 'node-addons'],
},
workerIdleMemoryLimit: '500MB',
// Ensure console output is buffered (not streamed) so reporters can access testResult.console
// Without this, Jest uses verbose mode for single-file runs which bypasses buffering
verbose: false,
};
11 changes: 11 additions & 0 deletions jest.integration.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ module.exports = {
// Jest doesn't support Prettier 3 yet, so we use Prettier 2
prettierPath: require.resolve('prettier-2'),
reporters: [
// Console baseline reporter MUST be first to capture raw console messages
// before jest-clean-console-reporter processes them
[
'<rootDir>/test/jest/console-baseline-reporter.js',
{
testType: 'integration',
},
],
[
'jest-clean-console-reporter',
{
Expand Down Expand Up @@ -63,4 +71,7 @@ module.exports = {
'jest-preview/transforms/file',
},
transformIgnorePatterns: ['/node_modules/'],
// Ensure console output is buffered (not streamed) so reporters can access testResult.console
// Without this, Jest uses verbose mode for single-file runs which bypasses buffering
verbose: false,
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@
"forwarder": "node ./development/static-server.js ./node_modules/@metamask/forwarder/dist/ --port 9010",
"dapp-forwarder": "concurrently -k -n forwarder,dapp -p '[{time}][{name}]' 'yarn forwarder' 'yarn dapp'",
"test:unit": "jest",
"test:unit:update-baseline": "UPDATE_BASELINE=true yarn test:unit",
"anvil": "node_modules/.bin/anvil",
"test:unit:watch": "jest --watch",
"test:unit:coverage": "jest --coverage",
"test:unit:webpack": "tsx --test development/webpack/test/*.test.ts",
"test:unit:webpack:coverage": "nyc --reporter=html --reporter=json --reporter=text --report-dir=./coverage/webpack tsx --test development/webpack/test/*.test.ts",
"test:integration": "yarn webpack-cli build --config ./development/webpack/webpack.integration.tests.config.ts && jest --config jest.integration.config.js",
"test:integration:update-baseline": "UPDATE_BASELINE=true yarn test:integration",
"test:integration:coverage": "yarn test:integration --coverage",
"test:e2e:chrome": "SELENIUM_BROWSER=chrome tsx test/e2e/run-all.ts",
"test:e2e:chrome:dist": "SELENIUM_BROWSER=chrome tsx test/e2e/run-all.ts --dist",
Expand Down
196 changes: 196 additions & 0 deletions test/jest/CONSOLE-BASELINE-TEST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# Console Baseline Reporter - Manual Testing Steps

## Prerequisites

- Node.js v24+
- Run `yarn install`

---

## Test 1: Unit Test Warning Detection

### 1. Add a test warning to a unit test

Add this code to `app/scripts/controllers/permissions/background-api.test.js` at the top of the first `describe` block:

```javascript
describe('permission background API methods', () => {
beforeEach(() => {
// Test warning to verify baseline system
console.warn(
'REVIEWER_TEST: This warning should be detected by the baseline system',
);
});

// ... rest of the tests
});
```

### 2. Run the unit test - should FAIL with the new warning

```bash
yarn test:unit app/scripts/controllers/permissions/background-api.test.js
```

**Expected output:**

```
PASS app/scripts/controllers/permissions/background-api.test.js

Test Suites: 1 passed, 1 total
Tests: 45 passed, 45 total
...

❌ BASELINE VIOLATIONS DETECTED

📁 app/scripts/controllers/permissions/background-api.test.js
🆕 NEW: warn: REVIEWER_TEST: This warning should
Current: 45 occurrences

💡 Next steps:
1. Fix the warnings in your code, OR
2. Update baseline: yarn test:unit:update-baseline (requires justification)
```

**Note:** Jest shows "Test Suites: 1 passed" but the command **fails** (exit code 1) due to the baseline violation.

### 3. Add the warning to the baseline

```bash
yarn test:unit:update-baseline app/scripts/controllers/permissions/background-api.test.js
```

**Expected output:**

```
PASS app/scripts/controllers/permissions/background-api.test.js

✅ Baseline updated: 1 file(s) updated, 495 total in baseline
Written to: /path/to/test/jest/console-baseline-unit.json
```

### 4. Re-run the test - should PASS

```bash
yarn test:unit app/scripts/controllers/permissions/background-api.test.js
```

**Expected output:**

```
PASS app/scripts/controllers/permissions/background-api.test.js

✅ Console baseline matches exactly!
```

✅ **System verified!** The warning is now in the baseline and the test passes.

### 5. Clean up

Remove the test warning from `app/scripts/controllers/permissions/background-api.test.js`:

```javascript
describe('permission background API methods', () => {
// DELETE the beforeEach block with the REVIEWER_TEST warning
// ... rest of the tests
});
```

Then update the baseline to remove it:

```bash
yarn test:unit:update-baseline app/scripts/controllers/permissions/background-api.test.js
```

Verify tests still pass:

```bash
yarn test:unit app/scripts/controllers/permissions/background-api.test.js
```

---

## Test 2: Integration Test Warning Detection

### 1. Add a test warning to an integration test

Add this code to `test/integration/confirmations/signatures/permit.test.tsx` at the top of the first `describe` block:

```typescript
describe('Permit Confirmation', () => {
beforeEach(() => {
// Test warning to verify baseline system
console.warn(
'REVIEWER_TEST: This warning should be detected by the baseline system',
);
});

// ... rest of the tests
});
```

### 2. Run the integration test - should FAIL with the new warning

```bash
yarn test:integration test/integration/confirmations/signatures/permit.test.tsx
```

**Expected output:**

```
PASS test/integration/confirmations/signatures/permit.test.tsx
...

❌ BASELINE VIOLATIONS DETECTED

📁 test/integration/confirmations/signatures/permit.test.tsx
🆕 NEW: warn: REVIEWER_TEST: This warning should
Current: X occurrences

💡 Next steps:
1. Fix the warnings in your code, OR
2. Update baseline: yarn test:integration:update-baseline (requires justification)
```

### 3. Add the warning to the baseline

```bash
yarn test:integration:update-baseline test/integration/confirmations/signatures/permit.test.tsx
```

### 4. Re-run the test - should PASS

```bash
yarn test:integration test/integration/confirmations/signatures/permit.test.tsx
```

**Expected output:**

```
✅ Console baseline matches exactly!
```

✅ **System verified!**

### 5. Clean up

Remove the test warning from `test/integration/confirmations/signatures/permit.test.tsx` and update baseline:

```bash
yarn test:integration:update-baseline test/integration/confirmations/signatures/permit.test.tsx
```

---

## Summary

All test types follow the same pattern:

| Step | Action | Result |
| ---- | --------------- | --------------------- |
| 1 | Add warning | ❌ Test fails |
| 2 | Update baseline | Warning added to JSON |
| 3 | Re-run test | ✅ Test passes |
| 4 | Clean up | Remove test code |

This demonstrates the baseline system correctly detects, stores, and validates console warnings!
88 changes: 88 additions & 0 deletions test/jest/CONSOLE-BASELINE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Console Baseline Enforcement

Prevents new console warnings/errors from being introduced. Baseline is tracked **per test file**.

## Quick Reference

| Command | Description |
| ---------------------------------------------------- | -------------------------------------- |
| `yarn test:unit` | Run tests with baseline enforcement |
| `yarn test:unit:update-baseline` | Update baseline (all files) |
| `yarn test:unit:update-baseline path/to/file` | Update baseline (single file) |
| `yarn test:integration` | Run integration tests with enforcement |
| `yarn test:integration:update-baseline` | Update integration baseline |
| `yarn test:integration:update-baseline path/to/file` | Update baseline (single file) |

## How It Works

1. **Run tests** → Reporter compares console output against baseline
2. **Violation detected** → Test fails, shows which file/category increased
3. **Improvement detected** → Shows reduced warnings (doesn't fail)
4. **Update baseline** → Run with `:update-baseline` to accept changes

## Example Output

```
❌ BASELINE VIOLATIONS DETECTED

📁 ui/pages/send/send.test.tsx
⬆️ React: Act warnings
Baseline: 5, Current: 12 (+7)

💡 Next steps:
1. Fix the warnings in your code, OR
2. Update baseline: yarn test:unit:update-baseline (requires justification)
```

## Workflow

```bash
# 1. Run tests (fails if warnings increased)
yarn test:unit path/to/my-component.test.tsx

# 2. Fix warnings in code, OR update baseline if justified
yarn test:unit:update-baseline path/to/my-component.test.tsx

# 3. Commit baseline changes
git add test/jest/console-baseline-*.json
git commit -m "fix: reduce warnings in my-component"
```

## Configuration

In `jest.config.js`:

```javascript
reporters: [
[
'<rootDir>/test/jest/console-baseline-reporter.js',
{
testType: 'unit', // 'unit' or 'integration'
failOnViolation: true, // Fail on increased warnings
showImprovements: true, // Show reduced warnings
},
],
];
```

## Baseline Files

- `test/jest/console-baseline-unit.json` - Unit test baseline
- `test/jest/console-baseline-integration.json` - Integration test baseline

Structure:

```json
{
"files": {
"path/to/test.tsx": {
"React: Act warnings": 5,
"MetaMask: Background connection not initialized": 2
}
}
}
```

## Related Documentation

- [Manual Testing Guide](./CONSOLE-BASELINE-TEST.md) - Step-by-step verification instructions
Loading
Loading