2020-12-19 08:45:19 +08:00
|
|
|
import path from 'path';
|
|
|
|
import supertest from 'supertest';
|
2021-12-03 07:31:22 +08:00
|
|
|
import { MockServer, mockServer } from '@nocobase/test';
|
2020-12-19 08:45:19 +08:00
|
|
|
|
2021-12-04 16:28:52 +08:00
|
|
|
import plugin from '../';
|
2021-03-28 13:34:51 +08:00
|
|
|
|
2021-12-03 07:31:22 +08:00
|
|
|
export async function getApp(options = {}): Promise<MockServer> {
|
|
|
|
const app = mockServer({
|
|
|
|
...options,
|
|
|
|
cors: {
|
2021-12-06 21:23:34 +08:00
|
|
|
origin: '*',
|
|
|
|
},
|
2020-12-19 08:45:19 +08:00
|
|
|
});
|
2021-12-04 07:58:31 +08:00
|
|
|
|
2021-12-03 07:31:22 +08:00
|
|
|
app.plugin(plugin);
|
2021-12-04 07:58:31 +08:00
|
|
|
|
2021-12-03 07:31:22 +08:00
|
|
|
await app.load();
|
2021-12-04 07:58:31 +08:00
|
|
|
|
2021-09-14 11:09:26 +08:00
|
|
|
app.db.import({
|
2021-12-06 21:23:34 +08:00
|
|
|
directory: path.resolve(__dirname, './tables'),
|
2020-12-19 08:45:19 +08:00
|
|
|
});
|
2020-12-23 12:46:13 +08:00
|
|
|
try {
|
2021-09-14 11:09:26 +08:00
|
|
|
await app.db.sync();
|
2020-12-23 12:46:13 +08:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
|
|
|
|
await app.emitAsync('beforeStart');
|
|
|
|
|
2020-12-19 08:45:19 +08:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2021-12-04 07:58:31 +08:00
|
|
|
// because the app in supertest will use a random port
|
2021-12-03 07:31:22 +08:00
|
|
|
export function requestFile(url, agent) {
|
2021-12-04 07:58:31 +08:00
|
|
|
// url starts with double slash "//" will be considered as http or https
|
|
|
|
// url starts with single slash "/" will be considered from local server
|
2021-12-06 21:23:34 +08:00
|
|
|
return url[0] === '/' && url[1] !== '/' ? agent.get(url) : supertest.agent(url).get('');
|
2020-12-19 08:45:19 +08:00
|
|
|
}
|