Last active
February 23, 2021 10:52
-
-
Save Lxxyx/d793eb3d89129209b64823e83d9824ad to your computer and use it in GitHub Desktop.
hooks test demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { get, post } from '../demo' | |
import { | |
createApp, | |
HooksApplication | |
} from '@midwayjs/mock' | |
// 纯函数 | |
test('pure function', () => { | |
const result = pureFunction() | |
expect(result).toEqual('pure') | |
}) | |
// 使用了 Hooks | |
test('hooks with defaultContext', async () => { | |
const app = await createApp<HooksApplication>() | |
const service = await app.runHooks(async () => useInject('service')) | |
expect(service.isTest).toEqual(true) | |
}) | |
// Hooks + 自定义 Context | |
test('only hooks', async () => { | |
const app = await createApp<HooksApplication>() | |
const ctx = app.mockContext({ | |
url: '/custom_context' | |
}) | |
const ctx = await app.runHooks(ctx, () => useContext()) | |
expect(ctx.url).toEqual('/custom_context') | |
}) | |
// 使用了 HTTP | |
test('with simple http', async () => { | |
const app = await createApp<HooksApplication>() | |
// 每次运行函数时,为这个函数创建一个全新的 Context 来实现隔离? | |
const result = await app.runFunction(() => get()) | |
expect(result).toEqual('get') | |
const postResult = await app.runFunction(() => post('lxxyx')) | |
expect(postResult).toEqual('lxxyx') | |
}) | |
// Supertest | |
test('with full feature http', async () => { | |
const app = await createApp<HooksApplication>() | |
await app.request(get) | |
.expect(200) | |
.expect('Content-Type', /json/) | |
.expect('Content-Length', '15') | |
.end((err, res) => { | |
expect(res.body).toStrictEqual('get') | |
}) | |
}) | |
// Supertest + 自定义 Context | |
test('with full feature http & custom context', async () => { | |
const app = await createApp<HooksApplication>() | |
const ctx = app.mockContext() | |
await app.request(ctx, post) | |
.args('lxxyx') | |
.end((err, res) => { | |
expect(res.body).toStrictEqual('lxxyx') | |
}) | |
}) |
orm({
}),
swagger({
}),
sdk,
hooks({
})
- 1、runHooks
- 2、runFunction
- 3、createHooksRequest().load().run()
最终:
测试 Hooks
app.runHooks(hooks)
app.runHooks(ctx, hooks)
测试接口
app.runFunction(function)
app.runFunction(ctx, function)
Supertest
Get
app
.request(function)
Post
app
.request(ctx, function)
.args()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.