alchemy 0.8.1 → 0.9.0

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/src/test/bun.ts CHANGED
@@ -4,8 +4,7 @@ import { afterAll, beforeAll, it } from "bun:test";
4
4
  import path from "node:path";
5
5
  import { alchemy } from "../alchemy";
6
6
  import { R2RestStateStore } from "../cloudflare";
7
- import { destroy } from "../destroy";
8
- import type { Scope } from "../scope";
7
+ import { Scope } from "../scope";
9
8
  import type { StateStoreType } from "../state";
10
9
 
11
10
  /**
@@ -26,12 +25,6 @@ alchemy.test = test;
26
25
  * Options for configuring test behavior
27
26
  */
28
27
  export interface TestOptions {
29
- /**
30
- * Whether to automatically destroy resources after test.
31
- * @default true.
32
- */
33
- destroy?: boolean;
34
-
35
28
  /**
36
29
  * Whether to suppress logging output.
37
30
  * @default false.
@@ -121,7 +114,6 @@ type test = {
121
114
  * ```
122
115
  */
123
116
  export function test(meta: ImportMeta, defaultOptions?: TestOptions): test {
124
- let isFailed = false;
125
117
  defaultOptions = defaultOptions ?? {};
126
118
  if (
127
119
  defaultOptions.stateStore === undefined &&
@@ -146,37 +138,21 @@ export function test(meta: ImportMeta, defaultOptions?: TestOptions): test {
146
138
  };
147
139
 
148
140
  // Create local test scope based on filename
149
- const localTestScope = alchemy.scope(
150
- `${defaultOptions.prefix ? `${defaultOptions.prefix}-` : ""}${path.basename(meta.filename)}`,
151
- {
152
- // parent: globalTestScope,
153
- stateStore: defaultOptions?.stateStore,
154
- }
155
- );
156
- test.scope = localTestScope;
141
+ const scope = new Scope({
142
+ scopeName: `${defaultOptions.prefix ? `${defaultOptions.prefix}-` : ""}${path.basename(meta.filename)}`,
143
+ // parent: globalTestScope,
144
+ stateStore: defaultOptions?.stateStore,
145
+ });
157
146
 
158
147
  test.beforeAll = (fn: (scope: Scope) => Promise<void>) => {
159
- return beforeAll(async () => {
160
- test.scope.enter();
161
- await fn(test.scope);
162
- });
148
+ return beforeAll(() => scope.run(() => fn(scope)));
163
149
  };
164
150
 
165
151
  test.afterAll = (fn: (scope: Scope) => Promise<void>) => {
166
- return afterAll(async () => {
167
- test.scope.enter();
168
- await fn(test.scope);
169
- });
152
+ return afterAll(() => scope.run(() => fn(scope)));
170
153
  };
171
154
 
172
- // Clean up test scope after all tests complete
173
- afterAll(async () => {
174
- if (defaultOptions?.destroy !== false && !isFailed) {
175
- await alchemy.destroy(test.scope);
176
- }
177
- });
178
-
179
- return test as any;
155
+ return test as test;
180
156
 
181
157
  function test(
182
158
  ...args:
@@ -204,7 +180,6 @@ export function test(meta: ImportMeta, defaultOptions?: TestOptions): test {
204
180
 
205
181
  // Merge options with defaults
206
182
  const options: TestOptions = {
207
- destroy: true,
208
183
  quiet: false,
209
184
  password: "test-password",
210
185
  ...spread(defaultOptions),
@@ -213,32 +188,26 @@ export function test(meta: ImportMeta, defaultOptions?: TestOptions): test {
213
188
 
214
189
  const fn = typeof args[1] === "function" ? args[1] : args[2]!;
215
190
 
216
- return alchemy.run(
191
+ return it(
217
192
  testName,
218
- {
219
- ...options,
220
- parent: localTestScope,
221
- },
222
- async (scope) => {
223
- return it(
193
+ async () =>
194
+ alchemy.run(
224
195
  testName,
225
- async () => {
226
- // Enter test scope since bun calls from different scope
227
- scope.enter();
196
+ {
197
+ ...options,
198
+ parent: scope,
199
+ },
200
+ async (scope) => {
228
201
  try {
229
- await fn(scope);
202
+ // Enter test scope since bun calls from different scope
203
+ await scope.run(() => fn(scope));
230
204
  } catch (err) {
231
205
  console.error(err);
232
206
  throw err;
233
- } finally {
234
- if (options.destroy !== false) {
235
- await destroy(scope);
236
- }
237
207
  }
238
- },
239
- timeout
240
- );
241
- }
208
+ }
209
+ ),
210
+ timeout
242
211
  );
243
212
  }
244
213
  }