Skip to content

Commit

Permalink
Migrate tests from tape to Jest
Browse files Browse the repository at this point in the history
The unit tests have been migrated from tape to Jest. The standard Jest
config from the MetaMask module template has been used.

We're using `ts-jest` to compile the tests and run them all at once, so
we don't need a build step for the tests now either. The `build`
command is now run separately on CI.

The tape tests didn't break the tests into sections, which is commonly
done for our Jest tests. So `describe` blocks haven't yet been used. I
will be adding `describe` sections in later PRs though, as the tests
are made more comprehensive.

The tests were left mostly functionally equivalent. Some included minor
changes, like checking the length of a property rather than just
whether it's truthy, or checking the error message, to confirm with our
Jest lint rules.
  • Loading branch information
Gudahtt committed Jul 16, 2021
1 parent 4d8324d commit 68b6033
Show file tree
Hide file tree
Showing 10 changed files with 2,326 additions and 521 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module.exports = {
files: ['*.ts'],
extends: ['@metamask/eslint-config-typescript'],
},
{
files: ['*.test.js'],
extends: ['@metamask/eslint-config-jest'],
},
],
rules: {
camelcase: [
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
key: yarn-cache-${{ runner.os }}-${{ steps.yarn-version.outputs.YARN_VERSION }}-${{ hashFiles('yarn.lock') }}
- run: yarn --frozen-lockfile
- run: yarn allow-scripts
- run: yarn build
- run: yarn lint
- run: yarn test
- name: Validate RC changelog
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ package-lock.json
dist
test/*.js

# ESLint
/.eslintcache

# Jest
/coverage

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Returns a hex-encoded public key.

### Testing and Linting

Run `yarn test` to run the tests.
Run `yarn test` to run the tests once. To run tests on file changes, run `yarn test:watch`.

Run `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and fix any automatically fixable issues.

Expand Down
27 changes: 27 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
collectCoverage: true,
// Ensures that we collect coverage from all source files, not just tested
// ones.
collectCoverageFrom: ['./src/**.ts'],
coverageReporters: ['text', 'html'],
coverageThreshold: {
global: {
branches: 74,
functions: 100,
lines: 94,
statements: 94,
},
},
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
preset: 'ts-jest',
// "resetMocks" resets all mocks, including mocked modules, to jest.fn(),
// between each test case.
resetMocks: true,
// "restoreMocks" restores all mocks created using jest.spyOn to their
// original implementations, between each test. It does not affect mocked
// modules.
restoreMocks: true,
testEnvironment: 'node',
testRegex: ['\\.test\\.(ts|js)$'],
testTimeout: 2500,
};
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
],
"scripts": {
"setup": "yarn install && yarn allow-scripts",
"build": "tsc --project ./tsconfig.json && tsc --project ./tsconfig.test.json",
"build": "tsc --project .",
"lint:eslint": "eslint . --cache --ext js,ts",
"lint:json": "prettier '**/*.json' --ignore-path .gitignore",
"lint": "yarn lint:eslint && yarn lint:json --check",
"lint:fix": "yarn lint:eslint --fix && yarn lint:json --write",
"test": "yarn build && node test/index.js",
"test": "jest",
"test:watch": "jest --watch",
"prepublishOnly": "yarn build"
},
"resolutions": {
Expand All @@ -43,19 +44,23 @@
"@lavamoat/allow-scripts": "^1.0.6",
"@metamask/auto-changelog": "^2.4.0",
"@metamask/eslint-config": "^7.0.1",
"@metamask/eslint-config-jest": "^7.0.0",
"@metamask/eslint-config-nodejs": "^7.0.0",
"@metamask/eslint-config-typescript": "^7.0.1",
"@types/jest": "^26.0.24",
"@types/node": "^14.14.25",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-jest": "^24.3.6",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.0",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"prettier-plugin-packagejson": "^2.2.11",
"tape": "^4.9.1",
"ts-jest": "^27.0.3",
"typescript": "^4.1.3"
},
"engines": {
Expand Down
Loading

0 comments on commit 68b6033

Please sign in to comment.