Skip to content

Commit

Permalink
Lint tests
Browse files Browse the repository at this point in the history
Disallows focused tests to avoid accidentally committing
a test that disables all others in the same file.
  • Loading branch information
Nevon committed Feb 7, 2022
1 parent d0288b3 commit 50cb9e7
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 16 deletions.
16 changes: 13 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
],
"plugins": [
"prettier",
"standard"
"standard",
"jest"
],
"parserOptions": {
"ecmaVersion": 2020
Expand Down Expand Up @@ -34,9 +35,18 @@
"^testIfKafka_*"
]
}
]
],
"jest/no-focused-tests": "error",
"jest/no-commented-out-tests": "error",
"jest/no-deprecated-functions": "error",
"jest/no-jasmine-globals": "error",
"jest/no-jest-import": "error",
"jest/no-test-prefixes": "error",
"jest/valid-describe-callback": "error",
"jest/valid-expect": "error",
"jest/valid-expect-in-promise": "error"
},
"env": {
"jest": true,
"jest": true
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"eslint-config-prettier": "^6.0.0",
"eslint-config-standard": "^13.0.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jest": "^26.1.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-promise": "^4.2.1",
Expand Down
12 changes: 7 additions & 5 deletions src/consumer/__tests__/commitOffsets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,35 @@ describe('Consumer', () => {

describe('when commitOffsets', () => {
it('throws an error if any of the topics is invalid', async () => {
expect(consumer.commitOffsets([{ topic: null }])).rejects.toThrow(
await expect(consumer.commitOffsets([{ topic: null }])).rejects.toThrow(
KafkaJSNonRetriableError,
'Invalid topic null'
)
})

it('throws an error if any of the partitions is not a number', async () => {
expect(consumer.commitOffsets([{ topic: topicName, partition: 'ABC' }])).rejects.toThrow(
await expect(
consumer.commitOffsets([{ topic: topicName, partition: 'ABC' }])
).rejects.toThrow(
KafkaJSNonRetriableError,
'Invalid partition, expected a number received ABC'
)
})

it('throws an error if any of the offsets is not a number', async () => {
expect(
await expect(
consumer.commitOffsets([{ topic: topicName, partition: 0, offset: 'ABC' }])
).rejects.toThrow(KafkaJSNonRetriableError, 'Invalid offset, expected a long received ABC')
})

it('throws an error if any of the offsets is not an absolute offset', async () => {
expect(
await expect(
consumer.commitOffsets([{ topic: topicName, partition: 0, offset: '-1' }])
).rejects.toThrow(KafkaJSNonRetriableError, 'Offset must not be a negative number')
})

it('throws an error if called before consumer run', async () => {
expect(
await expect(
consumer.commitOffsets([{ topic: topicName, partition: 0, offset: '1' }])
).rejects.toThrow(
KafkaJSNonRetriableError,
Expand Down
8 changes: 4 additions & 4 deletions src/consumer/__tests__/runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ describe('Consumer > Runner', () => {
throw error
})

expect(runner.commitOffsets(offsets)).rejects.toThrow(error.message)
await expect(runner.commitOffsets(offsets)).rejects.toThrow(error.message)
expect(consumerGroup.joinAndSync).toHaveBeenCalledTimes(0)

await sleep(100)
Expand Down Expand Up @@ -336,14 +336,14 @@ describe('Consumer > Runner', () => {

it('a triggered rejoin failing should cause a crash', async () => {
const unknownError = new KafkaJSProtocolError(createErrorFromCode(UNKNOWN))
consumerGroup.joinAndSync.mockImplementationOnce(() => {
consumerGroup.joinAndSync.mockImplementationOnce(async () => {
throw unknownError
})
consumerGroup.commitOffsets.mockImplementationOnce(() => {
consumerGroup.commitOffsets.mockImplementationOnce(async () => {
throw rebalancingError()
})

expect(runner.commitOffsets(offsets)).rejects.toThrow(rebalancingError().message)
await expect(runner.commitOffsets(offsets)).rejects.toThrow(rebalancingError().message)

await sleep(100)

Expand Down
2 changes: 1 addition & 1 deletion src/consumer/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = class Batch {
}

/**
* If the batch contained raw messages (i.e was not truely empty) but all messages were filtered out due to
* If the batch contained raw messages (i.e was not truly empty) but all messages were filtered out due to
* log compaction, control records or other reasons
*/
isEmptyDueToFiltering() {
Expand Down
2 changes: 1 addition & 1 deletion src/network/requestQueue/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('Network > RequestQueue', () => {
}
})

expect(requestQueue.canSendSocketRequestImmediately())
expect(requestQueue.canSendSocketRequestImmediately()).toBe(true)

const before = Date.now()
const throttledUntilBefore = requestQueue.throttledUntil
Expand Down
2 changes: 1 addition & 1 deletion src/utils/waitFor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Utils > waitFor', () => {
conditionValid = true
}, 6)

await expect(waitFor(() => conditionValid, { delay: 5 }))
await expect(waitFor(() => conditionValid, { delay: 5 })).resolves.toBe(true)
})

it('rejects the promise if the callback fail', async () => {
Expand Down
3 changes: 3 additions & 0 deletions testHelpers/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable jest/valid-describe-callback */
// Disabled to allow for higher order test functions where
// `callback` is parameterized instead of a static function
const fs = require('fs')
const execa = require('execa')
const uuid = require('uuid/v4')
Expand Down
Loading

0 comments on commit 50cb9e7

Please sign in to comment.