Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 1ec76aa

Browse files
committed
Add karma test runner
1 parent 456f369 commit 1ec76aa

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

.github/workflows/build.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,16 @@ jobs:
2828
uses: coverallsapp/github-action@master
2929
with:
3030
github-token: ${{ secrets.GITHUB_TOKEN }}
31+
32+
test-browser:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/setup-node@v1
36+
with:
37+
node-version: 12.x
38+
39+
- uses: actions/checkout@v1
40+
41+
- run: npm install
42+
- run: npm run test:browser
43+

karma.conf.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module.exports = function(config) {
2+
config.set({
3+
frameworks: ['mocha', 'karma-typescript'],
4+
files: ['src/**/*.ts', 'test/**/*.ts'],
5+
preprocessors: {
6+
'**/*.ts': ['karma-typescript'],
7+
},
8+
plugins: ['karma-mocha', 'karma-typescript', 'karma-chrome-launcher', 'karma-firefox-launcher'],
9+
karmaTypescriptConfig: {
10+
bundlerOptions: {
11+
entrypoints: /\.spec\.ts$/,
12+
acornOptions: {
13+
ecmaVersion: 8,
14+
},
15+
transforms: [require('karma-typescript-es6-transform')()],
16+
},
17+
},
18+
colors: true,
19+
reporters: ['progress', 'karma-typescript'],
20+
browsers: ['FirefoxHeadless', 'ChromeHeadless'],
21+
singleRun: true,
22+
concurrency: Infinity,
23+
// Extend timeouts for long tests
24+
browserDisconnectTimeout: 1000000,
25+
browserNoActivityTimeout: 1000000,
26+
})
27+
}

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"format:fix": "ethereumjs-config-format-fix",
1717
"lint": "ethereumjs-config-lint",
1818
"lint:fix": "ethereumjs-config-lint-fix",
19-
"test": "npm run build && nyc --reporter=lcov mocha ./test/**/*.ts && npm run test:browser",
19+
"test": "npm run build && nyc --reporter=lcov mocha ./test/**/*.ts",
20+
"test:browser": "karma start karma.conf.js",
2021
"tsc": "ethereumjs-config-tsc",
2122
"tslint": "ethereumjs-config-tslint",
2223
"tslint:fix": "ethereumjs-config-tslint-fix"
@@ -63,6 +64,12 @@
6364
"@types/node": "^12.0.10",
6465
"ethers": "^4.0.33",
6566
"husky": "^2.1.0",
67+
"karma": "^5.0.2",
68+
"karma-chrome-launcher": "^2.0.0",
69+
"karma-firefox-launcher": "^1.0.0",
70+
"karma-mocha": "^2.0.0",
71+
"karma-typescript": "^4.1.1",
72+
"karma-typescript-es6-transform": "^5.0.2",
6673
"lodash.zip": "^4.2.0",
6774
"mocha": "^7.1.2",
6875
"nyc": "^15.0.1",

test/index.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const fixturePublicKeyBuffer = Buffer.from(fixturePublicKey, 'hex')
2424
const fixtureWallet = Wallet.fromPrivateKey(fixturePrivateKeyBuffer)
2525
const fixtureEthersWallet = new ethersWallet(fixtureWallet.getPrivateKeyString())
2626

27+
const isRunningInKarma = () => {
28+
return typeof (global as any).window !== 'undefined' && (global as any).window.__karma__
29+
}
30+
2731
describe('.getPrivateKey()', function() {
2832
it('should work', function() {
2933
assert.strictEqual(fixtureWallet.getPrivateKey().toString('hex'), fixturePrivateKey)
@@ -212,7 +216,7 @@ describe('.toV3()', function() {
212216
const makePermutations = (...objs: Array<object>): Array<object> => {
213217
const permus = []
214218
const keys = Array.from(
215-
objs.reduce((acc: Set<string>, curr: object) => {
219+
objs.reduce((acc: any, curr: object) => {
216220
Object.keys(curr).forEach(key => {
217221
acc.add(key)
218222
})
@@ -246,7 +250,13 @@ describe('.toV3()', function() {
246250
return obj
247251
}
248252

249-
const permutations = makePermutations(strKdfOptions, buffKdfOptions)
253+
let permutations = makePermutations(strKdfOptions, buffKdfOptions)
254+
255+
if (isRunningInKarma()) {
256+
// These tests take a long time in the browser due to
257+
// the amount of permutations so we will shorten them.
258+
permutations = permutations.slice(1)
259+
}
250260

251261
it('should work with PBKDF2', async function() {
252262
this.timeout(0) // never
@@ -669,19 +679,22 @@ describe('.fromV3()', function() {
669679
it('should fail with invalid password', function() {
670680
const w =
671681
'{"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"6087dab2f9fdbbfaddc31a909735c1e6"},"ciphertext":"5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46","kdf":"pbkdf2","kdfparams":{"c":262144,"dklen":32,"prf":"hmac-sha256","salt":"ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"},"mac":"517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2"},"id":"3198bc9c-6672-5ab3-d995-4942343ae5b6","version":3}'
682+
this.timeout(0) // never
672683
assert.throws(function() {
673684
Wallet.fromV3(w, 'wrongtestpassword')
674685
}, /^Error: Key derivation failed - possibly wrong passphrase$/)
675686
})
676687
it('should work with (broken) mixed-case input files', function() {
677688
const w =
678689
'{"Crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"6087dab2f9fdbbfaddc31a909735c1e6"},"ciphertext":"5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46","kdf":"pbkdf2","kdfparams":{"c":262144,"dklen":32,"prf":"hmac-sha256","salt":"ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"},"mac":"517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2"},"id":"3198bc9c-6672-5ab3-d995-4942343ae5b6","version":3}'
690+
this.timeout(0) // never
679691
const wallet = Wallet.fromV3(w, 'testpassword', true)
680692
assert.strictEqual(wallet.getAddressString(), '0x008aeeda4d805471df9b2a5b0f38a0c3bcba786b')
681693
})
682694
it("shouldn't work with (broken) mixed-case input files in strict mode", function() {
683695
const w =
684696
'{"Crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"6087dab2f9fdbbfaddc31a909735c1e6"},"ciphertext":"5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46","kdf":"pbkdf2","kdfparams":{"c":262144,"dklen":32,"prf":"hmac-sha256","salt":"ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"},"mac":"517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2"},"id":"3198bc9c-6672-5ab3-d995-4942343ae5b6","version":3}'
697+
this.timeout(0) // never
685698
assert.throws(function() {
686699
Wallet.fromV3(w, 'testpassword')
687700
}) // FIXME: check for assert message(s)
@@ -760,6 +773,7 @@ describe('.fromKryptoKit()', function() {
760773
'qhah1VeT0RgTvff1UKrUrxtFViiQuki16dd353d59888c25',
761774
'testtest',
762775
)
776+
this.timeout(0) // never
763777
assert.strictEqual(wallet.getAddressString(), '0x3c753e27834db67329d1ec1fab67970ec1e27112')
764778
})
765779
})

0 commit comments

Comments
 (0)