2023-08-24 17:47:45 +08:00
|
|
|
import ws from 'ws';
|
|
|
|
|
2022-02-15 00:20:25 +08:00
|
|
|
export { mockDatabase } from '@nocobase/database';
|
2021-09-09 13:09:25 +08:00
|
|
|
export * from './mockServer';
|
2023-08-24 17:47:45 +08:00
|
|
|
export { default as supertest } from 'supertest';
|
2022-02-15 00:20:25 +08:00
|
|
|
|
2023-06-07 10:37:10 +08:00
|
|
|
export const pgOnly: () => jest.Describe = () => (process.env.DB_DIALECT == 'postgres' ? describe : describe.skip);
|
2023-02-23 20:14:50 +08:00
|
|
|
|
|
|
|
export function randomStr() {
|
|
|
|
// create random string
|
|
|
|
return Math.random().toString(36).substring(2);
|
|
|
|
}
|
2023-08-24 17:47:45 +08:00
|
|
|
|
|
|
|
export const waitSecond = async () => {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const startServerWithRandomPort = async (startServer) => {
|
|
|
|
return await new Promise((resolve) => {
|
|
|
|
startServer({
|
|
|
|
port: 0,
|
|
|
|
host: 'localhost',
|
|
|
|
callback(server) {
|
|
|
|
// @ts-ignore
|
|
|
|
const port = server.address().port;
|
|
|
|
resolve(port);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const createWsClient = async ({ serverPort, options = {} }) => {
|
|
|
|
console.log(`connect to ws://localhost:${serverPort}/ws`, options);
|
|
|
|
|
|
|
|
const wsc = new ws(`ws://localhost:${serverPort}/ws`, options);
|
|
|
|
const messages = [];
|
|
|
|
|
|
|
|
wsc.on('message', (data) => {
|
|
|
|
const message = data.toString();
|
|
|
|
messages.push(message);
|
|
|
|
});
|
|
|
|
|
|
|
|
// await connection established
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
wsc.on('open', resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
wsc,
|
|
|
|
messages,
|
|
|
|
async stop() {
|
|
|
|
const promise = new Promise((resolve) => {
|
|
|
|
wsc.on('close', resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
wsc.close();
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
lastMessage() {
|
|
|
|
return JSON.parse(messages[messages.length - 1]);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|