Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
* add tsconfig options to fix imports in tests
  (related to polkadot now using esm modules)
* fix tests according to sdk changes
  • Loading branch information
rflechtner committed Jun 24, 2021
1 parent 9ad9814 commit ea27dc0
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 92 deletions.
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "botlabs",
"license": "BSD-4-Clause",
"scripts": {
"build": "tsc && cp -r src/assets dist",
"build": "tsc -b ./tsconfig.build.json && cp -r src/assets dist",
"format": "prettier --write \"src/**/*.ts\"",
"start": "export NODE_ENV=dev-local; export SECRET=s3cr3t; export FAUCET_ACCOUNT=0xcdfd6024d2b0eba27d54cc92a44cd9a627c69b2dbda15ed7e58085425119ae03; ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "nodemon",
Expand Down Expand Up @@ -67,8 +67,14 @@
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
"preset": "ts-jest/presets/js-with-ts",
"transformIgnorePatterns": [
"/node_modules/(?!@polkadot|@babel/runtime/helpers/esm/)"
],
"globals": {
"ts-jest": {
"tsConfig": "./tsconfig.spec.json"
}
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
Expand Down
6 changes: 3 additions & 3 deletions src/contacts/contacts.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ describe('Contact Module', () => {
.mockReturnValue({ exec: async (): Promise<ContactDB[]> => [] })
public static findOne = jest
.fn()
.mockReturnValue({ exec: async (): Promise<ContactDB> => null })
.mockReturnValue({ exec: async () => null })
public static replaceOne = jest
.fn()
.mockReturnValue({ exec: async (): Promise<void> => {} })
.mockReturnValue({ exec: async (): Promise<void> => undefined })
public static deleteMany = jest.fn().mockReturnValue({
exec: async () => {
return
Expand Down Expand Up @@ -346,7 +346,7 @@ describe('Contact Module', () => {
it('queries database and converts match', async () => {
const findOneSpy = jest
.spyOn(contactsService['contactModel'], 'findOne')
.mockReturnValue({ exec: async (): Promise<ContactDB> => null })
.mockReturnValue({ exec: async () => null })
expect(await contactsService.findByAddress(address)).toEqual(
Optional.ofNullable<Contact>(null)
)
Expand Down
4 changes: 2 additions & 2 deletions src/ctypes/ctypes.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe('CType Module', () => {
.mockReturnValue({ exec: async () => [] as CTypeDB[] })
public static findOne = jest
.fn()
.mockReturnValue({ exec: async (): Promise<CTypeDB> => null })
.mockReturnValue({ exec: async () => null })
public static deleteMany = jest.fn().mockReturnValue({
exec: async (): Promise<void> => {
return
Expand Down Expand Up @@ -318,7 +318,7 @@ describe('CType Module', () => {
.spyOn(ctypesService['cTypeDBModel'], 'findOne')
.mockImplementation(() => {
return {
exec: async (): Promise<CTypeDB> => null,
exec: async () => null,
}
})

Expand Down
76 changes: 31 additions & 45 deletions src/messaging/messaging.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('Messaging Module', () => {
'layer donor village public cruel caution learn bronze fish come embrace hurt',
{ signingKeyPairType: 'ed25519' }
)
receiverSignature = receiverIdentity.signStr(encryptedMessage.messageId)
receiverSignature = receiverIdentity.signStr(encryptedMessage.messageId!)
})
describe('removeMessage', () => {
it('removes a message for an id from the service', async () => {
Expand Down Expand Up @@ -184,11 +184,11 @@ describe('Messaging Module', () => {
it('sets messageId, receival Date and calls messagingService.add for valid message', async () => {
const uuidv4Spy = jest
.spyOn(Controller, 'uuidv4')
.mockReturnValue(encryptedMessage.messageId)
.mockReturnValue(encryptedMessage.messageId!)

const nowSpy = jest
.spyOn(Date, 'now')
.mockReturnValue(encryptedMessage.receivedAt)
.mockReturnValue(encryptedMessage.receivedAt!)
const addSpy = jest
.spyOn(messagesService, 'add')
.mockResolvedValue(undefined)
Expand All @@ -208,49 +208,37 @@ describe('Messaging Module', () => {
})
})
it('throws BadRequestException on invalid message', async () => {
const noSender: IEncryptedMessage = {
...encryptedMessage,
senderAddress: null,
}
const noReceiver: IEncryptedMessage = {
...encryptedMessage,
receiverAddress: null,
}
const noNonce: IEncryptedMessage = { ...encryptedMessage, nonce: null }
const noMessage: IEncryptedMessage = {
...encryptedMessage,
ciphertext: null,
}
const noHash: IEncryptedMessage = { ...encryptedMessage, hash: null }
const noSignature: IEncryptedMessage = {
...encryptedMessage,
signature: null,
}
const { senderAddress, ...noSender } = encryptedMessage
const { receiverAddress, ...noReceiver } = encryptedMessage
const { nonce, ...noNonce } = encryptedMessage
const { ciphertext, ...noMessage } = encryptedMessage
const { hash, ...noHash } = encryptedMessage
const { signature, ...noSignature } = encryptedMessage

await expect(messagesController.sendMessage(noSender)).rejects.toThrow(
BadRequestException
)
await expect(messagesController.sendMessage(noReceiver)).rejects.toThrow(
BadRequestException
)
await expect(messagesController.sendMessage(noNonce)).rejects.toThrow(
BadRequestException
)
await expect(messagesController.sendMessage(noMessage)).rejects.toThrow(
BadRequestException
)
await expect(messagesController.sendMessage(noHash)).rejects.toThrow(
BadRequestException
)
await expect(messagesController.sendMessage(noSignature)).rejects.toThrow(
BadRequestException
)
await expect(
messagesController.sendMessage(noSender as IEncryptedMessage)
).rejects.toThrow(BadRequestException)
await expect(
messagesController.sendMessage(noReceiver as IEncryptedMessage)
).rejects.toThrow(BadRequestException)
await expect(
messagesController.sendMessage(noNonce as IEncryptedMessage)
).rejects.toThrow(BadRequestException)
await expect(
messagesController.sendMessage(noMessage as IEncryptedMessage)
).rejects.toThrow(BadRequestException)
await expect(
messagesController.sendMessage(noHash as IEncryptedMessage)
).rejects.toThrow(BadRequestException)
await expect(
messagesController.sendMessage(noSignature as IEncryptedMessage)
).rejects.toThrow(BadRequestException)
})
})

class MessageModel {
public static findOne = jest.fn().mockReturnValue({
exec: async (): Promise<MessageDB> => {
exec: async () => {
return null
},
})
Expand All @@ -263,9 +251,7 @@ describe('Messaging Module', () => {
},
})
public static deleteMany = jest.fn().mockReturnValue({
exec: (): Promise<void> => {
return
},
exec: async (): Promise<void> => undefined,
})
public static save = jest
.fn()
Expand Down Expand Up @@ -327,7 +313,7 @@ describe('Messaging Module', () => {
const findOneSpy = jest
.spyOn(messagingService['messageModel'], 'findOne')
.mockReturnValue({
exec: async (): Promise<MessageDB> => {
exec: async () => {
return null
},
})
Expand Down Expand Up @@ -437,7 +423,7 @@ describe('Messaging Module', () => {
return
},
})
await messagingService.remove(encryptedMessage.messageId)
await messagingService.remove(encryptedMessage.messageId!)
expect(deleteOneSpy).toHaveBeenCalledTimes(1)
expect(deleteOneSpy).toHaveBeenCalledWith({
messageId: encryptedMessage.messageId,
Expand Down
13 changes: 6 additions & 7 deletions test/contacts.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ describe('contacts endpoint (e2e)', () => {
})

it('rejects if address missing', async () => {
const corruptedContact: Contact = {
const corruptedContact = {
...contactA,
publicIdentity: { ...contactA.publicIdentity, address: undefined },
}
} as unknown as Contact
await request(app.getHttpServer())
.post(`/contacts`)
.send(corruptedContact)
Expand All @@ -165,13 +165,13 @@ describe('contacts endpoint (e2e)', () => {
})

it('rejects if boxPublicKeyAsHex missing', async () => {
const corruptedContact: Contact = {
const corruptedContact = {
...contactA,
publicIdentity: {
...contactA.publicIdentity,
boxPublicKeyAsHex: undefined,
},
}
} as unknown as Contact
await request(app.getHttpServer())
.post(`/contacts`)
.send(corruptedContact)
Expand All @@ -181,12 +181,11 @@ describe('contacts endpoint (e2e)', () => {
})

it('rejects if name missing', async () => {
const corruptedContact: Contact = {
const corruptedContact = {
...contactA,
metaData: {
name: undefined,
},
}
} as Contact
await request(app.getHttpServer())
.post(`/contacts`)
.send(corruptedContact)
Expand Down
10 changes: 8 additions & 2 deletions test/jest-e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
"preset": "ts-jest/presets/js-with-ts",
"transformIgnorePatterns": [
"/node_modules/(?!@polkadot|@babel/runtime/helpers/esm/)"
],
"globals": {
"ts-jest": {
"tsConfig": "./tsconfig.spec.json"
}
}
}
Loading

0 comments on commit ea27dc0

Please sign in to comment.