Skip to content

Commit f23f81a

Browse files
chore: add lint, prettier, knip, pre-commit and remove dead code (#68)
* chore: lint, format, knip, remove dead code * update package.json * update package.json
1 parent 188a8eb commit f23f81a

33 files changed

Lines changed: 1172 additions & 1263 deletions

.github/workflows/_check_code.yaml

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Run actionlint
1717
uses: rhysd/actionlint@v1.7.11
1818

19-
# TODO: Fix spell check after we merge current PRs
19+
# TODO: Fix spell check after we merge current PRs
2020
# spell_check:
2121
# name: Spell check
2222
# runs-on: ubuntu-latest
@@ -26,23 +26,22 @@ jobs:
2626
# - name: Check spelling with typos
2727
# uses: crate-ci/typos@v1
2828

29-
# TODO: Fix lint after we merge current PRs
30-
# lint_check:
31-
# name: Lint check
32-
# runs-on: ubuntu-latest
33-
# steps:
34-
# - name: Checkout repository
35-
# uses: actions/checkout@v6
36-
# - name: Use Node.js
37-
# uses: actions/setup-node@v6
38-
# with:
39-
# node-version: 24
40-
# cache: 'npm'
41-
# cache-dependency-path: 'package-lock.json'
42-
# - name: Install dependencies
43-
# run: npm ci
44-
# - name: Lint
45-
# run: npm run lint
29+
lint_check:
30+
name: Lint check
31+
runs-on: ubuntu-latest
32+
steps:
33+
- name: Checkout repository
34+
uses: actions/checkout@v6
35+
- name: Use Node.js
36+
uses: actions/setup-node@v6
37+
with:
38+
node-version: 24
39+
cache: 'npm'
40+
cache-dependency-path: 'package-lock.json'
41+
- name: Install dependencies
42+
run: npm ci
43+
- name: Lint
44+
run: npm run lint
4645

4746
type_check:
4847
name: Type check

.husky/pre-commit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
echo "pre-commit: typechecking, linting, testing, checking code formatting, checking for unused exports & validating schemas"
2+
3+
4+
npm test
5+
npm run format:check
6+
# We typecheck & lint last when we know our code works
7+
npx tsc --noEmit
8+
npx lint-staged
9+
10+
# Activate once we resolv unused delete old builds
11+
# npm run check-unused
12+
13+
echo "pre-commit: passed"

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
All notable changes to this project will be documented in this file.
44

55
<!-- git-cliff-unreleased-start -->
6+
67
## 0.5.7 - **not yet released**
78

89
### 🚀 Features
@@ -14,8 +15,8 @@ All notable changes to this project will be documented in this file.
1415

1516
- Bump version to test beta release ([1322d31](https://github.com/apify/apify-test-tools/commit/1322d31873b6d43e16a68e97bdc358752f813f79)) by [@metalwarrior665](https://github.com/metalwarrior665)
1617

17-
1818
<!-- git-cliff-unreleased-end -->
19+
1920
# Changelog
2021

2122
## 0.5.5
@@ -61,6 +62,7 @@ feat: feat: add maxRetriesPerRequest test
6162
## 0.2.3
6263

6364
### Lib
65+
6466
- feat: add `runId` option to test tests
6567
- fix: PPE pass won't override overall pass
6668

@@ -89,4 +91,4 @@ feat: feat: add maxRetriesPerRequest test
8991
### Cli
9092

9193
- fix: parsing commits
92-
- feat: add `--workspace` cli option
94+
- feat: add `--workspace` cli option

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Contributing
22

33
The package consists of two parts:
4+
45
- cli located in `bin/`
56
- test library located in `lib`
67

@@ -22,6 +23,7 @@ The package consists of two parts:
2223
### Development setup
2324

2425
1. Clone and build `apify-test-tools` repo:
26+
2527
```sh
2628
git clone git@github.com:apify-projects/apify-test-tools.git
2729
cd apify-test-tools
@@ -30,13 +32,15 @@ npm run build
3032
```
3133

3234
For testing purposes, we use `testing-repo-for-github-actions` repo so that we don't mess with the production repos:
35+
3336
```sh
3437
git clone git@github.com:apify-store/testing-repo-for-github-actions.git
3538
```
3639

3740
#### Working on the CLI
3841

3942
To work on the library, you just need to define `GITHUB_WORKSPACE` to tell the cli where you repo is located:
43+
4044
```sh
4145
export GITHUB_WORKSPACE=../path/to/testing-repo-for-github-actions # path to the repo
4246
npx tsx bin/main.ts --help
@@ -46,6 +50,7 @@ npx tsx bin/main.ts get-commits --target-branch master --source-branch feat/test
4650
#### Working on the library
4751

4852
You need to istall the local version of `apify-test-tools` in your cloned `testing-repo-for-github-actions`:
53+
4954
```sh
5055
npm i -D ../path/to/apify-test-tools
5156
```

README.md

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
## Getting Started
66

77
1. Install the package `npm i -D apify-test-tools`
8-
- because it uses [annotate](https://vitest.dev/guide/test-context.html#annotate), `vitest` version to be at least `3.2.0`
8+
- because it uses [annotate](https://vitest.dev/guide/test-context.html#annotate), `vitest` version to be at least `3.2.0`
99
- make sure that `target` and `module` in your `tsconfig.json`'s `compilerOptions` are set to `ES2022`
1010
2. create test directories: `mkdir -p test/platform/core`
1111
- core (hourly) tests should go to `test/platform/core`
1212
- daily tests should go to `test/platform`
1313
3. setup github worklows TODO
1414

1515
File structure:
16+
1617
```
1718
google-maps
1819
├── actors
1920
└── src
2021
└── test
2122
├── unit
2223
└── platform
23-
├── core <- Core tests need to be inside core directory
24+
├── core <- Core tests need to be inside core directory
2425
│ └── core.test.ts
2526
├── some.test.ts <- Other tests can be defined anywhere inside platform directory
2627
└── some-other.test.ts
@@ -73,7 +74,7 @@ name: PR Test
7374
7475
on:
7576
pull_request:
76-
branches: [ master ]
77+
branches: [master]
7778
7879
jobs:
7980
buildDevelAndTest:
@@ -88,7 +89,7 @@ name: Release latest
8889
8990
on:
9091
push:
91-
branches: [ master ]
92+
branches: [master]
9293
9394
jobs:
9495
buildLatest:
@@ -102,17 +103,18 @@ jobs:
102103

103104
### Test structure
104105

105-
To run the tests concurrently, we had to start the run outside of `it` and then call `await` inside. This is now no longer needed and everything can be inside `it` aka `testActor`.
106+
To run the tests concurrently, we had to start the run outside of `it` and then call `await` inside. This is now no longer needed and everything can be inside `it` aka `testActor`.
106107

107108
Before:
109+
108110
```ts
109111
({ it, xit, run, expect, expectAsync, input, describe }: TestSpecInputs) => {
110112
describe('test', () => {
111113
{
112114
const runPromise = run({ actorId, input })
113115
it('actor test 1', async () => {
114116
const runResult = await runPromise;
115-
117+
116118
// your checks
117119
});
118120
}
@@ -121,25 +123,26 @@ Before:
121123
const runPromise = run({ actorId, input })
122124
it('actor test 2', async () => {
123125
const runResult = await runPromise;
124-
126+
125127
// your checks
126128
});
127-
}
129+
}
128130
});
129131
})
130132
```
131133

132134
After:
135+
133136
```ts
134137
import { describe, testActor } from 'apify-test-tools';
135138
136139
describe('test', () => {
137140
testActor(actorId, 'actor test 1', async ({ expect, run }) => {
138141
const runResult = await run({ input })
139-
142+
140143
// your checks
141144
)};
142-
145+
143146
testActor(actorId, 'actor test 2', async ({ expect, run }) => {
144147
const runResult = await run({ input })
145148
@@ -155,6 +158,7 @@ describe('test', () => {
155158
### Validating basic run attributes
156159

157160
Before:
161+
158162
```ts
159163
await expectAsync(runResult).toHaveStatus('SUCCEEDED');
160164
@@ -164,78 +168,70 @@ await expectAsync(runResult).withLog((log) => {
164168
});
165169
166170
await expectAsync(runResult).withStatistics((stats) => {
167-
expect(stats.requestsRetries)
168-
.withContext(runResult.format('Request retries'))
169-
.toBeLessThan(3);
170-
expect(stats.crawlerRuntimeMillis)
171-
.withContext(runResult.format('Run time'))
172-
.toBeWithinRange(600, 600_000)
173-
})
174-
171+
expect(stats.requestsRetries).withContext(runResult.format('Request retries')).toBeLessThan(3);
172+
expect(stats.crawlerRuntimeMillis).withContext(runResult.format('Run time')).toBeWithinRange(600, 600_000);
173+
});
174+
175175
await expectAsync(runResult).withDataset(({ dataset }) => {
176-
expect(dataset.items?.length)
177-
.withContext(runResult.format('Dataset cleanItemCount'))
178-
.toBe(100);
179-
})
176+
expect(dataset.items?.length).withContext(runResult.format('Dataset cleanItemCount')).toBe(100);
177+
});
180178
```
181179

182180
After:
181+
183182
```ts
184183
await expect(runResult).toFinishWith({
185-
datasetItemCount: 100,
186-
})
184+
datasetItemCount: 100,
185+
});
187186
```
188187

189188
You can also specify a range:
190189

191190
```ts
192191
await expect(runResult).toFinishWith({
193-
datasetItemCount: { min: 80, max: 120 },
194-
})
192+
datasetItemCount: { min: 80, max: 120 },
193+
});
195194
```
196195

197196
Here is full example of what you can validate with `toFinishWith`
198197

199198
```ts
200199
await expect(runResult).toFinishWith({
201-
// These are default
200+
// These are default
202201
status: 'SUCCEEDED',
203202
duration: {
204203
min: 600, // 0.6 sec
205204
max: 600_000, // 10 min
206205
},
207206
failedRequests: 0,
208207
requestsRetries: { max: 3 },
209-
forbiddenLogs: [
210-
'ReferenceError',
211-
'TypeError',
212-
],
213-
214-
// only datasetItemCount is required
215-
datasetItemCount: { min: 80, max: 120 },
216-
208+
forbiddenLogs: ['ReferenceError', 'TypeError'],
209+
210+
// only datasetItemCount is required
211+
datasetItemCount: { min: 80, max: 120 },
212+
217213
// optional
218214
chargedEventCounts: {
219-
'actor-start': 1,
220-
'place-scraped': 9,
221-
},
222-
})
215+
'actor-start': 1,
216+
'place-scraped': 9,
217+
},
218+
});
223219
```
224220

225221
---
226222

227223
### Custom validations
228224

229225
Before:
226+
230227
```ts
231-
expect(place.title)
232-
.withContext(runResult.format(`London Eye's title`))
233-
.toEqual('lastminute.com London Eye')
228+
expect(place.title).withContext(runResult.format(`London Eye's title`)).toEqual('lastminute.com London Eye');
234229
```
235230

236231
After:
232+
237233
```ts
238-
expect(place.title, `London Eye's title`).toEqual('lastminute.com London Eye')
234+
expect(place.title, `London Eye's title`).toEqual('lastminute.com London Eye');
239235
```
240236

241237
---

0 commit comments

Comments
 (0)