apify-test-tools 0.6.4-beta.1 → 0.6.4-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/lib.ts CHANGED
@@ -92,29 +92,24 @@ export const testStandbyActor = <I = any, O = any>(
92
92
 
93
93
  vitestTest.runIf(shouldRun)(name, options, async <T extends TestContext>(context: T) => {
94
94
  const standbyTask = await createStandbyTask(actorName, config.get(actorName)?.buildNumber);
95
- const { annotate } = context;
96
95
  const { expect, ...rest } = context;
97
96
 
98
- // NOTE: we need to wrap `fn` in try-catch so that we can always clean up (delete the task) afterwards
97
+ // NOTE: we wrap `fn` in try/finally so cleanup (deleting the task) always runs afterwards
99
98
  try {
100
99
  await fn({
101
100
  expect: extendExpect(expect),
102
- callStandby: createStartStandbyFn(standbyTask),
101
+ callStandby: createStartStandbyFn(standbyTask, context, name),
103
102
  ...rest,
104
103
  });
105
- } catch {
106
- /* */
107
- }
108
-
109
- const { taskId } = standbyTask;
110
- const runs = (await apifyClient.task(taskId).runs().list()).items;
111
- for (const run of runs) {
112
- const runLink = generateRunLink(run);
113
- await annotate(`${name} - ${runLink}`, 'run_link');
114
- }
115
-
116
- if (taskId) {
117
- await apifyClient.task(taskId).delete();
104
+ } finally {
105
+ const { taskId } = standbyTask;
106
+ // we want to delete the task at the end of the test
107
+ await apifyClient
108
+ .task(taskId)
109
+ .delete()
110
+ .catch((error) => {
111
+ console.error(`Failed to delete standby task "${taskId}": ${error}`);
112
+ });
118
113
  }
119
114
  });
120
115
  };
@@ -129,7 +124,7 @@ export const testTestActor = <T>(
129
124
  expect: extendExpect(expect),
130
125
  // @ts-expect-error: this just to test custom matchers
131
126
  // eslint-disable-next-line @typescript-eslint/no-empty-function
132
- run: () => {},
127
+ run: () => { },
133
128
  ...rest,
134
129
  });
135
130
  });
@@ -138,11 +133,28 @@ export const testTestActor = <T>(
138
133
  export const it = testActor;
139
134
 
140
135
  /**
141
- * Creates a function the accepts input for a standby actor and sends request containing input
136
+ * Creates a function that accepts input for a standby actor and sends a request containing the input
142
137
  * to the task's standby url.
143
138
  */
144
- const createStartStandbyFn = <I, O>(standbyTask: StandbyTask) => {
145
- const { standbyUrl } = standbyTask;
139
+ const createStartStandbyFn = <I, O>(standbyTask: StandbyTask, { annotate }: TestContext, testName: string) => {
140
+ const { standbyUrl, taskId } = standbyTask;
141
+ const annotatedRuns = new Set<string>();
142
+
143
+ // We annotate all the runs of the task, though it will be only one run
144
+ // for most of the tests. To avoid annotating the same run multiple times
145
+ // (in case the test calls the standby more than once), we use `annotatedRuns`
146
+ // set to keep track of which runs have already been annotated.
147
+ const annotateStandbyRuns = async () => {
148
+ const runs = (await apifyClient.task(taskId).runs().list()).items;
149
+ for (const run of runs) {
150
+ if (!annotatedRuns.has(run.id)) {
151
+ const runLink = generateRunLink(run);
152
+ await annotate(`${testName} - ${runLink}`, 'run_link');
153
+ }
154
+ annotatedRuns.add(run.id);
155
+ }
156
+ };
157
+
146
158
  return async ({
147
159
  input,
148
160
  path = '',
@@ -157,6 +169,8 @@ const createStartStandbyFn = <I, O>(standbyTask: StandbyTask) => {
157
169
  body: JSON.stringify(input),
158
170
  });
159
171
 
172
+ await annotateStandbyRuns();
173
+
160
174
  const data = (await response.json()) as O;
161
175
  return {
162
176
  data,
@@ -171,10 +185,6 @@ interface StandbyTask {
171
185
  taskId: string;
172
186
  }
173
187
 
174
- const randomInt = (min: number, max: number) => {
175
- return Math.floor(Math.random() * (max - min + 1)) + min;
176
- };
177
-
178
188
  /**
179
189
  * Creates a task with specific `build` - either `buildNumber` or default.
180
190
  *
@@ -206,16 +216,15 @@ const createStandbyTask = async (actorNameOrId: string, buildNumber?: string): P
206
216
  };
207
217
 
208
218
  try {
209
- const title = `Test task - ${build}:${actorNameOrId}`.slice(0, 62);
219
+ const title = `Test task - ${build}`.slice(0, 62);
220
+ const randomValueLength = 15;
210
221
  // we try to create unique task name containing only `a-z0-9-` characters and at most 63 characters long
211
- const name = `${randomInt(1, 1_000_000)}${title
212
- .toLowerCase()
213
- .replaceAll(/\s+/g, '')
214
- .replaceAll(/[^a-z0-9-]+/g, '-')}`.slice(0, 62);
222
+ const randomValue = Math.random().toString(10).slice(2).padEnd(randomValueLength, '0');
223
+ const name = `test-${randomValue.slice(0, randomValueLength)}`;
215
224
  const newTask = (await apifyClient.tasks().create({
216
225
  actId: actorNameOrId,
217
226
  actorStandby: actorStandbyOptions,
218
- description: `Task for testing standby version ${build}`,
227
+ description: `Task for testing standby version ${build} of actor "${actorNameOrId}"`,
219
228
  title,
220
229
  name,
221
230
  })) as Task & { standbyUrl?: string };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apify-test-tools",
3
- "version": "0.6.4-beta.1",
3
+ "version": "0.6.4-beta.2",
4
4
  "type": "module",
5
5
  "description": "TBD",
6
6
  "repository": {