Skip to content

Commit 6e6189e

Browse files
committed
Feat: Perfect the test
1 parent 17ef66d commit 6e6189e

18 files changed

+296
-111
lines changed

jest.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ module.exports = {
33
preset: 'ts-jest',
44
testEnvironment: 'node',
55
collectCoverage: true,
6-
detectOpenHandles: true
6+
detectOpenHandles: true,
7+
moduleNameMapper: {
8+
'^src/(.*)$': '<rootDir>/src/$1',
9+
'^publish/(.*)$': '<rootDir>/publish/$1'
10+
}
711
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
"start-server": "rollup --config script/server.mjs",
2626
"test-dev": "jest test/environment/test.ts dev",
2727
"test-pro": "jest test/environment/test.ts pro",
28-
"test-crawlPage": "jest test/environment/crawlPage.test.ts dev",
29-
"test-crawlData": "jest test/environment/crawlData.test.ts dev",
30-
"test-crawlFile": "jest test/environment/crawlFile.test.ts dev",
28+
"test-crawlPage": "jest test/environment/api/crawlPage.test.ts dev",
29+
"test-crawlData": "jest test/environment/api/crawlData.test.ts dev",
30+
"test-crawlFile": "jest test/environment/api/crawlFile.test.ts dev",
3131
"prettier": "prettier --write ."
3232
},
3333
"dependencies": {

script/start.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export default {
88
file: 'test/start/index.js',
99
format: 'cjs'
1010
},
11-
plugins: [tsPlugin(), runPlugin({ stdin: { clear: true } })]
11+
plugins: [tsPlugin(), terserPlugin(), runPlugin({ stdin: { clear: true } })]
1212
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import process from 'node:process'
2+
import { expect, test, jest } from '@jest/globals'
3+
import chalk from 'chalk'
4+
5+
import IXCrawl from 'src/'
6+
7+
const args = process.argv.slice(3)
8+
const environment = args[0]
9+
10+
let xCrawl: typeof IXCrawl
11+
if (environment === 'dev') {
12+
xCrawl = require('src/').default
13+
} else if (environment === 'pro') {
14+
xCrawl = require('publish/dist')
15+
}
16+
17+
jest.setTimeout(60000)
18+
19+
async function testCrawlData() {
20+
const testXCrawl = xCrawl()
21+
22+
const res = await testXCrawl.crawlData({
23+
targets: ['http://localhost:8888', { url: 'http://localhost:8888' }]
24+
})
25+
26+
return res.reduce((prev, item) => prev && item.isSuccess, true)
27+
}
28+
29+
test('crawlData', async () => {
30+
console.log(chalk.bgGreen('================ crawlData ================'))
31+
await expect(testCrawlData()).resolves.toBe(true)
32+
})
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import process from 'node:process'
2+
import path from 'node:path'
3+
import { expect, test, jest } from '@jest/globals'
4+
import chalk from 'chalk'
5+
6+
import IXCrawl from 'src/'
7+
8+
const args = process.argv.slice(3)
9+
const environment = args[0]
10+
11+
let xCrawl: typeof IXCrawl
12+
if (environment === 'dev') {
13+
xCrawl = require('src/').default
14+
} else if (environment === 'pro') {
15+
xCrawl = require('publish/dist')
16+
}
17+
18+
jest.setTimeout(60000)
19+
20+
const urls: string[] = [
21+
'https://raw.githubusercontent.com/coder-hxl/airbnb-upload/master/area/4401.jpg',
22+
'https://raw.githubusercontent.com/coder-hxl/airbnb-upload/master/area/4403.jpg'
23+
]
24+
25+
const storeDir = path.resolve(__dirname, './upload')
26+
27+
async function testCrawlFile() {
28+
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
29+
30+
const res = await testXCrawl.crawlFile({
31+
targets: urls,
32+
storeDir
33+
})
34+
35+
return res.reduce(
36+
(prev, item) => prev && item.isSuccess && !!item.data?.data.isSuccess,
37+
true
38+
)
39+
}
40+
41+
test('crawlFile', async () => {
42+
console.log(chalk.bgGreen('================ crawlFile ================'))
43+
await expect(testCrawlFile()).resolves.toBe(true)
44+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import process from 'node:process'
2+
import { expect, test, jest } from '@jest/globals'
3+
import chalk from 'chalk'
4+
5+
import IXCrawl from 'src/'
6+
7+
8+
const args = process.argv.slice(3)
9+
const environment = args[0]
10+
11+
let xCrawl: typeof IXCrawl
12+
if (environment === 'dev') {
13+
xCrawl = require('src/').default
14+
} else if (environment === 'pro') {
15+
xCrawl = require('publish/dist')
16+
}
17+
18+
jest.setTimeout(60000)
19+
20+
async function testCrawlPage() {
21+
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
22+
23+
const res = await testXCrawl.crawlPage({
24+
targets: [
25+
'https://github.com/coder-hxl/x-crawl',
26+
{ url: 'https://github.com/coder-hxl/x-crawl' }
27+
]
28+
})
29+
30+
await res[0].data.browser.close()
31+
32+
return res.reduce((prev, item) => prev && item.isSuccess, true)
33+
}
34+
35+
test('crawlPage', async () => {
36+
console.log(chalk.bgGreen('================ crawlPage ================'))
37+
await expect(testCrawlPage()).resolves.toBe(true)
38+
})

test/environment/startPolling.test.ts renamed to test/environment/api/startPolling.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import process from 'node:process'
22
import { expect, test } from '@jest/globals'
33
import chalk from 'chalk'
44

5-
import IXCrawl from '../../src'
5+
import IXCrawl from 'src/'
6+
67

78
const args = process.argv.slice(3)
89
const environment = args[0]
910

1011
let xCrawl: typeof IXCrawl
1112
if (environment === 'dev') {
12-
xCrawl = require('../../src').default
13+
xCrawl = require('src/').default
1314
} else if (environment === 'pro') {
14-
xCrawl = require('../../publish/dist')
15+
xCrawl = require('publish/dist')
1516
}
1617

1718
function startPolling() {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import process from 'node:process'
2+
import { expect, test, jest } from '@jest/globals'
3+
import chalk from 'chalk'
4+
5+
import IXCrawl from 'src/'
6+
7+
const args = process.argv.slice(3)
8+
const environment = args[0]
9+
10+
let xCrawl: typeof IXCrawl
11+
if (environment === 'dev') {
12+
xCrawl = require('src/').default
13+
} else if (environment === 'pro') {
14+
xCrawl = require('publish/dist')
15+
}
16+
17+
jest.setTimeout(60000)
18+
19+
async function fingerprint() {
20+
const testXCrawl = xCrawl()
21+
22+
const res = await testXCrawl.crawlPage({
23+
targets: [
24+
'http://localhost:8888',
25+
{ url: 'http://localhost:8888', fingerprint: null },
26+
{
27+
url: 'http://localhost:8888',
28+
fingerprint: {
29+
maxWidth: 1024,
30+
maxHeight: 800,
31+
ua: `Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99`,
32+
mobile: 'random',
33+
platform: 'Windows',
34+
platformVersion: '10',
35+
acceptLanguage: `zh-CN,zh;q=0.9,en;q=0.8`,
36+
userAgent: {
37+
value:
38+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
39+
versions: [
40+
{
41+
name: 'Chrome',
42+
maxMinorVersion: 10,
43+
maxPatchVersion: 5615
44+
},
45+
{ name: 'Safari', maxMinorVersion: 36, maxPatchVersion: 2333 }
46+
]
47+
}
48+
}
49+
}
50+
],
51+
fingerprints: [
52+
{
53+
platform: 'Windows',
54+
mobile: 'random',
55+
userAgent: {
56+
value:
57+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
58+
versions: [
59+
{
60+
name: 'Chrome',
61+
maxMajorVersion: 112,
62+
minMajorVersion: 100,
63+
maxMinorVersion: 20,
64+
maxPatchVersion: 5000
65+
},
66+
{
67+
name: 'Safari',
68+
maxMajorVersion: 537,
69+
minMajorVersion: 500,
70+
maxMinorVersion: 36,
71+
maxPatchVersion: 5000
72+
}
73+
]
74+
}
75+
}
76+
]
77+
})
78+
79+
res[0].data.browser.close()
80+
81+
return res.reduce((prev, item) => prev && item.isSuccess, true)
82+
}
83+
84+
test('fingerprint', async () => {
85+
console.log(chalk.bgGreen('================ fingerprint ================'))
86+
await expect(fingerprint()).resolves.toBe(true)
87+
})

test/environment/mode.test.ts renamed to test/environment/arguments/mode.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import process from 'node:process'
22
import { expect, test, jest } from '@jest/globals'
33
import chalk from 'chalk'
44

5-
import IXCrawl from '../../src'
5+
import IXCrawl from 'src/'
66

77
const args = process.argv.slice(3)
88
const environment = args[0]
99

1010
let xCrawl: typeof IXCrawl
1111
if (environment === 'dev') {
12-
xCrawl = require('../../src').default
12+
xCrawl = require('src/').default
1313
} else if (environment === 'pro') {
14-
xCrawl = require('../../publish/dist')
14+
xCrawl = require('publish/dist')
1515
}
1616

1717
jest.setTimeout(60000)

test/environment/proxy.test.ts renamed to test/environment/arguments/proxy.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import process from 'node:process'
22
import { expect, test, jest } from '@jest/globals'
33
import chalk from 'chalk'
44

5-
import IXCrawl from '../../src'
5+
import IXCrawl from 'src/'
66

77
const args = process.argv.slice(3)
88
const environment = args[0]
99

1010
let xCrawl: typeof IXCrawl
1111
if (environment === 'dev') {
12-
xCrawl = require('../../src').default
12+
xCrawl = require('src/').default
1313
} else if (environment === 'pro') {
14-
xCrawl = require('../../publish/dist')
14+
xCrawl = require('publish/dist')
1515
}
1616

1717
jest.setTimeout(60000)
@@ -20,7 +20,7 @@ async function proxy() {
2020
const testXCrawl = xCrawl()
2121

2222
const res = await testXCrawl.crawlPage({
23-
targets: ['https://www.google.com', 'https://github.com/coder-hxl'],
23+
targets: ['https://', 'https://github.com/coder-hxl'],
2424
maxRetry: 3,
2525
proxy: {
2626
urls: ['http://localhost:129032', 'http://localhost:14892'],
@@ -30,7 +30,11 @@ async function proxy() {
3030

3131
await res[0].data.browser.close()
3232

33-
return res.reduce((prev, item) => prev && item.isSuccess, true)
33+
return (
34+
res[0].proxyDetails[0].state === false &&
35+
res[1].isSuccess &&
36+
res[1].proxyDetails[1].state === true
37+
)
3438
}
3539

3640
test('proxy', async () => {

0 commit comments

Comments
 (0)