Skip to content

Commit bd85671

Browse files
committed
added tests
1 parent a5ce9ea commit bd85671

File tree

6 files changed

+191
-78
lines changed

6 files changed

+191
-78
lines changed

package.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
{
22
"name": "email-validator",
33
"version": "0.1.0",
4-
"description": "Email Validator",
4+
"description": "Validates emails based on regex, common typos, disposable email blacklists, DNS records and SMTP server response.",
55
"repository": {
66
"type": "git",
7-
"url": "https://github.com/mfbx9da4"
7+
"url": "https://github.com/mfbx9da4/email-validator"
88
},
9-
"author": "David Adler",
9+
"author": "David Alberto Adler",
1010
"license": "MIT",
1111
"scripts": {
12-
"start": "npm run serve",
12+
"start": "yarn serve",
13+
"build": "tsc",
14+
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
15+
"prepare": "yarn build",
16+
"prepublishOnly": "yarn test && yarn lint",
17+
"preversion": "yarn lint",
18+
"version": "yarn format && git add -A src",
19+
"postversion": "git push && git push --tags",
1320
"watch-node": "nodemon dist/index.js",
1421
"test": "jest --verbose",
15-
"watch-test": "npm run test -- --watchAll",
22+
"watch-test": "yarn test -- --watchAll",
1623
"watch-ts": "tsc -w",
1724
"lint": "tsc --noEmit && eslint \"**/*.{js,ts}\" --quiet --fix",
18-
"debug": "npm run build && npm run watch-debug",
25+
"debug": "yarn build && yarn watch-debug",
1926
"run-debug": "nodemon --inspect dist/server.js"
2027
},
2128
"dependencies": {

src/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@ import { checkTypo } from './typo/typo'
44
import { getBestMx } from './dns/dns'
55
import { checkSMTP } from './smtp/smtp'
66
import { checkDisposable } from './disposable/disposable'
7-
8-
declare global {
9-
interface ObjectConstructor {
10-
typedKeys<T>(o: T): Array<keyof T>
11-
}
12-
}
13-
Object.typedKeys = Object.keys as any
7+
import './types'
148

159
export async function validate(
1610
recipient: string,
1711
sender: string = '[email protected]'
1812
): Promise<OutputFormat> {
19-
if (!isEmail(recipient)) return createOutput('regex')
13+
if (!isEmail(recipient)) return createOutput('regex', 'Invalid regex')
2014

2115
const typoResponse = await checkTypo(recipient)
2216
if (typoResponse) return createOutput('typo', typoResponse)

src/smtp/smtp.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import net from 'net'
22
import { OutputFormat, createOutput } from '../output/output'
33
import { hasCode, ErrorCodes } from './errorCodes'
44

5+
const log = (...args: unknown[]) => {
6+
if (process.env.DEBUG === 'true') {
7+
console.log(...args)
8+
}
9+
}
10+
511
export const checkSMTP = async (
612
sender: string,
713
recipient: string,
@@ -13,7 +19,7 @@ export const checkSMTP = async (
1319
socket.setEncoding('ascii')
1420
socket.setTimeout(timeout)
1521
socket.on('error', error => {
16-
console.log('error', error)
22+
log('error', error)
1723
socket.emit('fail', error)
1824
})
1925

@@ -51,7 +57,7 @@ export const checkSMTP = async (
5157

5258
socket.on('connect', () => {
5359
socket.on('data', msg => {
54-
console.log('data', msg)
60+
log('data', msg)
5561
if (hasCode(msg, 220) || hasCode(msg, 250)) {
5662
socket.emit('next', msg)
5763
} else if (hasCode(msg, 550)) {

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export {}
2+
declare global {
3+
interface ObjectConstructor {
4+
typedKeys<T>(o: T): Array<keyof T>
5+
}
6+
}
7+
Object.typedKeys = Object.keys as any

test/__snapshots__/index.test.ts.snap

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`validation tests bad username 1`] = `
4+
Object {
5+
"valid": false,
6+
"validators": Object {
7+
"disposable": Object {
8+
"reason": undefined,
9+
"valid": false,
10+
},
11+
"mx": Object {
12+
"reason": undefined,
13+
"valid": false,
14+
},
15+
"regex": Object {
16+
"reason": undefined,
17+
"valid": false,
18+
},
19+
"smtp": Object {
20+
"reason": "Invalid Mailbox",
21+
"valid": false,
22+
},
23+
"typo": Object {
24+
"reason": undefined,
25+
"valid": false,
26+
},
27+
},
28+
}
29+
`;
30+
31+
exports[`validation tests fails with bad dns 1`] = `
32+
Object {
33+
"valid": false,
34+
"validators": Object {
35+
"disposable": Object {
36+
"reason": undefined,
37+
"valid": false,
38+
},
39+
"mx": Object {
40+
"reason": "MX record not found",
41+
"valid": false,
42+
},
43+
"regex": Object {
44+
"reason": undefined,
45+
"valid": false,
46+
},
47+
"smtp": Object {
48+
"reason": undefined,
49+
"valid": false,
50+
},
51+
"typo": Object {
52+
"reason": undefined,
53+
"valid": false,
54+
},
55+
},
56+
}
57+
`;
58+
59+
exports[`validation tests fails with bad regex 1`] = `
60+
Object {
61+
"valid": false,
62+
"validators": Object {
63+
"disposable": Object {
64+
"reason": undefined,
65+
"valid": false,
66+
},
67+
"mx": Object {
68+
"reason": undefined,
69+
"valid": false,
70+
},
71+
"regex": Object {
72+
"reason": "Invalid regex",
73+
"valid": false,
74+
},
75+
"smtp": Object {
76+
"reason": undefined,
77+
"valid": false,
78+
},
79+
"typo": Object {
80+
"reason": undefined,
81+
"valid": false,
82+
},
83+
},
84+
}
85+
`;
86+
87+
exports[`validation tests fails with bad username 1`] = `
88+
Object {
89+
"valid": false,
90+
"validators": Object {
91+
"disposable": Object {
92+
"reason": undefined,
93+
"valid": false,
94+
},
95+
"mx": Object {
96+
"reason": undefined,
97+
"valid": false,
98+
},
99+
"regex": Object {
100+
"reason": undefined,
101+
"valid": false,
102+
},
103+
"smtp": Object {
104+
"reason": "Invalid Mailbox",
105+
"valid": false,
106+
},
107+
"typo": Object {
108+
"reason": undefined,
109+
"valid": false,
110+
},
111+
},
112+
}
113+
`;
114+
115+
exports[`validation tests fails with common typo 1`] = `
116+
Object {
117+
"valid": false,
118+
"validators": Object {
119+
"disposable": Object {
120+
"reason": undefined,
121+
"valid": false,
122+
},
123+
"mx": Object {
124+
"reason": undefined,
125+
"valid": false,
126+
},
127+
"regex": Object {
128+
"reason": undefined,
129+
"valid": false,
130+
},
131+
"smtp": Object {
132+
"reason": undefined,
133+
"valid": false,
134+
},
135+
"typo": Object {
136+
"reason": "Likely typo, suggested email: [email protected]",
137+
"valid": false,
138+
},
139+
},
140+
}
141+
`;

test/index.test.ts

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,21 @@
1-
import { Robot } from "../src/index";
2-
import _ from "lodash";
1+
import { validate } from '../src/index'
2+
import _ from 'lodash'
33

4-
const equal = (a: any, b: any) => expect(_.isEqual(a, b)).toBe(true);
5-
6-
describe("name", () => {
7-
// describe("I/O", () => {
8-
// it("parses input correctly", () => {
9-
// Robot.fromInput(`\
10-
// 3
11-
// pile 1 onto 2
12-
// pile 0 onto 2
13-
// quit`);
14-
// });
15-
// });
16-
17-
it("doesn't crash when constructing", () => {
18-
new Robot(3);
19-
});
20-
21-
it("blocks should remain without operations", () => {
22-
const robot = new Robot(3);
23-
equal(robot.getOutput(), [[0], [1], [2]]);
24-
});
25-
26-
describe("pile onto", () => {
27-
it("two operations", () => {
28-
const robot = new Robot(3);
29-
robot.pileOnto(1, 2);
30-
robot.pileOnto(0, 2);
31-
equal(robot.getOutput(), [[], [1], [2, 0]]);
32-
});
33-
});
34-
describe("pile over", () => {
35-
it("one operation", () => {
36-
const robot = new Robot(3);
37-
robot.pileOver(1, 2);
38-
equal(robot.getOutput(), [[0], [], [2, 1]]);
39-
});
40-
it("three operations", () => {
41-
const robot = new Robot(3);
42-
robot.pileOver(1, 2);
43-
robot.pileOver(1, 0);
44-
robot.pileOver(0, 2);
45-
46-
equal(robot.getOutput(), [[], [], [2, 0, 1]]);
47-
});
48-
});
49-
describe("move over", () => {
50-
it("one operation", () => {
51-
const robot = new Robot(3);
52-
robot.moveOver(1, 2);
53-
equal(robot.getOutput(), [[0], [], [2, 1]]);
54-
});
55-
56-
it("two operations", () => {
57-
const robot = new Robot(3);
58-
robot.moveOver(1, 2);
59-
robot.moveOver(2, 0);
60-
equal(robot.getOutput(), [[0, 2], [1], []]);
61-
});
62-
});
63-
});
4+
describe('validation tests', () => {
5+
it('fails with bad regex', async () => {
6+
const res = await validate('dav [email protected]')
7+
expect(res).toMatchSnapshot()
8+
})
9+
it('fails with common typo', async () => {
10+
const res = await validate('[email protected]')
11+
expect(res).toMatchSnapshot()
12+
})
13+
// it('fails with bad dns', async () => {
14+
// const res = await validate('[email protected]')
15+
// expect(res).toMatchSnapshot()
16+
// })
17+
it('fails with bad username', async () => {
18+
const res = await validate('[email protected]')
19+
expect(res).toMatchSnapshot()
20+
})
21+
})

0 commit comments

Comments
 (0)