Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
refactor: migrate to Node.js test runner
Browse files Browse the repository at this point in the history
Closes #592
  • Loading branch information
coderbyheart committed Oct 19, 2023
1 parent 534fdd6 commit 4d4968d
Show file tree
Hide file tree
Showing 15 changed files with 4,373 additions and 8,876 deletions.
4 changes: 3 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged && npm test && npx tsc
npx lint-staged
npm test
npx tsc
9 changes: 5 additions & 4 deletions agps/cacheKey.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { cacheKey } from './cacheKey.js'
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'

describe('cacheKey', () => {
it('should create a cache key', () =>
expect(
void describe('cacheKey', () => {
void it('should create a cache key', () =>
assert.equal(
cacheKey({
binHours: 1,
request: {
Expand All @@ -13,7 +15,6 @@ describe('cacheKey', () => {
types: [1, 2, 3, 4, 6, 7, 8, 9],
},
}),
).toEqual(
`242-1-21626624-30401-1_2_3_4_6_7_8_9-${new Date()
.toISOString()
.slice(0, 13)
Expand Down
37 changes: 21 additions & 16 deletions feature-runner/steps/device/encodePropertyBag.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { encodePropertyBag } from './encodePropertyBag.js'
import { describe, it, test } from 'node:test'
import assert from 'node:assert/strict'
void describe('encodePropertyBag', () => {
for (const bag of [undefined, {}]) {
void it(`should return an empty string for ${JSON.stringify(bag)}`, () =>
assert.equal(encodePropertyBag(bag as any), ''))
}

describe('encodePropertyBag', () => {
it.each([undefined, {}])('should return an empty string for %j', (bag) =>
expect(encodePropertyBag(bag as any)).toEqual(''),
)
it('should encode a single nulled property', () =>
expect(encodePropertyBag({ batch: null })).toEqual('batch'))
void it('should encode a single nulled property', () =>
assert.equal(encodePropertyBag({ batch: null }), 'batch'))

describe('it should encode properties', () => {
it.each([
void describe('it should encode properties', () => {
for (const [props, expected] of [
// Sample from https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#receiving-cloud-to-device-messages
// Note: "?" is not included.
[
Expand All @@ -26,12 +29,13 @@ describe('encodePropertyBag', () => {
},
'%24.ct=application%2Fjson&%24.ce=utf-8',
],
])('%j => %s', (props, expected) =>
expect(encodePropertyBag(props)).toEqual(expected),
)
]) {
void test(`${JSON.stringify(props)} => ${expected}`, () =>
assert.equal(encodePropertyBag(props as any), expected))
}
})
describe('it should sort $ properties to the end', () => {
it.each([
void describe('it should sort $ properties to the end', () => {
for (const [props, expected] of [
[
{
'$.ct': 'application/json',
Expand All @@ -49,8 +53,9 @@ describe('encodePropertyBag', () => {
},
'prop1&prop3=a%20string&%24.ct=application%2Fjson&%24.ce=utf-8',
],
])('%j => %s', (props, expected) =>
expect(encodePropertyBag(props)).toEqual(expected),
)
]) {
void test(`${JSON.stringify(props)} => ${expected}`, () =>
assert.equal(encodePropertyBag(props as any), expected))
}
})
})
32 changes: 19 additions & 13 deletions feature-runner/steps/device/matchDeviceBoundTopic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
import { matchDeviceBoundTopic } from './matchDeviceBoundTopic.js'
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'

describe('matchTopic', () => {
it('should match a simple topic', () =>
expect(
void describe('matchTopic', () => {
void it('should match a simple topic', () =>
assert.equal(
matchDeviceBoundTopic(
'devices/49dd7d86-e547-4a4d-8f0f-f4b09591838c/messages/devicebound',
'devices/49dd7d86-e547-4a4d-8f0f-f4b09591838c/messages/devicebound',
),
).toEqual(true))
it('should match topic with a property bag', () =>
expect(
true,
))
void it('should match topic with a property bag', () =>
assert.equal(
matchDeviceBoundTopic(
'devices/49dd7d86-e547-4a4d-8f0f-f4b09591838c/messages/devicebound/pgps=result',
'devices/49dd7d86-e547-4a4d-8f0f-f4b09591838c/messages/devicebound/%24.to=%2Fdevices%2F49dd7d86-e547-4a4d-8f0f-f4b09591838c%2Fmessages%2Fdevicebound&pgps=result&%24.ct=application%2Fjson&%24.ce=utf-8',
),
).toEqual(true))
it('should not match topic with a property bag thats not contained', () =>
expect(
true,
))
void it('should not match topic with a property bag thats not contained', () =>
assert.equal(
matchDeviceBoundTopic(
'devices/49dd7d86-e547-4a4d-8f0f-f4b09591838c/messages/devicebound/agps=result',
'devices/49dd7d86-e547-4a4d-8f0f-f4b09591838c/messages/devicebound/%24.to=%2Fdevices%2F49dd7d86-e547-4a4d-8f0f-f4b09591838c%2Fmessages%2Fdevicebound&pgps=result&%24.ct=application%2Fjson&%24.ce=utf-8',
),
).toEqual(false))
it('should match a topic regardless of property bag', () =>
expect(
false,
))
void it('should match a topic regardless of property bag', () =>
assert.equal(
matchDeviceBoundTopic(
'devices/49dd7d86-e547-4a4d-8f0f-f4b09591838c/messages/devicebound',
'devices/49dd7d86-e547-4a4d-8f0f-f4b09591838c/messages/devicebound/%24.to=%2Fdevices%2F49dd7d86-e547-4a4d-8f0f-f4b09591838c%2Fmessages%2Fdevicebound&pgps=result&%24.ct=application%2Fjson&%24.ce=utf-8',
),
).toEqual(true))
true,
))
})
57 changes: 30 additions & 27 deletions lib/batchToDoc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { batchToDoc } from './batchToDoc.js'
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'

describe('batchToDoc', () => {
it('should convert a batch document to multiple updates', () => {
expect(
void describe('batchToDoc', () => {
void it('should convert a batch document to multiple updates', () => {
assert.deepEqual(
batchToDoc({
gnss: [
{
Expand All @@ -29,33 +31,34 @@ describe('batchToDoc', () => {
},
],
}),
).toEqual([
{
gnss: {
v: {
lng: 8.669555,
lat: 50.109177,
acc: 28.032738,
alt: 204.623276,
spd: 0.698944,
hdg: 0,
[
{
gnss: {
v: {
lng: 8.669555,
lat: 50.109177,
acc: 28.032738,
alt: 204.623276,
spd: 0.698944,
hdg: 0,
},
ts: 1567094051000,
},
ts: 1567094051000,
},
},
{
gnss: {
v: {
lng: 10.424793,
lat: 63.422975,
acc: 12.276645,
alt: 137.319351,
spd: 6.308265,
hdg: 77.472923,
{
gnss: {
v: {
lng: 10.424793,
lat: 63.422975,
acc: 12.276645,
alt: 137.319351,
spd: 6.308265,
hdg: 77.472923,
},
ts: 1567165503000,
},
ts: 1567165503000,
},
},
])
],
)
})
})
11 changes: 7 additions & 4 deletions lib/lowerCaseRecord.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { lowerCaseRecord } from './lowerCaseRecord.js'
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'

describe('lowerCaseRecord', () => {
it('should lower-case all keys', () =>
expect(
void describe('lowerCaseRecord', () => {
void it('should lower-case all keys', () =>
assert.deepEqual(
lowerCaseRecord({
Foo: 'Bar', // will be overwritten by the next key
foo: 'bar',
}),
).toMatchObject({ foo: 'bar' }))
{ foo: 'bar' },
))
})
18 changes: 10 additions & 8 deletions lib/parseConnectionString.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { parseConnectionString } from './parseConnectionString.js'

describe('parseConnectionString', () => {
it('should parse a connection string', () => {
expect(
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
void describe('parseConnectionString', () => {
void it('should parse a connection string', () => {
assert.deepEqual(
parseConnectionString(
'AccountEndpoint=https://xxxx.documents.azure.com:443/;AccountKey=oKHTAxxx92GKkq3CDzeCd1WYnVslfIUaQqOa7Xw==;',
),
).toEqual({
AccountEndpoint: 'https://xxxx.documents.azure.com:443/',
AccountKey: 'oKHTAxxx92GKkq3CDzeCd1WYnVslfIUaQqOa7Xw==',
})
{
AccountEndpoint: 'https://xxxx.documents.azure.com:443/',
AccountKey: 'oKHTAxxx92GKkq3CDzeCd1WYnVslfIUaQqOa7Xw==',
},
)
})
})
10 changes: 5 additions & 5 deletions mock-http-api/sortQueryString.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { sortQueryString } from './sortQueryString.js'

describe('sortQueryString', () => {
it('should sort the query part of a mock URL', () =>
expect(
import { describe, it } from 'node:test'
import assert from 'node:assert'
void describe('sortQueryString', () => {
void it('should sort the query part of a mock URL', () =>
assert.equal(
sortQueryString(
'api.nrfcloud.com/v1/location/agps?eci=73393515&tac=132&requestType=custom&mcc=397&mnc=73&customTypes=2',
),
).toEqual(
'api.nrfcloud.com/v1/location/agps?customTypes=2&eci=73393515&mcc=397&mnc=73&requestType=custom&tac=132',
))
})
20 changes: 11 additions & 9 deletions mock-http-api/splitMockResponse.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { splitMockResponse } from './splitMockResponse.js'

describe('split mock response', () => {
it('should parse headers and body', () =>
expect(
import { describe, it } from 'node:test'
import assert from 'node:assert'
void describe('split mock response', () => {
void it('should parse headers and body', () =>
assert.deepEqual(
splitMockResponse(`Content-Type: application/octet-stream
(binary A-GNSS data) other types`),
).toMatchObject({
headers: {
'Content-Type': 'application/octet-stream',
{
headers: {
'Content-Type': 'application/octet-stream',
},
body: '(binary A-GNSS data) other types',
},
body: '(binary A-GNSS data) other types',
}))
))
})
24 changes: 13 additions & 11 deletions pack/flattenDependencies.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { flattenDependencies } from './flattenDependencies.js'

describe('flattenDependencies', () => {
it('should flatten dependencies', () =>
expect(
import { describe, it } from 'node:test'
import assert from 'node:assert'
void describe('flattenDependencies', () => {
void it('should flatten dependencies', () =>
assert.deepEqual(
flattenDependencies({
'/mock-http-api/mock-http-api.js': {
'/lib/log.js': {},
Expand All @@ -13,11 +14,12 @@ describe('flattenDependencies', () => {
'/lib/fromEnv.js': {},
},
}),
).toEqual([
'/lib/fromEnv.js',
'/lib/http.js',
'/lib/log.js',
'/lib/request.js',
'/mock-http-api/mock-http-api.js',
]))
[
'/lib/fromEnv.js',
'/lib/http.js',
'/lib/log.js',
'/lib/request.js',
'/mock-http-api/mock-http-api.js',
],
))
})
Loading

0 comments on commit 4d4968d

Please sign in to comment.