Skip to content

Commit

Permalink
test: run address-codec tests in the browser (#2466)
Browse files Browse the repository at this point in the history
Update tests to use jasmine compatible functions.

This means changing `test` to `it`, `toStrictEqual` to `toEqual` (which
is still strict), `toThrowError` to `toError`, and updating the param
for toError to pass an `Error` object.

Remove the need to specify --single-run.
  • Loading branch information
ckniffen committed Oct 25, 2023
1 parent 4da9426 commit 38bafba
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 235 deletions.
28 changes: 28 additions & 0 deletions karma.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = function (config) {
config.set({
plugins: ["karma-webpack", "karma-jasmine", "karma-chrome-launcher"],

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ["jasmine"],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
// Use webpack to bundle our test files
"test/**/*.test.ts": ["webpack"],
},

browsers: ["ChromeHeadless"],
// runs only one browser at a time
concurrency: 1,
// CI mode
singleRun: true,
client: {
jasmine: {
// ensures that tests are run in order instead of a random order
random: false,
},
},
});
};
3 changes: 3 additions & 0 deletions packages/ripple-address-codec/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* Remove Node 14 support
* Remove `assert` dependency. If you were catching `AssertionError` you need to change to `Error`.

### Changes
* Execute test in a browser in addition to node

## 4.3.1 (2023-09-27)
### Fixed
* Fix source-maps not finding their designated source
Expand Down
15 changes: 15 additions & 0 deletions packages/ripple-address-codec/karma.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const baseKarmaConfig = require('../../karma.config')
const webpackConfig = require('./test/webpack.config')
delete webpackConfig.entry

