Directory Structure
.
├── __tests__
│ ├── integration
│ │ └── posts.integration.spec.js
│ └── unit
│ ├── controllers
│ │ └── posts.controller.unit.spec.js
│ ├── repositories
│ │ └── posts.repository.unit.spec.js
│ └── services
│ └── posts.service.unit.spec.js
├── app.js
├── config
│ └── config.json
├── controllers
│ └── posts.controller.js
├── jest.config.js
├── migrations
│ └── 20220731133318-create-posts.js
├── models
│ ├── index.js
│ └── posts.js
├── package-lock.json
├── package.json
├── repositories
│ └── posts.repository.js
├── routes
│ ├── index.js
│ └── posts.routes.js
└── services
└── posts.service.js
테스트코드 예시(기초)
// validation.js
module.exports = {
isEmail: (value) => {
const email = (value || '');
// 이메일 주소에 @ 문자가 1개만 있어야 한다.
if (email.split('@').length !== 2) {
return false;
}
// 이메일 주소에 공백(스페이스)이 존재하면 안된다.
else if (email.includes(' ')) {
return false;
}
// 이메일 주소 맨 앞에 하이픈(-)이 존재하면 안된다.
else if (email[0] === '-') {
return false;
}
// 위 3개의 조건을 만족한다면 패스
return true;
},
};
// validation.spec.js
// → validation.js 에 있는 isEmail 에 대한 테스트코드
const { isEmail } = require('./validation');
test('입력한 이메일 주소에는 "@" 문자가 1개만 있어야 이메일 형식이다.', () => {
expect(isEmail('[email protected]')).toEqual(true);
expect(isEmail('luna@@@@@naver.com')).toEqual(false);
expect(isEmail('lunanaver.com')).toEqual(false);
});
test('입력한 이메일 주소에 공백(스페이스)이 존재하면 이메일 형식이 아니다.', () => {
expect(isEmail('[email protected]')).toEqual(true);
expect(isEmail('luna @naver.com')).toEqual(false);
});
test('입력한 이메일 주소 맨 앞에 하이픈(-)이 있으면 이메일 형식이 아니다.', () => {
expect(isEmail('[email protected]')).toEqual(true);
expect(isEmail('[email protected]')).toEqual(false);
});
테스트코드 예시(심화)
// validation.js
module.exports = {
isEmail: (value) => {
const email = value || '';
const [localPart, domain, ...etc] = email.split('@');
if (email.split('@').length !== 2) {
return false;
} else if (email.includes(' ')) {
return false;
} else if (email[0] === '-') {
return false;
} else if (!/^[a-z0-9+_-]+$/gi.test(localPart)) {
return false;
} else if (!/^[a-z0-9.-]+$/gi.test(domain)) {
return false;
}
return true;
},
};
// validation.spec.js
// → validation.js 에 있는 isEmail 에 대한 테스트코드
const { isEmail } = require('./validation');
test('입력한 이메일 주소에는 "@" 문자가 1개만 있어야 이메일 형식이다.', () => {
expect(isEmail('[email protected]')).toEqual(true);
expect(isEmail('luna@@@@@naver.com')).toEqual(false);
expect(isEmail('lunanaver.com')).toEqual(false);
});
test('입력한 이메일 주소에 공백(스페이스)이 존재하면 이메일 형식이 아니다.', () => {
expect(isEmail('[email protected]')).toEqual(true);
expect(isEmail('luna @naver.com')).toEqual(false);
});
test('입력한 이메일 주소 맨 앞에 하이픈(-)이 있으면 이메일 형식이 아니다.', () => {
expect(isEmail('[email protected]')).toEqual(true);
expect(isEmail('[email protected]')).toEqual(false);
});
test('입력한 이메일 주소중, 로컬 파트(골뱅이 기준 앞부분)에는 영문 대소문자와 숫자, 특수문자는 덧셈기호(+), 하이픈(-), 언더바(_) 3개 외에 다른 값이 존재하면 이메일 형식이 아니다.', () => {
expect(isEmail('[email protected]')).toEqual(true);
expect(isEmail('[email protected]')).toEqual(false);
});
test('입력한 이메일 주소중, 도메인(골뱅이 기준 뒷부분)에는 영문 대소문자와 숫자, 점(.), 하이픈(-) 외에 다른 값이 존재하면 이메일 형식이 아니다.', () => {
expect(isEmail('[email protected]')).toEqual(true);
expect(isEmail('Dev-Luna1234@test_domain.com')).toEqual(false);
expect(isEmail('Dev-Luna1234@test~domain.com')).toEqual(false);
});
궁금증
Q. service 단에서 왜 ‘../models/index.js’ 를 require 하였는가?
Q. repository 단에서 PostsModel 은 어디서 나온 것인가?