不能在一个文件中运行多个测试
Posted
技术标签:
【中文标题】不能在一个文件中运行多个测试【英文标题】:Can not run multiple tests in a file 【发布时间】:2021-02-24 14:02:30 【问题描述】:我正在构建一个 GraphQL API,我想开玩笑地测试一些解析器和数据库。
这是我的帮助文件,我在其中设置上下文和 Prisma 客户端以进行测试。
import PrismaClient from "@prisma/client";
import ServerInfo from "apollo-server";
import execSync from "child_process";
import getPort, makeRange from "get-port";
import GraphQLClient from "graphql-request";
import nanoid from "nanoid";
import join from "path";
import Client from "pg";
import server from "../api/server";
type TestContext =
client: GraphQLClient;
db: PrismaClient;
;
export function createTestContext(): TestContext
let ctx = as TestContext;
const graphqlCtx = graphqlTestContext();
const prismaCtx = prismaTestContext();
beforeEach(async () =>
const client = await graphqlCtx.before();
const db = await prismaCtx.before();
Object.assign(ctx,
client,
db,
);
);
afterEach(async () =>
await graphqlCtx.after();
await prismaCtx.after();
);
return ctx;
function graphqlTestContext()
let serverInstance: ServerInfo | null = null;
return
async before()
const port = await getPort( port: makeRange(4000, 6000) );
serverInstance = await server.listen( port );
return new GraphQLClient(`http://localhost:$port`);
,
async after()
serverInstance?.server.close();
,
;
function prismaTestContext()
const prismaBinary = join(__dirname, "..", "node_modules", ".bin", "prisma");
let schema = "";
let databaseUrl = "";
let prismaClient: null | PrismaClient = null;
return
async before()
schema = `test_$nanoid()`;
databaseUrl = `postgresql://user:123@localhost:5432/testing?schema=$schema`;
process.env.DATABASE_URL = databaseUrl;
execSync(`$prismaBinary migrate up --create-db --experimental`,
env:
...process.env,
DATABASE_URL: databaseUrl,
,
);
prismaClient = new PrismaClient();
return prismaClient;
,
async after()
const client = new Client(
connectionString: databaseUrl,
);
await client.connect();
await client.query(`DROP SCHEMA IF EXISTS "$schema" CASCADE`);
await client.end();
await prismaClient?.$disconnect();
,
;
我的测试文件如下所示:
import createTestContext from "./__helpers";
const ctx = createTestContext();
it("register user", async () =>
const testUser =
username: "Test",
email: "test@test.com",
password: "password",
;
const registerResult = await ctx.client.request(
`
mutation registerNewUser($username: String!, $email: String!, $password: String!)
register(username: $username, email: $email, password: $password)
user
user_id
username
email
`,
username: testUser.username,
email: testUser.email,
password: testUser.password,
);
const resultUsername = registerResult.register.user.username;
const resultEmail = registerResult.register.user.email;
const resultUserID = registerResult.register.user.user_id;
expect(resultUsername).toBe(testUser.username);
expect(resultEmail).toBe(testUser.email);
expect(resultUserID).not.toBeNull;
const users = await ctx.db.user.findMany();
const savedUser = users[0];
expect(savedUser.username).toBe(testUser.username);
expect(savedUser.email).toBe(testUser.email);
expect(savedUser.user_id).toBe(resultUserID);
expect(savedUser.first_name).toBeNull;
expect(savedUser.last_name).toBeNull;
expect(savedUser.role).toBe("USER");
expect(savedUser.password).not.toBe(testUser.password);
);
it("all events", async () =>
const eventsResult = await ctx.client.request(
`
query
allEvents
event_id
title
description
`
);
expect(eventsResult.allEvents.length).toBe(0)
);
当我只运行一个包含一个测试的文件时,一切正常。但是当我在一个文件中运行多个测试时,第一个运行正常,但之后的运行不正常。我收到此错误:
The table `test_LjrcmbMjI4vLaDYM9-lvw.Event` does not exist in the current database.: "response":"errors":["message":"\nInvalid `prisma.event.findMany()` invocation:\n\n\n The table `test_LjrcmbMjI4vLaDYM9-lvw.Event` does not exist in the current database.","locations":["line":3,"column":7],"path":["allEvents"],"extensions":"code":"INTERNAL_SERVER_ERROR","exception":"code":"P2021","clientVersion":"2.11.0","meta":"table":"test_LjrcmbMjI4vLaDYM9-lvw.Event"],"data":null,"status":200,"request":"query":"\n query \n allEvents \n event_id\n title\n description\n \n \n "
此外,当我在单独的文件中运行两个测试时,在每第二次测试运行时我都会收到此错误:listen EADDRINUSE: address already in use :::4200
我完成了 nexus 教程(第 4 步和第 5 步),他们解释了如何进行测试,但不知何故它不起作用。所以请帮助我。
https://nexusjs.org/docs/getting-started/tutorial
【问题讨论】:
【参考方案1】:我为相同的here 创建了一个包含并行测试的存储库。测试环境设置在prisma
文件夹中,在tests
文件夹中创建了一个类似的帮助程序。
【讨论】:
这对我没有帮助,因为您使用的是 SQLite 数据库而不是 Postgres 数据库。 这里是 one 和 Postgres。思路是一样的,只是我这里用的是自省,你可以用我的 SQLite 例子中的 Migrate 命令替换那些。以上是关于不能在一个文件中运行多个测试的主要内容,如果未能解决你的问题,请参考以下文章
使用 Android-Espresso 运行多个测试时出现内存不足异常