Skip to content

Use jest for unit tests#5427

Merged
shiftkey merged 23 commits intomasterfrom
jest
Aug 29, 2018
Merged

Use jest for unit tests#5427
shiftkey merged 23 commits intomasterfrom
jest

Conversation

@outofambit
Copy link
Copy Markdown
Contributor

@outofambit outofambit commented Aug 17, 2018

Goals

  • port our unit testing setup to something simpler and more featured
    • specifically, support for test coverage metrics
  • explore alternatives to electron-mocha that play nicer with other libraries

Implementation

  • jest
  • refactored scripts/unit-test.ts into configuration/setup files used by jest (see jest.config.js)
  • manually mocked electron libraries for tests (see __mocks__/electron.ts)
  • mocked database with fake-indexeddb
  • run jest in electron as node to ensure similar build architecture

Discussion

  • had a couple disagreements with the linter that could be cleaned up better
  • one outstanding issue with VSTS regarding git config location (still looking into this)

@j-f1
Copy link
Copy Markdown
Contributor

j-f1 commented Aug 17, 2018

Has the team read about snapshot testing in Jest yet?

@outofambit
Copy link
Copy Markdown
Contributor Author

@j-f1 i am familiar with it! i haven't spent much time thinking how to apply them in this codebase tho, so if you have any thoughts, send 'em my way :)

outofambit and others added 3 commits August 17, 2018 14:16
Co-Authored-By: Brendan Forster <github@brendanforster.com>
@outofambit outofambit changed the title [WIP] Spike on using Jest for unit tests Use jest for unit tests Aug 17, 2018
@kateeliza24

This comment has been minimized.

