'How to use mocks in tests with Adonis 5 (adonisjs/fold package)?
I have an API built with Adonis 5 (core version 5.4.0). The tests are made with Adonis' own runner, japa (version 3.1.1).
As per the Adonis documentation, I build my tests to interact with a real test database without mocks. However, we implemented a functionality which calls a third party API using HTTP requests and need to write tests for that. Naturally, the tests should not actually call this third party API and must be mocked.
I'm having difficulties setting mocks in the tests as japa does not seem to have a way to do this and there's no mention of it in the documentation. The best I could come up with was seeing that Adonis 4 uses an adonisjs/fold package to build mocks and tried doing the same, but to no results.
Here's an example of what I tried with adonis/fold
version 8.1.9
, but using a login method for simplification:
// in App/Services/AuthService
export default class AuthService {
public static async userLogin(email: string, password: string) {
// Authentication logic
return {
token: 'realGeneratedToken'
}
}
}
// In AuthService.spec.ts
test('It should login user using mock', async (assert) => {
const ioc = new Ioc()
ioc.useProxies(true)
ioc.fake('App/Services/AuthService', () => {
return {
async userLogin(email: string, password: string) {
return {
token: 'mocked token',
}
},
}
})
const { status, body } = await supertest(BASE_URL).post(LOGIN_URL).send({
email: '[email protected]',
password: 'any_password',
})
assert.equal(status, 200)
assert.equal(body.token, 'mocked token')
ioc.restore('App/Services/AuthService')
})
This does not work. The real method is called, but there are no errors by using fold in the test, either.
Does anyone know how to set up mocks in Adonis 5 tests? Is using adonisjs/fold
the correct approach?
Solution 1:[1]
I figured out a way to mock Adonis providers for a running server, and I see you are attempting to hit a running server. In this example, I mock Redis.
I'm using Jest and Open API but I think some of this will be the same.
import 'reflect-metadata'
import path, { join } from 'path'
import getPort from 'get-port'
import sourceMapSupport from 'source-map-support'
import jestOpenAPI from 'jest-openapi'
import supertest from 'supertest'
process.env.NODE_ENV = 'testing'
process.env.ADONIS_ACE_CWD = join(__dirname)
sourceMapSupport.install({ handleUncaughtExceptions: false })
jestOpenAPI(path.resolve('./openapi/spec.yaml'))
let BASE_URL
let SERVER
async function startHttpServer() {
const { Ignitor } = await import('@adonisjs/core/build/src/Ignitor')
let port = String(await getPort())
process.env.PORT = port
BASE_URL = `http://localhost:${port}`
SERVER = new Ignitor(`${__dirname}/../`).httpServer()
// Mock Redis
const app = SERVER.kernel.application
app.container.useProxies(true)
app.container.fake('Adonis/Addons/Redis', () => ({
hset: () => {
return Promise.resolve(1)
},
hgetall: () => {
return Promise.resolve({
content: 'hello world',
})
},
quitAll: () => {},
}))
await SERVER.start()
}
async function closeHttpServer() {
await SERVER.close()
}
describe('Open API Spec tests', () => {
beforeAll(() => startHttpServer())
afterAll(() => closeHttpServer())
it('satisfies GET /ping', async () => {
const response = await supertest(BASE_URL).get('/ping').expect(200)
expect(response).toSatisfyApiSpec()
})
test('satisfies GET /PATH_HERE', async () => {
const response = await supertest(BASE_URL).get('/PATH_HERE').expect(200)
expect(response).toSatisfyApiSpec()
})
test('satisfies POST /PATH_HERE', async () => {
await supertest(BASE_URL)
.post('/PATH_HERE')
.send({
field1: 'value1',
field2: 'value2'
})
.expect(202)
expect(response).toSatisfyApiSpec()
})
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |