Skip to content
This repository was archived by the owner on Nov 9, 2023. It is now read-only.

Commit 07f7604

Browse files
authored
Migrate to Jest (#102)
Migrates all tests to Jest and TypeScript per the module template. Care was taken to modify the existing tests as little as possible. Therefore, the tests unfortunately make prodigious use of casts to `any`. Nevertheless, to minimize such casts, a pair of assertion type guards were added for successful and failed JSON-RPC responses. They use the existing boolean type guards under the hood, and are fully tested. Assertion type guards are tremendously helpful in situations like this, where boolean type guards don't help since e.g. `expect(typeGuard(value)).toBe(true);` doesn't tell TypeScript anything, and we have lint rules preventing us from calling `expect` conditionally.
1 parent 75d2d37 commit 07f7604

20 files changed

+3480
-2133
lines changed

.eslintrc.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ module.exports = {
1818
},
1919

2020
{
21-
files: ['test/*'],
22-
extends: ['@metamask/eslint-config-mocha'],
23-
parserOptions: {
24-
ecmaVersion: 2020,
25-
},
21+
files: ['*.test.ts', '*.test.js'],
22+
extends: ['@metamask/eslint-config-jest'],
2623
},
2724
],
2825

29-
ignorePatterns: ['!.eslintrc.js', '.nyc*', 'coverage/', 'dist/'],
26+
ignorePatterns: ['!.eslintrc.js', '!.prettierrc.js', 'dist/'],
3027
};

jest.config.ts

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
* For a detailed explanation regarding each configuration property and type check, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
import type { Config } from '@jest/types';
7+
8+
const config: Config.InitialOptions = {
9+
// All imported modules in your tests should be mocked automatically
10+
// automock: false,
11+
12+
// Stop running tests after `n` failures
13+
// bail: 0,
14+
15+
// The directory where Jest should store its cached dependency information
16+
// cacheDirectory: "/private/var/folders/fk/c3y07g0576j8_2s9m01pk4qw0000gn/T/jest_dx",
17+
18+
// Automatically clear mock calls, instances and results before every test.
19+
// This does not remove any mock implementation that may have been provided,
20+
// so we disable it.
21+
// clearMocks: true,
22+
23+
// Indicates whether the coverage information should be collected while executing the test
24+
collectCoverage: true,
25+
26+
// An array of glob patterns indicating a set of files for which coverage information should be collected
27+
collectCoverageFrom: ['./src/**/*.ts'],
28+
29+
// The directory where Jest should output its coverage files
30+
coverageDirectory: 'coverage',
31+
32+
// An array of regexp pattern strings used to skip coverage collection
33+
// coveragePathIgnorePatterns: [
34+
// "/node_modules/"
35+
// ],
36+
37+
// Indicates which provider should be used to instrument code for coverage
38+
coverageProvider: 'babel',
39+
40+
// A list of reporter names that Jest uses when writing coverage reports
41+
coverageReporters: ['text', 'html'],
42+
43+
// An object that configures minimum threshold enforcement for coverage results
44+
coverageThreshold: {
45+
global: {
46+
branches: 100,
47+
functions: 100,
48+
lines: 100,
49+
statements: 100,
50+
},
51+
},
52+
53+
// A path to a custom dependency extractor
54+
// dependencyExtractor: undefined,
55+
56+
// Make calling deprecated APIs throw helpful error messages
57+
// errorOnDeprecated: false,
58+
59+
// Force coverage collection from ignored files using an array of glob patterns
60+
// forceCoverageMatch: [],
61+
62+
// A path to a module which exports an async function that is triggered once before all test suites
63+
// globalSetup: undefined,
64+
65+
// A path to a module which exports an async function that is triggered once after all test suites
66+
// globalTeardown: undefined,
67+
68+
// A set of global variables that need to be available in all test environments
69+
// globals: {},
70+
71+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
72+
// maxWorkers: "50%",
73+
74+
// An array of directory names to be searched recursively up from the requiring module's location
75+
// moduleDirectories: [
76+
// "node_modules"
77+
// ],
78+
79+
// An array of file extensions your modules use
80+
// moduleFileExtensions: [
81+
// "js",
82+
// "jsx",
83+
// "ts",
84+
// "tsx",
85+
// "json",
86+
// "node"
87+
// ],
88+
89+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
90+
// moduleNameMapper: {},
91+
92+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
93+
// modulePathIgnorePatterns: [],
94+
95+
// Activates notifications for test results
96+
// notify: false,
97+
98+
// An enum that specifies notification mode. Requires { notify: true }
99+
// notifyMode: "failure-change",
100+
101+
// A preset that is used as a base for Jest's configuration
102+
preset: 'ts-jest',
103+
104+
// Run tests from one or more projects
105+
// projects: undefined,
106+
107+
// Use this configuration option to add custom reporters to Jest
108+
// reporters: undefined,
109+
110+
// "resetMocks" resets all mocks, including mocked modules, to jest.fn(),
111+
// between each test case.
112+
resetMocks: true,
113+
114+
// Reset the module registry before running each individual test
115+
// resetModules: false,
116+
117+
// A path to a custom resolver
118+
// resolver: undefined,
119+
120+
// "restoreMocks" restores all mocks created using jest.spyOn to their
121+
// original implementations, between each test. It does not affect mocked
122+
// modules.
123+
restoreMocks: true,
124+
125+
// The root directory that Jest should scan for tests and modules within
126+
// rootDir: undefined,
127+
128+
// A list of paths to directories that Jest should use to search for files in
129+
// roots: [
130+
// "<rootDir>"
131+
// ],
132+
133+
// Allows you to use a custom runner instead of Jest's default test runner
134+
// runner: "jest-runner",
135+
136+
// The paths to modules that run some code to configure or set up the testing environment before each test
137+
// setupFiles: [],
138+
139+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
140+
// setupFilesAfterEnv: [],
141+
142+
// The number of seconds after which a test is considered as slow and reported as such in the results.
143+
// slowTestThreshold: 5,
144+
145+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
146+
// snapshotSerializers: [],
147+
148+
// The test environment that will be used for testing
149+
// testEnvironment: "jest-environment-node",
150+
151+
// Options that will be passed to the testEnvironment
152+
// testEnvironmentOptions: {},
153+
154+
// Adds a location field to test results
155+
// testLocationInResults: false,
156+
157+
// The glob patterns Jest uses to detect test files
158+
// testMatch: [
159+
// "**/__tests__/**/*.[jt]s?(x)",
160+
// "**/?(*.)+(spec|test).[tj]s?(x)"
161+
// ],
162+
163+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
164+
// testPathIgnorePatterns: [
165+
// "/node_modules/"
166+
// ],
167+
168+
// The regexp pattern or array of patterns that Jest uses to detect test files
169+
// testRegex: [],
170+
171+
// This option allows the use of a custom results processor
172+
// testResultsProcessor: undefined,
173+
174+
// This option allows use of a custom test runner
175+
// testRunner: "jest-circus/runner",
176+
177+
// Reduce the default test timeout from 5s to 2.5s
178+
testTimeout: 2500,
179+
180+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
181+
// testURL: "http://localhost",
182+
183+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
184+
// timers: "real",
185+
186+
// A map from regular expressions to paths to transformers
187+
// transform: undefined,
188+
189+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
190+
// transformIgnorePatterns: [
191+
// "/node_modules/",
192+
// "\\.pnp\\.[^\\/]+$"
193+
// ],
194+
195+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
196+
// unmockedModulePathPatterns: undefined,
197+
198+
// Indicates whether each individual test should be reported during the run
199+
// verbose: undefined,
200+
201+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
202+
// watchPathIgnorePatterns: [],
203+
204+
// Whether to use watchman for file crawling
205+
// watchman: true,
206+
};
207+
208+
export default config;

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
2828
"prepublishOnly": "yarn build:clean && yarn lint && yarn test:coverage",
2929
"setup": "yarn install && yarn allow-scripts",
30-
"test": "mocha ./test",
31-
"test:coverage": "nyc --check-coverage yarn test"
30+
"test": "jest",
31+
"test:watch": "jest --watch"
3232
},
3333
"dependencies": {
3434
"@metamask/safe-event-emitter": "^2.0.0",
@@ -38,25 +38,26 @@
3838
"@lavamoat/allow-scripts": "^1.0.5",
3939
"@metamask/auto-changelog": "^2.3.0",
4040
"@metamask/eslint-config": "^9.0.0",
41-
"@metamask/eslint-config-mocha": "^6.0.0",
41+
"@metamask/eslint-config-jest": "^9.0.0",
4242
"@metamask/eslint-config-nodejs": "^9.0.0",
4343
"@metamask/eslint-config-typescript": "^9.0.1",
44+
"@types/jest": "^26.0.13",
4445
"@types/node": "^17.0.23",
4546
"@typescript-eslint/eslint-plugin": "^4.21.0",
4647
"@typescript-eslint/parser": "^4.21.0",
4748
"eslint": "^7.23.0",
4849
"eslint-config-prettier": "^8.1.0",
4950
"eslint-plugin-import": "^2.22.1",
51+
"eslint-plugin-jest": "^24.3.4",
5052
"eslint-plugin-jsdoc": "^36.1.0",
51-
"eslint-plugin-mocha": "^8.1.0",
5253
"eslint-plugin-node": "^11.1.0",
5354
"eslint-plugin-prettier": "^3.3.1",
54-
"mocha": "^7.1.1",
55-
"nyc": "^15.1.0",
55+
"jest": "^27.5.1",
5656
"prettier": "^2.2.1",
5757
"prettier-plugin-packagejson": "^2.2.11",
5858
"rimraf": "^3.0.2",
59-
"sinon": "^9.0.2",
59+
"ts-jest": "^27.1.4",
60+
"ts-node": "^10.7.0",
6061
"typescript": "^4.2.4"
6162
},
6263
"engines": {

0 commit comments

Comments
 (0)