@outofambit outofambit added the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Aug 19, 2018
@@ -0,0 +1,19 @@
export const shell = {
moveItemToTrash: jest.fn(),

This comment was marked as spam.

@@ -0,0 +1,19 @@
export const shell = {

This comment was marked as spam.

"@types/keytar": "^4.0.0",
"@types/legal-eagle": "^0.15.0",
"@types/mini-css-extract-plugin": "^0.2.0",
"@types/mocha": "^2.2.48",

This comment was marked as spam.

"klaw-sync": "^3.0.0",
"legal-eagle": "0.16.0",
"mini-css-extract-plugin": "^0.4.0",
"mocha": "^5.0.4",

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

repositoryRoot,
'node_modules',
'.bin',
'xvfb-maybe'

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


const Dexie = require('dexie')
Dexie.dependencies.indexedDB = require('fake-indexeddb')
Dexie.dependencies.IDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange')

This comment was marked as spam.

@@ -0,0 +1,3 @@
/* eslint-disable strict */

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@@ -0,0 +1,16 @@
/* eslint-disable strict */

This comment was marked as spam.

@shiftkey shiftkey self-assigned this Aug 27, 2018
@outofambit
Copy link
Copy Markdown
Contributor Author

thanks for #5455 @shiftkey!! 💜

Copy link
Copy Markdown
Member

@shiftkey shiftkey left a comment

Choose a reason for hiding this comment

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

I had a few deeper Jest questions (mostly around how to organize the mocks we've already accrued) but for now I just focused on testing this and other bits that can be tidied up.

package.json Outdated
"electron-winstaller": "2.5.2"
"electron-winstaller": "2.5.2",
"jest": "^23.5.0",
"ts-jest": "^23.1.3"

This comment was marked as spam.

package.json Outdated
"electron-packager": "^12.0.0",
"electron-winstaller": "2.5.2"
"electron-winstaller": "2.5.2",
"jest": "^23.5.0",

This comment was marked as spam.

"test:unit": "cross-env ELECTRON_RUN_AS_NODE=1 node_modules/.bin/electron ./node_modules/jest/bin/jest --config ./jest.config.js",
"test:unit:cov": "yarn test:unit --coverage",
"test:script": "mocha -P ./tsonfig.json -t 10000 --require ts-node/register script/changelog/test/*.ts",
"test": "yarn test:unit --runInBand && yarn test:script && yarn test:integration",

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

// ensuring Electron doesn't attach to the current console session (Windows only)
ELECTRON_NO_ATTACH_CONSOLE: '1',
// speed up ts-node usage by using the new transpile-only mode
TS_NODE_TRANSPILE_ONLY: 'true',

This comment was marked as spam.

@@ -0,0 +1,4 @@
'use strict'

This comment was marked as spam.

package.json Outdated
@@ -156,6 +158,8 @@
"electron-builder": "20.27.1",
"electron-mocha": "^6.0.1",

This comment was marked as spam.

@@ -0,0 +1,17 @@
'use strict'

This comment was marked as spam.

@outofambit
Copy link
Copy Markdown
Contributor Author

outofambit commented Aug 27, 2018

@shiftkey i tried removing the use stricts from those two .ts files, but keep getting these linter errors from yarn lint:

$ yarn lint
yarn run v1.9.4
$ yarn lint:src && yarn lint:prettier
$ yarn tslint && yarn eslint-check && yarn eslint
$ tslint -p .
$ eslint --print-config .eslintrc.* | eslint-config-prettier-check
No rules that are unnecessary or conflict with Prettier were found.
$ ts-node -P script/tsconfig.json script/eslint.ts

/Users/outofambit/GitHub/desktop/app/test/setup-test-framework.ts
  2:1  error  Use the global form of 'use strict'  strict

/Users/outofambit/GitHub/desktop/app/test/unit-test-env.ts
  1:1  error  Use the global form of 'use strict'  strict

any ideas? i've tried a lot of stuff but am getting nowhere.

@shiftkey
Copy link
Copy Markdown
Member

any ideas? i've tried a lot of stuff but am getting nowhere.

I believe this is down to eslint considering these two files to be different to the rest of our codebase ("no import hints -> plain old script"?), and also ignoring that we've configured tsconfig.json to be strict: true which implies alwaysStrict when emitting TS ([docs]).

I can workaround this by adding an import * as Path from 'path' to the start of each file, in place of 'use strict' but that's unsatisfying because it's dead code - and I couldn't quite figure out the right eslint flag to set.

Three other workaround ideas I had:

  • disable each rule explicitly
diff --git a/app/test/setup-test-framework.ts b/app/test/setup-test-framework.ts
index e2baa26f0..36615fc18 100644
--- a/app/test/setup-test-framework.ts
+++ b/app/test/setup-test-framework.ts
@@ -1,4 +1,4 @@
-'use strict'
+/* eslint-disable strict */

 // set test timeout to 10s
 jest.setTimeout(10000)
diff --git a/app/test/unit-test-env.ts b/app/test/unit-test-env.ts
index 1088212de..a315028ef 100644
--- a/app/test/unit-test-env.ts
+++ b/app/test/unit-test-env.ts
@@ -1,4 +1,4 @@
-'use strict'
+/* eslint-disable strict */

 const environmentVariables = {
   // setting commit information so that tests don't need to rely on global config
  • disable the strict rule for all files under app/test (my preference):
diff --git a/app/test/.eslintrc.yml b/app/test/.eslintrc.yml
index 37d751d0d..38cf443ed 100644
--- a/app/test/.eslintrc.yml
+++ b/app/test/.eslintrc.yml
@@ -1,3 +1,4 @@
 rules:
   # throws with Chai
   no-unused-expressions: off
+  strict: off
  • leave it as-is and move on with our lives

aka "gawd, don't be so strict"
@outofambit
Copy link
Copy Markdown
Contributor Author

🍏

Copy link
Copy Markdown
Member

@shiftkey shiftkey left a comment

Choose a reason for hiding this comment

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

Looks great @outofambit! I've opened #5496 to track writing some docs on this broad area, but that doesn't need to hold up this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-review Pull Requests that are ready to be reviewed by the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants