Skip to content

Commit ee6f971

Browse files
committed
test: createCrawl API import mode
1 parent 1a970df commit ee6f971

File tree

13 files changed

+124
-151
lines changed

13 files changed

+124
-151
lines changed

test/environment/api/crawlData.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ 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 type * as XCrawl from 'x-crawl'
66

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

10-
let xCrawl: typeof IXCrawl
11-
if (environment === 'dev') {
12-
xCrawl = require('src/').default
13-
} else if (environment === 'pro') {
14-
xCrawl = require('publish/')
15-
}
10+
const targetPath = environment === 'pro' ? 'publish/' : 'packages/'
11+
const createCrawl = (require(targetPath) as typeof XCrawl).createCrawl
1612

1713
jest.setTimeout(60000)
1814

1915
async function testCrawlData() {
20-
const testXCrawl = xCrawl()
16+
const testCrawlApp = createCrawl()
2117

22-
const res = await testXCrawl.crawlData({
18+
const res = await testCrawlApp.crawlData({
2319
targets: [
2420
'http://localhost:8888/data',
2521
{ url: 'http://localhost:8888/data' }

test/environment/api/crawlFile.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ import path from 'node:path'
33
import { expect, test, jest } from '@jest/globals'
44
import chalk from 'chalk'
55

6-
import IXCrawl from 'src/'
6+
import type * as XCrawl from 'x-crawl'
77

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

11-
let xCrawl: typeof IXCrawl
12-
if (environment === 'dev') {
13-
xCrawl = require('src/').default
14-
} else if (environment === 'pro') {
15-
xCrawl = require('publish/')
16-
}
11+
const targetPath = environment === 'pro' ? 'publish/' : 'packages/'
12+
const createCrawl = (require(targetPath) as typeof XCrawl).createCrawl
1713

1814
jest.setTimeout(60000)
1915

@@ -25,9 +21,11 @@ const urls: string[] = [
2521
const storeDirs = path.resolve(__dirname, './upload')
2622

2723
async function testCrawlFile() {
28-
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
24+
const testCrawlApp = createCrawl({
25+
proxy: { urls: ['http://localhost:14892'] }
26+
})
2927

30-
const res = await testXCrawl.crawlFile({
28+
const res = await testCrawlApp.crawlFile({
3129
targets: urls,
3230
storeDirs
3331
})

test/environment/api/crawlHTML.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@ 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 type * as XCrawl from 'x-crawl'
66

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

10-
let xCrawl: typeof IXCrawl
11-
if (environment === 'dev') {
12-
xCrawl = require('src/').default
13-
} else if (environment === 'pro') {
14-
xCrawl = require('publish/')
15-
}
10+
const targetPath = environment === 'pro' ? 'publish/' : 'packages/'
11+
const createCrawl = (require(targetPath) as typeof XCrawl).createCrawl
1612

1713
jest.setTimeout(60000)
1814

1915
async function testCrawlHTML() {
20-
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
16+
const testCrawlApp = createCrawl({
17+
proxy: { urls: ['http://localhost:14892'] }
18+
})
2119

22-
const res = await testXCrawl.crawlHTML({
20+
const res = await testCrawlApp.crawlHTML({
2321
targets: [
2422
'http://localhost:8888/html',
2523
{ url: 'http://localhost:8888/html' }

test/environment/api/crawlPage.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@ 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 type * as XCrawl from 'x-crawl'
66

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

10-
let xCrawl: typeof IXCrawl
11-
if (environment === 'dev') {
12-
xCrawl = require('src/').default
13-
} else if (environment === 'pro') {
14-
xCrawl = require('publish/')
15-
}
10+
const targetPath = environment === 'pro' ? 'publish/' : 'packages/'
11+
const createCrawl = (require(targetPath) as typeof XCrawl).createCrawl
1612

1713
jest.setTimeout(60000)
1814

1915
async function testCrawlPage() {
20-
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
16+
const testCrawlApp = createCrawl({
17+
proxy: { urls: ['http://localhost:14892'] }
18+
})
2119

22-
const res = await testXCrawl.crawlPage({
20+
const res = await testCrawlApp.crawlPage({
2321
targets: [
2422
'http://localhost:8888/html',
2523
{ url: 'http://localhost:8888/html' }

test/environment/arguments/fingerprint.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ 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 type * as XCrawl from 'x-crawl'
66

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

10-
let xCrawl: typeof IXCrawl
11-
if (environment === 'dev') {
12-
xCrawl = require('src/').default
13-
} else if (environment === 'pro') {
14-
xCrawl = require('publish/')
15-
}
10+
const targetPath = environment === 'pro' ? 'publish/' : 'packages/'
11+
const createCrawl = (require(targetPath) as typeof XCrawl).createCrawl
1612

1713
jest.setTimeout(60000)
1814

1915
async function fingerprint() {
20-
const testXCrawl = xCrawl()
16+
const testCrawlApp = createCrawl()
2117

22-
const res = await testXCrawl.crawlPage({
18+
const res = await testCrawlApp.crawlPage({
2319
targets: [
2420
'http://localhost:8888/html',
2521
{ url: 'http://localhost:8888/html', fingerprint: null },

test/environment/arguments/mode.test.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ 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 * as XCrawl from 'x-crawl'
66

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

10-
let xCrawl: typeof IXCrawl
11-
if (environment === 'dev') {
12-
xCrawl = require('src/').default
13-
} else if (environment === 'pro') {
14-
xCrawl = require('publish/')
15-
}
10+
const targetPath = environment === 'pro' ? 'publish/' : 'packages/'
11+
const createCrawl = (require(targetPath) as typeof XCrawl).createCrawl
1612

1713
jest.setTimeout(60000)
1814

1915
async function async() {
20-
const testXCrawl = xCrawl()
16+
const testCrawlApp = createCrawl()
2117

22-
const res = await testXCrawl.crawlData([
18+
const res = await testCrawlApp.crawlData([
2319
'http://localhost:8888/data',
2420
'http://localhost:8888/data'
2521
])
@@ -28,9 +24,9 @@ async function async() {
2824
}
2925

3026
async function sync() {
31-
const testXCrawl = xCrawl({ mode: 'sync' })
27+
const testCrawlApp = createCrawl({ mode: 'sync' })
3228

33-
const res = await testXCrawl.crawlData([
29+
const res = await testCrawlApp.crawlData([
3430
'http://localhost:8888/data',
3531
'http://localhost:8888/data'
3632
])

test/environment/arguments/proxy.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ 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 type * as XCrawl from 'x-crawl'
66

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

10-
let xCrawl: typeof IXCrawl
11-
if (environment === 'dev') {
12-
xCrawl = require('src/').default
13-
} else if (environment === 'pro') {
14-
xCrawl = require('publish/')
15-
}
10+
const targetPath = environment === 'pro' ? 'publish/' : 'packages/'
11+
const createCrawl = (require(targetPath) as typeof XCrawl).createCrawl
1612

1713
jest.setTimeout(60000)
1814

1915
async function proxy() {
20-
const testXCrawl = xCrawl()
16+
const testCrawlApp = createCrawl()
2117

22-
const res = await testXCrawl.crawlPage({
18+
const res = await testCrawlApp.crawlPage({
2319
targets: ['https://', 'http://localhost:8888/html'],
2420
maxRetry: 3,
2521
proxy: {

test/environment/functions/errorCollect.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ 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 type * as XCrawl from 'x-crawl'
66

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

10-
let xCrawl: typeof IXCrawl
11-
if (environment === 'dev') {
12-
xCrawl = require('src/').default
13-
} else if (environment === 'pro') {
14-
xCrawl = require('publish/')
15-
}
10+
const targetPath = environment === 'pro' ? 'publish/' : 'packages/'
11+
const createCrawl = (require(targetPath) as typeof XCrawl).createCrawl
1612

1713
jest.setTimeout(60000)
1814

1915
async function errorCollect() {
20-
const testXCrawl = xCrawl({ maxRetry: 2 })
16+
const testCrawlApp = createCrawl({ maxRetry: 2 })
2117

22-
const res = await testXCrawl.crawlPage(['https://', 'https://', 'https://'])
18+
const res = await testCrawlApp.crawlPage(['https://', 'https://', 'https://'])
2319

2420
await res[0].data.browser.close()
2521

test/environment/written/crawlData.test.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,31 @@ 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 type * as XCrawl from 'x-crawl'
66

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

10-
let xCrawl: typeof IXCrawl
11-
if (environment === 'dev') {
12-
xCrawl = require('src/').default
13-
} else if (environment === 'pro') {
14-
xCrawl = require('publish/')
15-
}
10+
const targetPath = environment === 'pro' ? 'publish/' : 'packages/'
11+
const createCrawl = (require(targetPath) as typeof XCrawl).createCrawl
1612

1713
jest.setTimeout(60000)
1814

1915
/* 1.Written */
2016
// 1.1.written string
2117
async function writtenString() {
22-
const testXCrawl = xCrawl()
18+
const testCrawlApp = createCrawl()
2319

24-
const res = await testXCrawl.crawlData('http://localhost:8888/data')
20+
const res = await testCrawlApp.crawlData('http://localhost:8888/data')
2521

2622
return res.isSuccess
2723
}
2824

2925
// 1.2.written CrawlDataDetailConfig
3026
async function writtenCrawlDataDetailConfig() {
31-
const testXCrawl = xCrawl()
27+
const testCrawlApp = createCrawl()
3228

33-
const res = await testXCrawl.crawlData({
29+
const res = await testCrawlApp.crawlData({
3430
url: 'http://localhost:8888/data'
3531
})
3632

@@ -39,9 +35,9 @@ async function writtenCrawlDataDetailConfig() {
3935

4036
// 1.3.written (string | CrawlDataDetailConfig)[]
4137
async function writtenStringAndCrawlDataDetailConfigArr() {
42-
const testXCrawl = xCrawl()
38+
const testCrawlApp = createCrawl()
4339

44-
const res = await testXCrawl.crawlData([
40+
const res = await testCrawlApp.crawlData([
4541
'http://localhost:8888/data',
4642
{ url: 'http://localhost:8888/data' }
4743
])
@@ -51,9 +47,9 @@ async function writtenStringAndCrawlDataDetailConfigArr() {
5147

5248
// 1.4.written CrawlDataAdvancedConfig
5349
async function writtenCrawlDataAdvancedConfig() {
54-
const testXCrawl = xCrawl()
50+
const testCrawlApp = createCrawl()
5551

56-
const res = await testXCrawl.crawlData({
52+
const res = await testCrawlApp.crawlData({
5753
targets: [
5854
'http://localhost:8888/data',
5955
{ url: 'http://localhost:8888/data' }
@@ -66,26 +62,26 @@ async function writtenCrawlDataAdvancedConfig() {
6662
/* 2.Loader Config */
6763
// 2.1.Loader Base Config
6864
async function loaderBaseConfig() {
69-
const testXCrawl = xCrawl({
65+
const testCrawlApp = createCrawl({
7066
baseUrl: 'http://localhost:8888',
7167
proxy: { urls: ['http://localhost:14892'] },
7268
timeout: 10000,
7369
intervalTime: { max: 1000 },
7470
maxRetry: 0
7571
})
7672

77-
const res = await testXCrawl.crawlData(['/data', '/data'])
73+
const res = await testCrawlApp.crawlData(['/data', '/data'])
7874

7975
return res.reduce((prev, item) => prev && item.isSuccess, true)
8076
}
8177

8278
// 2.2.Loader Advanced Config
8379
async function loaderAdvancedConfig() {
84-
const testXCrawl = xCrawl({
80+
const testCrawlApp = createCrawl({
8581
baseUrl: 'http://localhost:8888'
8682
})
8783

88-
const res = await testXCrawl.crawlData({
84+
const res = await testCrawlApp.crawlData({
8985
targets: ['/data', '/data'],
9086
proxy: { urls: ['http://localhost:14892'] },
9187
timeout: 10000,

0 commit comments

Comments
 (0)