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-03 07:31:22 +08:00
|
|
|
import plugin from '../server';
|
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: {
|
|
|
|
origin: '*'
|
2020-12-19 08:45:19 +08:00
|
|
|
}
|
|
|
|
});
|
2021-12-03 07:31:22 +08:00
|
|
|
app.plugin(require('@nocobase/plugin-collections/src/server').default);
|
|
|
|
app.plugin(plugin);
|
|
|
|
await app.load();
|
2021-09-14 11:09:26 +08:00
|
|
|
app.db.import({
|
2020-12-23 12:46:13 +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-03 07:31:22 +08:00
|
|
|
|
2020-12-19 08:45:19 +08:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2021-12-03 07:31:22 +08:00
|
|
|
// because the app in supertest is using a random port
|
|
|
|
export function requestFile(url, agent) {
|
|
|
|
return path.isAbsolute(url)
|
|
|
|
? agent.get(url)
|
|
|
|
: supertest.agent(url).get('');
|
2020-12-19 08:45:19 +08:00
|
|
|
}
|