mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
test: speed up api tests (#53969)
This commit is contained in:
committed by
GitHub
parent
6f3496a2a5
commit
fcf6bfae6a
+1
-1
@@ -4,7 +4,7 @@ const config: Config = {
|
|||||||
verbose: true,
|
verbose: true,
|
||||||
testRegex: '\\.test\\.ts$',
|
testRegex: '\\.test\\.ts$',
|
||||||
transform: {
|
transform: {
|
||||||
'^.+\\.ts$': 'ts-jest'
|
'^.+\\.ts$': ['ts-jest', { isolatedModules: true }]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+83
-22
@@ -1,11 +1,8 @@
|
|||||||
import { execSync } from 'child_process';
|
|
||||||
|
|
||||||
import request from 'supertest';
|
import request from 'supertest';
|
||||||
|
|
||||||
import { build } from './src/app';
|
import { build } from './src/app';
|
||||||
import { createUserInput } from './src/utils/create-user';
|
import { createUserInput } from './src/utils/create-user';
|
||||||
import { examJson } from './__mocks__/exam';
|
import { examJson } from './__mocks__/exam';
|
||||||
import { MONGOHQ_URL } from './src/utils/env';
|
|
||||||
|
|
||||||
type FastifyTestInstance = Awaited<ReturnType<typeof build>>;
|
type FastifyTestInstance = Awaited<ReturnType<typeof build>>;
|
||||||
|
|
||||||
@@ -68,6 +65,77 @@ export function createSuperRequest(config: {
|
|||||||
return (resource, options) => superRequest(resource, config, options);
|
return (resource, options) => superRequest(resource, config, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type IndexData = {
|
||||||
|
collection: string;
|
||||||
|
indexes: {
|
||||||
|
key: Record<string, 1>;
|
||||||
|
name: string;
|
||||||
|
expireAfterSeconds?: number;
|
||||||
|
}[];
|
||||||
|
};
|
||||||
|
const indexData: IndexData[] = [
|
||||||
|
{
|
||||||
|
collection: 'AccessToken',
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
key: { userId: 1 },
|
||||||
|
name: 'userId_1'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: 'Donation',
|
||||||
|
indexes: [
|
||||||
|
{ key: { email: 1 }, name: 'email_1' },
|
||||||
|
{ key: { userId: 1 }, name: 'userId_1' }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: 'MsUsername',
|
||||||
|
indexes: [{ key: { userId: 1, id: 1 }, name: 'userId_1__id_1' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: 'Survey',
|
||||||
|
indexes: [{ key: { userId: 1 }, name: 'userId_1' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: 'UserRateLimit',
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
key: { expirationDate: 1 },
|
||||||
|
name: 'expirationDate_1',
|
||||||
|
expireAfterSeconds: 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: 'UserToken',
|
||||||
|
indexes: [{ key: { userId: 1 }, name: 'userId_1' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: 'sessions',
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
key: { expires: 1 },
|
||||||
|
name: 'expires_1',
|
||||||
|
expireAfterSeconds: 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: 'user',
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
key: { email: 1, sendQuincyEmail: 1 },
|
||||||
|
name: 'mailing-list-pull'
|
||||||
|
},
|
||||||
|
{ key: { email: 1 }, name: 'email_1' },
|
||||||
|
{ key: { isDonating: 1 }, name: 'isDonating_1' },
|
||||||
|
{ key: { username: 1, id: 1 }, name: 'username_1__id_1' }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
export function setupServer(): void {
|
export function setupServer(): void {
|
||||||
let fastify: FastifyTestInstance;
|
let fastify: FastifyTestInstance;
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
@@ -76,25 +144,18 @@ export function setupServer(): void {
|
|||||||
|
|
||||||
// Prisma does not support TTL indexes in the schema yet, so, to avoid
|
// Prisma does not support TTL indexes in the schema yet, so, to avoid
|
||||||
// conflicts with the TTL index in the sessions collection, we need to
|
// conflicts with the TTL index in the sessions collection, we need to
|
||||||
// create it manually (before interacting with the db in any way)
|
// create it manually (before interacting with the db in any way). Also,
|
||||||
await fastify.prisma.$runCommandRaw({
|
// to save time, we create all other indexes so we don't need to invoke
|
||||||
createIndexes: 'sessions',
|
// `prisma db push` (which is relatively slow).
|
||||||
indexes: [
|
|
||||||
{
|
await Promise.all(
|
||||||
key: { expires: 1 },
|
indexData.map(async ({ collection, indexes }) => {
|
||||||
name: 'expires_1',
|
await fastify.prisma.$runCommandRaw({
|
||||||
background: true,
|
createIndexes: collection,
|
||||||
expireAfterSeconds: 0
|
indexes
|
||||||
}
|
});
|
||||||
]
|
})
|
||||||
});
|
);
|
||||||
// push the schema to the test db to setup indexes, unique constraints, etc
|
|
||||||
execSync('pnpm prisma db push -- --skip-generate', {
|
|
||||||
env: {
|
|
||||||
...process.env,
|
|
||||||
MONGOHQ_URL
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
global.fastifyTestInstance = fastify;
|
global.fastifyTestInstance = fastify;
|
||||||
// allow a little time to setup the db
|
// allow a little time to setup the db
|
||||||
|
|||||||
Reference in New Issue
Block a user