diff --git a/.eslintrc.js b/.eslintrc.js index a2207e6ba560..3bba4a402125 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -3,6 +3,8 @@ const path = require('node:path'); const ts = require('typescript'); const { version: reactVersion } = require('react/package.json'); +const { legacyMochaTests } = require('./test/mocha/legacy-mocha-tests'); + const tsconfigPath = ts.findConfigFile('./', ts.sys.fileExists); const { config } = ts.readConfigFile(tsconfigPath, ts.sys.readFile); const tsconfig = ts.parseJsonConfigFileContent(config, ts.sys, './'); @@ -261,26 +263,7 @@ module.exports = { * Mocha library. */ { - files: [ - '**/*.test.js', - 'test/lib/wait-until-called.js', - 'test/e2e/**/*.spec.js', - ], - excludedFiles: [ - 'app/scripts/controllers/app-state.test.js', - 'app/scripts/controllers/mmi-controller.test.js', - 'app/scripts/controllers/permissions/**/*.test.js', - 'app/scripts/controllers/preferences.test.js', - 'app/scripts/lib/**/*.test.js', - 'app/scripts/metamask-controller.test.js', - 'app/scripts/migrations/*.test.js', - 'app/scripts/platforms/*.test.js', - 'development/**/*.test.js', - 'shared/**/*.test.js', - 'ui/**/*.test.js', - 'ui/__mocks__/*.js', - 'test/e2e/helpers.test.js', - ], + files: [...legacyMochaTests, 'test/e2e/**/*.spec.js'], extends: ['@metamask/eslint-config-mocha'], rules: { // In Mocha tests, it is common to use `this` to store values or do @@ -298,26 +281,18 @@ module.exports = { { files: [ '**/__snapshots__/*.snap', - 'app/scripts/controllers/app-state.test.js', - 'app/scripts/controllers/mmi-controller.test.ts', - 'app/scripts/controllers/permissions/**/*.test.js', - 'app/scripts/controllers/preferences.test.js', - 'app/scripts/lib/**/*.test.js', - 'app/scripts/metamask-controller.test.js', - 'app/scripts/migrations/*.test.js', - 'app/scripts/platforms/*.test.js', - 'development/**/*.test.js', - 'development/**/*.test.ts', + '**/__mocks__/*.js', + 'app/scripts/**/*.test.js', + 'app/scripts/**/*.test.ts', + 'app/scripts/**/*.test.tsx', 'shared/**/*.test.js', 'shared/**/*.test.ts', - 'test/helpers/*.js', - 'test/jest/*.js', - 'test/lib/timer-helpers.js', - 'test/e2e/helpers.test.js', + 'shared/**/*.test.tsx', 'ui/**/*.test.js', - 'ui/__mocks__/*.js', - 'shared/lib/error-utils.test.js', + 'ui/**/*.test.ts', + 'ui/**/*.test.tsx', ], + excludedFiles: legacyMochaTests, extends: ['@metamask/eslint-config-jest'], parserOptions: { sourceType: 'module', diff --git a/.mocharc.js b/.mocharc.js index 5febd260a6cd..ff6200513b96 100644 --- a/.mocharc.js +++ b/.mocharc.js @@ -1,19 +1,6 @@ module.exports = { // TODO: Remove the `exit` setting, it can hide broken tests. exit: true, - ignore: [ - './app/scripts/lib/**/*.test.js', - './app/scripts/migrations/*.test.js', - './app/scripts/platforms/*.test.js', - './app/scripts/controllers/app-state.test.js', - './app/scripts/controllers/permissions/**/*.test.js', - './app/scripts/controllers/mmi-controller.test.ts', - './app/scripts/controllers/preferences.test.js', - './app/scripts/constants/error-utils.test.js', - './app/scripts/metamask-controller.test.js', - './development/fitness-functions/**/*.test.ts', - './test/e2e/helpers.test.js', - ], recursive: true, require: ['test/env.js', 'test/setup.js'], }; diff --git a/jest.config.js b/jest.config.js index f02cc20ab6b5..414fe0f88807 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,29 +1,22 @@ +const { + legacyMochaTests, + legacyMochaTestCoveredFiles, +} = require('./test/mocha/legacy-mocha-tests'); + module.exports = { collectCoverageFrom: [ - '/app/scripts/constants/error-utils.js', - '/app/scripts/controllers/app-metadata.ts', - '/app/scripts/controllers/permissions/**/*.js', - '/app/scripts/controllers/sign.ts', - '/app/scripts/controllers/decrypt-message.ts', - '/app/scripts/controllers/encryption-public-key.ts', - '/app/scripts/controllers/transactions/etherscan.ts', - '/app/scripts/controllers/transactions/EtherscanRemoteTransactionSource.ts', - '/app/scripts/controllers/transactions/IncomingTransactionHelper.ts', - '/app/scripts/controllers/preferences.js', - '/app/scripts/flask/**/*.js', - '/app/scripts/lib/**/*.(js|ts)', - '/app/scripts/metamask-controller.js', - '/app/scripts/migrations/*.js', - '/app/scripts/migrations/*.ts', - '!/app/scripts/migrations/*.test.(js|ts)', - '/app/scripts/platforms/*.js', + '/app/scripts/**/*.(js|ts|tsx)', '/shared/**/*.(js|ts|tsx)', '/ui/**/*.(js|ts|tsx)', - '/development/fitness-functions/**/*.test.(js|ts|tsx)', - '/test/e2e/helpers.test.js', ], coverageDirectory: './coverage', - coveragePathIgnorePatterns: ['.stories.*', '.snap'], + coveragePathIgnorePatterns: [ + '.stories.*', + '.snap', + ...legacyMochaTestCoveredFiles.map((filePath) => { + return `/${filePath}`; + }), + ], coverageReporters: ['html', 'json'], reporters: [ 'default', @@ -41,35 +34,15 @@ module.exports = { setupFiles: ['/test/setup.js', '/test/env.js'], setupFilesAfterEnv: ['/test/jest/setup.js'], testMatch: [ - '/app/scripts/constants/error-utils.test.js', - '/app/scripts/controllers/app-metadata.test.ts', - '/app/scripts/controllers/app-state.test.js', - '/app/scripts/controllers/encryption-public-key.test.ts', - '/app/scripts/controllers/transactions/etherscan.test.ts', - '/app/scripts/controllers/transactions/EtherscanRemoteTransactionSource.test.ts', - '/app/scripts/controllers/transactions/IncomingTransactionHelper.test.ts', - '/app/scripts/controllers/onboarding.test.ts', - '/app/scripts/controllers/mmi-controller.test.ts', - '/app/scripts/controllers/permissions/**/*.test.js', - '/app/scripts/controllers/preferences.test.js', - '/app/scripts/controllers/sign.test.ts', - '/app/scripts/controllers/decrypt-message.test.ts', - '/app/scripts/controllers/authentication/**/*.test.ts', - '/app/scripts/controllers/push-platform-notifications/**/*.test.ts', - '/app/scripts/controllers/user-storage/**/*.test.ts', - '/app/scripts/controllers/metamask-notifications/**/*.test.ts', - '/app/scripts/flask/**/*.test.js', - '/app/scripts/lib/**/*.test.(js|ts)', - '/app/scripts/lib/createRPCMethodTrackingMiddleware.test.js', - '/app/scripts/metamask-controller.test.js', - '/app/scripts/migrations/*.test.(js|ts)', - '/app/scripts/platforms/*.test.js', - '/app/scripts/translate.test.ts', - '/shared/**/*.test.(js|ts)', + '/app/scripts/**/*.test.(js|ts|tsx)', + '/shared/**/*.test.(js|ts|tsx)', '/ui/**/*.test.(js|ts|tsx)', '/development/fitness-functions/**/*.test.(js|ts|tsx)', '/test/e2e/helpers.test.js', ], + testPathIgnorePatterns: legacyMochaTests.map((filePath) => { + return `/${filePath}`; + }), testTimeout: 5500, // We have to specify the environment we are running in, which is jsdom. The // default is 'node'. This can be modified *per file* using a comment at the diff --git a/test/mocha/legacy-mocha-tests.js b/test/mocha/legacy-mocha-tests.js new file mode 100644 index 000000000000..a0550f873175 --- /dev/null +++ b/test/mocha/legacy-mocha-tests.js @@ -0,0 +1,24 @@ +// This is a list of remaining unit tests written for Mocha. +// We are in the middle of a migration from Mocha to Jest. Please do not add any additional test +// files to this list. We should only be removing files. +const legacyMochaTests = [ + 'app/scripts/controllers/metametrics.test.js', + 'app/scripts/controllers/swaps.test.js', + 'app/scripts/detect-multiple-instances.test.js', + 'app/scripts/metamask-controller.actions.test.js', +] + +const legacyMochaTestCoveredFiles = [ + 'app/scripts/controllers/metametrics.js', + 'app/scripts/controllers/swaps.js', + 'app/scripts/detect-multiple-instances.js', + // Intentionally omitted because it's partially covered by Jest tests as well + // Lets consider the Jest coverage as the authority on what is covered here, since we'll be + // migrating these tests to Jest eventually. + // 'app/scripts/metamask-controller.js' +] + +module.exports = { + legacyMochaTests, + legacyMochaTestCoveredFiles, +} diff --git a/test/run-unit-tests.js b/test/run-unit-tests.js index a58a858c09c8..e1072ae77d74 100644 --- a/test/run-unit-tests.js +++ b/test/run-unit-tests.js @@ -1,6 +1,7 @@ const { hideBin } = require('yargs/helpers'); const yargs = require('yargs/yargs'); const { runCommand, runInShell } = require('../development/lib/run-command'); +const { legacyMochaTests } = require('./mocha/legacy-mocha-tests'); const { CIRCLE_NODE_INDEX, CIRCLE_NODE_TOTAL } = process.env; @@ -71,7 +72,7 @@ async function runJest( * @param {boolean} coverage - Use nyc to collect coverage */ async function runMocha({ coverage }) { - const options = ['mocha', './app/**/*.test.js']; + const options = ['mocha', ...legacyMochaTests]; // If coverage is true, then we need to run nyc as the first command // and mocha after, so we use unshift to add three options to the beginning // of the options array.