module.exports = function (config) {
baseKarmaConfig(config)

config.set({
base: '',
webpack: webpackConfig,

// list of files / patterns to load in the browser
files: ['test/**/*.test.ts'],
})
}
1 change: 1 addition & 0 deletions packages/ripple-address-codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"scripts": {
"build": "tsc --build tsconfig.build.json",
"test": "jest --verbose false --silent=false ./test/*.test.ts",
"test:browser": "npm run build && karma start ./karma.config.js",
"lint": "eslint . --ext .ts",
"clean": "rm -rf ./dist ./coverage ./test/testCompiledForWeb tsconfig.build.tsbuildinfo"
},
Expand Down
32 changes: 16 additions & 16 deletions packages/ripple-address-codec/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const testCases: AddressTestCase[] = [
const classicAddress = testCase[0]
const tag = testCase[1] === false ? false : testCase[1]
const xAddress = isTestAddress ? testCase[3] : testCase[2]
test(`Converts ${classicAddress}${
it(`Converts ${classicAddress}${
tag ? `:${tag}` : ''
} to ${xAddress}${network}`, () => {
expect(classicAddressToXAddress(classicAddress, tag, isTestAddress)).toBe(
Expand All @@ -179,19 +179,19 @@ const testCases: AddressTestCase[] = [
const classicAddress = 'rGWrZyQqhTp9Xu7G5Pkayo7bXjH4k4QYpf'
const tag = MAX_32_BIT_UNSIGNED_INT + 1

test(`Converting ${classicAddress}:${tag}${network} throws`, () => {
it(`Converting ${classicAddress}:${tag}${network} throws`, () => {
expect(() => {
classicAddressToXAddress(classicAddress, tag, isTestAddress)
}).toThrowError(new Error('Invalid tag'))
}).toThrow(new Error('Invalid tag'))
})
}

{
const classicAddress = 'r'
test(`Invalid classic address: Converting ${classicAddress}${network} throws`, () => {
it(`Invalid classic address: Converting ${classicAddress}${network} throws`, () => {
expect(() => {
classicAddressToXAddress(classicAddress, false, isTestAddress)
}).toThrowError(
}).toThrow(
new Error('invalid_input_size: decoded data must have length >= 5'),
)
})
Expand All @@ -215,7 +215,7 @@ const testCases: AddressTestCase[] = [
tagTestCases.forEach((testCase) => {
const tag = testCase || false
const xAddress = encodeXAddress(accountId, tag, isTestAddress)
test(`Encoding ${accountId.toString('hex')}${
it(`Encoding ${accountId.toString('hex')}${
tag ? `:${tag}` : ''
} to ${xAddress} has expected length`, () => {
expect(xAddress.length).toBe(47)
Expand All @@ -227,46 +227,46 @@ const testCases: AddressTestCase[] = [

{
const xAddress = 'XVLhHMPHU98es4dbozjVtdWzVrDjtV5fdx1mHp98tDMoQXa'
test(`Invalid X-address (bad checksum): Converting ${xAddress} throws`, () => {
it(`Invalid X-address (bad checksum): Converting ${xAddress} throws`, () => {
expect(() => {
xAddressToClassicAddress(xAddress)
}).toThrowError(new Error('checksum_invalid'))
}).toThrow(new Error('checksum_invalid'))
})
}

{
const xAddress = 'dGzKGt8CVpWoa8aWL1k18tAdy9Won3PxynvbbpkAqp3V47g'
test(`Invalid X-address (bad prefix): Converting ${xAddress} throws`, () => {
it(`Invalid X-address (bad prefix): Converting ${xAddress} throws`, () => {
expect(() => {
xAddressToClassicAddress(xAddress)
}).toThrowError(new Error('Invalid X-address: bad prefix'))
}).toThrow(new Error('Invalid X-address: bad prefix'))
})
}

test(`Invalid X-address (64-bit tag) throws`, () => {
it(`Invalid X-address (64-bit tag) throws`, () => {
expect(() => {
// Encoded from:
// {
// classicAddress: 'rGWrZyQqhTp9Xu7G5Pkayo7bXjH4k4QYpf',
// tag: MAX_32_BIT_UNSIGNED_INT + 1
// }
xAddressToClassicAddress('XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8zeUygYrCgrPh')
}).toThrowError('Unsupported X-address')
}).toThrow(new Error('Unsupported X-address'))
})

test(`Invalid Account ID throws`, () => {
it(`Invalid Account ID throws`, () => {
expect(() => {
encodeXAddress(Buffer.from('00'.repeat(19), 'hex'), false, false)
}).toThrowError('Account ID must be 20 bytes')
}).toThrow(new Error('Account ID must be 20 bytes'))
})

test(`isValidXAddress returns false for invalid X-address`, () => {
it(`isValidXAddress returns false for invalid X-address`, () => {
expect(
isValidXAddress('XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8zeUygYrCgrPh'),
).toBe(false)
})

test(`Converts X7AcgcsBL6XDcUb... to r9cZA1mLK5R5A... and tag: false`, () => {
it(`Converts X7AcgcsBL6XDcUb... to r9cZA1mLK5R5A... and tag: false`, () => {
const classicAddress = 'r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59'
// eslint-disable-next-line @typescript-eslint/naming-convention -- tag can be false or a number
const tag = false
Expand Down
26 changes: 13 additions & 13 deletions packages/ripple-address-codec/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import { seqEqual, concatArgs } from '../src/utils'

test('two sequences are equal', () => {
it('two sequences are equal', () => {
expect(seqEqual([1, 2, 3], [1, 2, 3])).toBe(true)
})

test('elements must be in the same order', () => {
it('elements must be in the same order', () => {
expect(seqEqual([3, 2, 1], [1, 2, 3])).toBe(false)
})

test('sequences do not need to be the same type', () => {
it('sequences do not need to be the same type', () => {
expect(seqEqual(Buffer.from([1, 2, 3]), [1, 2, 3])).toBe(true)
expect(seqEqual(Buffer.from([1, 2, 3]), new Uint8Array([1, 2, 3]))).toBe(true)
})

test('sequences with a single element', () => {
it('sequences with a single element', () => {
expect(seqEqual(Buffer.from([1]), [1])).toBe(true)
expect(seqEqual(Buffer.from([1]), new Uint8Array([1]))).toBe(true)
})

test('empty sequences', () => {
it('empty sequences', () => {
expect(seqEqual(Buffer.from([]), [])).toBe(true)
expect(seqEqual(Buffer.from([]), new Uint8Array([]))).toBe(true)
})

test('plain numbers are concatenated', () => {
expect(concatArgs(10, 20, 30, 40)).toStrictEqual([10, 20, 30, 40])
it('plain numbers are concatenated', () => {
expect(concatArgs(10, 20, 30, 40)).toEqual([10, 20, 30, 40])
})

test('a variety of values are concatenated', () => {
it('a variety of values are concatenated', () => {
expect(
concatArgs(1, [2, 3], Buffer.from([4, 5]), new Uint8Array([6, 7])),
).toStrictEqual([1, 2, 3, 4, 5, 6, 7])
).toEqual([1, 2, 3, 4, 5, 6, 7])
})

test('a single value is returned as an array', () => {
expect(concatArgs(Buffer.from([7]))).toStrictEqual([7])
it('a single value is returned as an array', () => {
expect(concatArgs(Buffer.from([7]))).toEqual([7])
})

test('no arguments returns an empty array', () => {
expect(concatArgs()).toStrictEqual([])
it('no arguments returns an empty array', () => {
expect(concatArgs()).toEqual([])
})
9 changes: 9 additions & 0 deletions packages/ripple-address-codec/test/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'
const { merge } = require('webpack-merge')
const { webpackForTest } = require('../../../weback.test.config')
const { getDefaultConfiguration } = require('../../../webpack.config')

module.exports = merge(
getDefaultConfiguration(),
webpackForTest('./test/index.ts', __dirname),
)
Loading

0 comments on commit 38bafba

Please sign in to comment.