Skip to content
Closed
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
7 changes: 6 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ module.exports = {
extends: ['@metamask/eslint-config-jest'],
},
{
files: ['*.js'],
files: ['scripts/*.js'],
parserOptions: {
sourceType: 'script',
ecmaVersion: '2018',
},
rules: {
'node/no-process-exit': 'off',
'node/no-sync': 'off',
'node/shebang': 'off',
},
},
{
files: ['*.ts'],
Expand Down
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* text=auto

yarn.lock linguist-generated=false
# Collapse changes to Polly recordings in PRs.
tests/__recordings__/** linguist-generated
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = {
// modules.
restoreMocks: true,
setupFiles: ['./tests/setupTests.ts'],
testEnvironment: 'jsdom',
testRegex: ['\\.test\\.(ts|js)$'],
setupFilesAfterEnv: ['./tests/setupTestsAfterEnv.ts'],
testEnvironment: 'setup-polly-jest/jest-environment-node',
testTimeout: 5000,
transform: {
'^.+\\.tsx?$': 'ts-jest',
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@metamask/contract-metadata": "^1.33.0",
"@metamask/metamask-eth-abis": "3.0.0",
"@metamask/types": "^1.1.0",
"@types/setup-polly-jest": "^0.5.1",
"@types/uuid": "^8.3.0",
"abort-controller": "^3.0.0",
"async-mutex": "^0.2.6",
Expand Down Expand Up @@ -79,6 +80,10 @@
"@metamask/eslint-config-jest": "^9.0.0",
"@metamask/eslint-config-nodejs": "^9.0.0",
"@metamask/eslint-config-typescript": "^9.0.1",
"@pollyjs/adapter-fetch": "^6.0.4",
"@pollyjs/adapter-node-http": "^6.0.4",
"@pollyjs/core": "^6.0.4",
"@pollyjs/persister-fs": "^6.0.4",
"@types/deep-freeze-strict": "^1.1.0",
"@types/jest": "^26.0.22",
"@types/jest-when": "^2.7.3",
Expand All @@ -97,13 +102,15 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.1",
"ethjs-provider-http": "^0.1.6",
"glob": "^7.2.0",
"jest": "^26.4.2",
"jest-environment-jsdom": "^25.0.0",
"jest-when": "^3.4.2",
"nock": "^13.0.7",
"prettier": "^2.6.2",
"prettier-plugin-packagejson": "^2.2.17",
"rimraf": "^3.0.2",
"setup-polly-jest": "^0.10.0",
"sinon": "^9.2.4",
"ts-jest": "^26.5.2",
"typedoc": "^0.22.15",
Expand Down
50 changes: 50 additions & 0 deletions scripts/check-for-expired-polly-recordings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env node
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Polly has an expiresIn setting, which causes the recordings Polly has captured to automatically expire after a set time. Since we set recordIfMissing: false, this will cause tests with expired recordings to fail tests, which may force developers to recapture recordings for tests that they've never touched. For this reason, I've opted to write this script to check for expired recordings instead of using expiresIn. That way we could run this on CI or locally or wherever and it wouldn't interfere with development.


const path = require('path');
const fs = require('fs');
const glob = require('glob');

const getThirtyDaysAgo = () => {
const now = new Date();
now.setMonth(now.getMonth() - 1);
return now;
};

/**
* This script scans tests/__recordings__, which is where Polly captures
* recordings of requests, and looks for files that have captured requests that
* are older than 30 days. If any are found, the script will report the names of
* the tests that have these recordings and exit with 1. Otherwise, it will
* exit with 0.
*/
function main() {
const thirtyDaysAgoTimestamp = getThirtyDaysAgo().getTime();
const outdatedTests = [];
const filePaths = glob.sync(
path.resolve(__dirname, '../tests/__recordings__/**/*.har'),
{ realpath: true },
);
filePaths.forEach((filePath) => {
const encodedJson = fs.readFileSync(filePath, 'utf8');
const decodedJson = JSON.parse(encodedJson);
const isOutdated = decodedJson.log.entries.some((entry) => {
const timestamp = Date.parse(entry.startedDateTime);
return timestamp < thirtyDaysAgoTimestamp;
});
if (isOutdated) {
outdatedTests.push(decodedJson.log._recordingName);
}
});

if (outdatedTests.length > 0) {
console.log('Some tests have outdated Polly recordings!');
outdatedTests.forEach((test) => {
console.log(`- ${test.replace(/\//gu, ' -> ')}`);
});
process.exit(1);
} else {
console.log('No tests have outdated Polly recordings, all good!');
}
}

main();
Loading