Skip to content

Instantly share code, notes, and snippets.

@gusflopes
Created April 30, 2020 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusflopes/f06f83a6faa5287bd3c3545e7a713db4 to your computer and use it in GitHub Desktop.
Save gusflopes/f06f83a6faa5287bd3c3545e7a713db4 to your computer and use it in GitHub Desktop.
Adonis V5 - Testing
// import Account from '../../app/Models/Account'
import test from 'japa'
import {join} from 'path'
import supertest from 'supertest'
import {createServer} from 'http'
import {Filesystem} from '@poppinss/dev-utils'
import 'reflect-metadata'
import { Ioc } from '@adonisjs/fold'
import { Application } from '@adonisjs/application/build/standalone'
import {HealthCheck} from '@adonisjs/core/build/src/HealthCheck'
import {Ignitor} from '@adonisjs/core/build/src/Ignitor'
import {setupApplicationFiles} from '../../test-helpers'
// import { Router } from '@adonisjs/http-server/build/src/Router'
const fs = new Filesystem(join(__dirname, '__app'))
// const ioc = new Ioc()
test.group('Health Check', (group) => {
group.before(() => {
process.env.ENV_SILENT = 'true'
})
group.beforeEach(() => {
process.env.NODE_ENV = 'testing'
})
group.after(async () => {
await fs.cleanup()
delete process.env.ENV_SILENT
delete process.env.APP_KEY
})
group.afterEach(async () => {
delete process.env.NODE_ENV
await fs.cleanup()
})
test('use application isReady state to find if application is ready', (assert) => {
const application = new Application(__dirname, new Ioc(), {}, {})
const healthCheck = new HealthCheck(application)
assert.isFalse(healthCheck.isReady())
application.isReady = true
assert.isTrue(healthCheck.isReady())
application.isShuttingDown = true
assert.isFalse(healthCheck.isReady())
})
test('start http server to accept incoming requests', async (assert, done) => {
await setupApplicationFiles(fs)
const ignitor = new Ignitor(fs.basePath)
const boostrapper = ignitor.boostrapper()
const httpServer = ignitor.httpServer()
const application = boostrapper.setup()
boostrapper.registerAliases()
boostrapper.registerProviders(false)
await boostrapper.bootProviders()
const server = application.container.use('Adonis/Core/Server')
application.container.use('Adonis/Core/Route').get('/', () => 'handled')
httpServer.injectBootstrapper(boostrapper)
await httpServer.start((handler) => createServer(handler))
assert.isTrue(httpServer.application.isReady)
const { text } = await supertest(server.instance).get('/').expect(200)
server.instance.close()
setTimeout(() => {
assert.isFalse(application.isReady)
assert.equal(text, 'handled')
done()
}, 100)
})
})
test.group('Account', () => {
test('should be able to add an account', async (assert) => {
// const account = await Account.all()
// console.log(account)
assert.deepEqual(2 + 2, 4)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment