Jest did not exit one second after the test run has completed.์ ๊ฐ์ ์๋ฌ๊ฐ ๋ฌ๋ค๋ฉด app.close()๋ฅผ ํตํด ํ
์คํธ๊ฐ ๋๋๊ณ ๋ ํ, app์ ์ข
๋ฃ์ํฌ ์ ์๋ค.
afterAll(async () => {
await app.close();
});
Connection์ ํน์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ํ ๋จ์ผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ORM ์ฐ๊ฒฐ.
์๋๋ getConnection()์ด๋ผ๋ ํจ์๋ฅผ ํตํด Connection์ ์งํํ ์ ์์์ง๋ง Deprecated๋ ์ดํ์๋ ์๋์ afterAll ๋ถ๋ถ์ ์ฝ๋์ ๊ฐ์ด ์์ฑํด์ผํ๋ค.
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํฉ๋๋ค. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ด ์๋ฃ๋ ํ์๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค. test๊ฐ ๋๋ ํ์ test๋ฅผ ์ํด ์์ฑํ๋ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํด์ฃผ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
/* eslint-disable prettier/prettier */
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { AppModule } from '../src/app.module';
import { DataSource } from 'typeorm';
const GRAPHQL_ENDPOINT = '/graphql';
const testUser = {
email: 'yeop@naver.com',
password: '12345',
};
jest.mock('got', () => {
return {
post: jest.fn(),
};
});
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = module.createNestApplication();
await app.init();
});
afterAll(async () => {
const dataSource = new DataSource({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'juyeop',
password: '12345',
database: 'nuber-eats-test',
});
const connection = await dataSource.initialize();
await connection.dropDatabase();
await connection.destroy();
app.close();
});
it.todo('can createAccount');
it.todo('userProfile');
it.todo('login');
it.todo('me');
it.todo('verifyEmail');
it.todo('userEdit');
});
"Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue. "
์ด์ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ ๋ package.json์ --detectOpenHandles๋ฅผ ์ถ๊ฐํ๋ฉด ์ด๋ ๋ถ๋ถ์ด asynchronous operations that weren't stopped ์ธ์ง ์๋ ค์ค๋ค.
์ด๋ฒ ๊ฒฝ์ฐ์๋ got moudule์์ ์๋ฌ๊ฐ ๋ฌ์ผ๋ฉฐ ์ด๋ฅผ ๊ณ ์น๊ธฐ ์ํด got.post๋ฅผ mockํจ์๋ก ๋ง๋ค์๋ค.
describe('login', () => {
it('Should login with correct credentials', () => {
return request(app.getHttpServer())
.post(GRAPHQL_ENDPOINT)
.send({
query: `
mutation {
login(input:{
email:"${testUser.email}",
password:"${testUser.password}",
}) {
ok
error
token
}
}
`,
})
.expect(200)
.expect((res) => {
const {
body: {
data: { login },
},
} = res;
expect(login.ok).toEqual(true);
expect(login.error).toEqual(null);
expect(login.token).toEqual(expect.any(String));
jwtToken = login.token;
});
});
});
usersRepository = module.get<Repository<User>>(getRepositoryToken(User));
๋ํ superTest๋ก request๋ฅผ ๋ณด๋ผ ๋, .post() ๋ค์ .set()์ ์ด์ฉํด์ http header๋ฅผ ๋ณด๋ผ ์ ์๋ค.
describe('userProfile', () => {
let userId: number;
beforeAll(async () => {
const [user] = await usersRepository.find();
userId = user.id;
});
it("Should see a User's Profile", () => {
return request(app.getHttpServer())
.post(GRAPHQL_ENDPOINT)
.set('X-JWT', jwtToken)
.send({
query: `
{
userProfile(userId:${userId}){
ok
error
user {
id
}
}
}
`,
})
.expect(200)
.expect((res) => {
const {
body: {
data: {
userProfile: {
ok,
error,
user: { id },
},
},
},
} = res;
expect(ok).toEqual(true);
expect(error).toEqual(null);
expect(id).toEqual(userId);
});
});