forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-utils.spec.ts
More file actions
151 lines (120 loc) · 3.86 KB
/
test-utils.spec.ts
File metadata and controls
151 lines (120 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* @adonisjs/core
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { test } from '@japa/runner'
import { join } from 'path'
import supertest from 'supertest'
import { createServer } from 'http'
import { stdout } from 'test-console'
import { Ignitor } from '../src/Ignitor'
import { TestUtils } from '../src/TestUtils'
import { setupApplicationFiles, fs } from '../test-helpers'
test.group('Test utils', (group) => {
group.each.teardown(async () => {
await fs.cleanup()
})
test('start http server using test utils', async ({ assert }) => {
await setupApplicationFiles()
await fs.add(
'config/cors.ts',
`
export const enabled = true
export const exposeHeaders = []
`
)
const kernel = new Ignitor(fs.basePath).kernel('test')
await kernel.boot()
const server = kernel.application.container.use('Adonis/Core/Server')
kernel.application.container.use('Adonis/Core/Route').get('/', async () => 'handled')
const testUtils = new TestUtils(kernel.application)
const cleanup = await testUtils.httpServer().start((handler) => createServer(handler))
const { text } = await supertest(server.instance).get('/').expect(200)
await cleanup()
assert.equal(text, 'handled')
})
test('load ace commands using test utils', async ({ assert }) => {
await setupApplicationFiles()
/**
* Overwriting .adonisrc.json
*/
await fs.add(
'.adonisrc.json',
JSON.stringify({
typescript: false,
commands: ['./FooCommand'],
providers: [join(__dirname, '../providers/AppProvider.ts')],
})
)
await fs.add(
'FooCommand.ts',
`
const { BaseCommand } = require('@adonisjs/ace')
export default class FooCommand extends BaseCommand {
static get commandName () {
return 'foo'
}
static get settings () {
return {
loadApp: true,
stayAlive: true
}
}
run () {
console.log(\`is ready \${this.application.isReady}\`)
}
}
`
)
const ignitor = new Ignitor(fs.basePath)
await ignitor.ace().handle(['generate:manifest'])
const kernel = ignitor.kernel('test')
await kernel.boot()
const testUtils = new TestUtils(kernel.application)
const ace = kernel.application.container.resolveBinding('Adonis/Core/Ace')
assert.isNull(await ace.find(['foo']))
await testUtils.ace().loadCommands()
assert.isNotNull(await ace.find(['foo']))
const { output, restore } = stdout.inspect()
await ignitor.ace().handle(['foo'])
restore()
assert.equal(output[0].trim(), 'is ready true')
})
test('add macros to test utils class', async ({ assert }) => {
await setupApplicationFiles()
await fs.add(
'config/cors.ts',
`
export const enabled = true
export const exposeHeaders = []
`
)
const kernel = new Ignitor(fs.basePath).kernel('test')
await kernel.boot()
const testUtils = kernel.application.container.resolveBinding('Adonis/Core/TestUtils')
testUtils.constructor.macro('foo', () => 'bar')
assert.equal(testUtils['foo'](), 'bar')
})
test('add getters to test utils class', async ({ assert }) => {
await setupApplicationFiles()
await fs.add(
'config/cors.ts',
`
export const enabled = true
export const exposeHeaders = []
`
)
const kernel = new Ignitor(fs.basePath).kernel('test')
await kernel.boot()
const testUtils = kernel.application.container.resolveBinding('Adonis/Core/TestUtils')
testUtils.constructor.macro('foo', () => 'bar')
testUtils.constructor.getter('bar', function () {
return this['foo']()
})
assert.equal(testUtils['bar'], 'bar')
})
})