@orieljs/testing 0.1.0-alpha.3 → 0.1.0-alpha.4

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/dist/index.d.ts CHANGED
@@ -362,6 +362,12 @@ declare class HttpClient {
362
362
  request(method: string, uri: string, options?: RequestOptions): Promise<TestResponse>;
363
363
  /** Runs `fn` in a scope prepared like a request's (carried session, actingAs users), e.g. for assertAuthenticated(). */
364
364
  inScope<T>(fn: (scope: Container) => T | Promise<T>): Promise<T>;
365
+ /**
366
+ * Runs `run` in a request scope like this client's requests have (a request for `method uri` bound, the carried
367
+ * session, actingAs users), then carries the session and the users on, as after a request. Live.test() runs its
368
+ * mount and updates through it.
369
+ */
370
+ withinRequest<T>(run: () => Promise<T>, method?: string, uri?: string): Promise<T>;
365
371
  /** Runs the request through the app's real HttpKernel, in a request scope this client owns. */
366
372
  protected send(request: Request): Promise<TestResponse>;
367
373
  /** Prepares the request scope: recorders, the carried session and actingAs users. */
@@ -435,6 +441,11 @@ declare abstract class InteractsWithApplication {
435
441
  protected defineEnvironment(_app: Application): void | Promise<void>;
436
442
  setUp(): Promise<void>;
437
443
  tearDown(): Promise<void>;
444
+ /**
445
+ * Laravel's withoutVite(): in this test's requests vite([...]) and viteReactRefresh() render nothing, so feature
446
+ * tests of pages pass before `vite build` has written public/build/manifest.json.
447
+ */
448
+ withoutVite(): this;
438
449
  /** Runs in tearDown(), before the application is thrown away (in registration order). */
439
450
  beforeApplicationDestroyed(callback: Callback): void;
440
451
  }
@@ -446,6 +457,8 @@ declare abstract class MakesHttpRequests extends InteractsWithApplication {
446
457
  /** The current test's HTTP client (created on first use). */
447
458
  protected get http(): HttpClient;
448
459
  protected createHttpClient(): HttpClient;
460
+ /** Live.test() runs in this test's request scope (Live reads the same token: no import between the packages). */
461
+ setUp(): Promise<void>;
449
462
  tearDown(): Promise<void>;
450
463
  withHeaders(headers: Headers$1): this;
451
464
  withHeader(name: string, value: string): this;
package/dist/index.js CHANGED
@@ -274,7 +274,7 @@ var DatabaseTransactions = {
274
274
  };
275
275
 
276
276
  // Concerns/MakesHttpRequests.ts
277
- import { AuthFactory as AuthFactory2 } from "@orieljs/contracts";
277
+ import { AuthFactory as AuthFactory2, token } from "@orieljs/contracts";
278
278
 
279
279
  // Http/HttpClient.ts
280
280
  import {
@@ -283,9 +283,11 @@ import {
283
283
  Encrypter as Encrypter2,
284
284
  ExceptionHandler,
285
285
  HttpKernel,
286
+ HttpRequest,
286
287
  Session,
287
288
  ViewFactory
288
289
  } from "@orieljs/contracts";
290
+ import { IncomingRequest } from "@orieljs/http";
289
291
 
290
292
  // environment.ts
291
293
  var TESTING_ENVIRONMENT = Object.freeze({
@@ -1444,6 +1446,31 @@ var HttpClient = class {
1444
1446
  return fn(scope);
1445
1447
  });
1446
1448
  }
1449
+ /**
1450
+ * Runs `run` in a request scope like this client's requests have (a request for `method uri` bound, the carried
1451
+ * session, actingAs users), then carries the session and the users on, as after a request. Live.test() runs its
1452
+ * mount and updates through it.
1453
+ */
1454
+ async withinRequest(run, method = "GET", uri = "/") {
1455
+ await this.app.boot();
1456
+ const request = this.buildRequest(method, uri, {});
1457
+ setClientAddress(request, "127.0.0.1");
1458
+ const scope = this.app.createScope();
1459
+ const context = { request, app: this.app, client: this };
1460
+ const result = await scope.run(async () => {
1461
+ this.beforeRequest(scope, context);
1462
+ scope.instance(HttpRequest, new IncomingRequest(request));
1463
+ if (scope.bound(AuthFactory)) {
1464
+ try {
1465
+ await scope.make(AuthFactory).guard().check();
1466
+ } catch {
1467
+ }
1468
+ }
1469
+ return run();
1470
+ });
1471
+ await scope.run(() => this.afterRequest(scope, context));
1472
+ return result;
1473
+ }
1447
1474
  /** Runs the request through the app's real HttpKernel, in a request scope this client owns. */
1448
1475
  async send(request) {
1449
1476
  const app = this.app;
@@ -1588,7 +1615,7 @@ import {
1588
1615
  DatabaseManager as DatabaseManager2
1589
1616
  } from "@orieljs/contracts";
1590
1617
  import { Container } from "@orieljs/container";
1591
- import { Application as FoundationApplication } from "@orieljs/foundation";
1618
+ import { Application as FoundationApplication, Vite, ViteHtml } from "@orieljs/foundation";
1592
1619
  var globalHooks = () => {
1593
1620
  const g = globalThis;
1594
1621
  return typeof g.beforeEach === "function" && typeof g.afterEach === "function" ? { beforeEach: g.beforeEach, afterEach: g.afterEach } : void 0;
@@ -1669,11 +1696,43 @@ var InteractsWithApplication = class {
1669
1696
  if (global === app) Container.setInstance(void 0);
1670
1697
  }
1671
1698
  }
1699
+ /**
1700
+ * Laravel's withoutVite(): in this test's requests vite([...]) and viteReactRefresh() render nothing, so feature
1701
+ * tests of pages pass before `vite build` has written public/build/manifest.json.
1702
+ */
1703
+ withoutVite() {
1704
+ const app = this.app;
1705
+ app.scoped(Vite, () => new WithoutVite(app));
1706
+ return this;
1707
+ }
1672
1708
  /** Runs in tearDown(), before the application is thrown away (in registration order). */
1673
1709
  beforeApplicationDestroyed(callback) {
1674
1710
  this.destroying.push(callback);
1675
1711
  }
1676
1712
  };
1713
+ var WithoutVite = class extends Vite {
1714
+ tags() {
1715
+ return new ViteHtml("");
1716
+ }
1717
+ reactRefresh() {
1718
+ return void 0;
1719
+ }
1720
+ asset() {
1721
+ return "";
1722
+ }
1723
+ content() {
1724
+ return "";
1725
+ }
1726
+ manifestHash() {
1727
+ return void 0;
1728
+ }
1729
+ preloadedAssets() {
1730
+ return {};
1731
+ }
1732
+ toHtml() {
1733
+ return "";
1734
+ }
1735
+ };
1677
1736
  async function disconnectAll(db) {
1678
1737
  const manager = db;
1679
1738
  if (typeof manager.disconnectAll === "function") await manager.disconnectAll();
@@ -1690,6 +1749,11 @@ var MakesHttpRequests = class extends InteractsWithApplication {
1690
1749
  createHttpClient() {
1691
1750
  return new HttpClient(this.app);
1692
1751
  }
1752
+ /** Live.test() runs in this test's request scope (Live reads the same token: no import between the packages). */
1753
+ async setUp() {
1754
+ await super.setUp();
1755
+ this.app.instance(token("Testing.RequestScope"), (run) => this.http.withinRequest(run));
1756
+ }
1693
1757
  async tearDown() {
1694
1758
  try {
1695
1759
  await super.tearDown();
@@ -1705,8 +1769,8 @@ var MakesHttpRequests = class extends InteractsWithApplication {
1705
1769
  this.http.headers[name] = value;
1706
1770
  return this;
1707
1771
  }
1708
- withToken(token, type = "Bearer") {
1709
- return this.withHeader("Authorization", `${type} ${token}`);
1772
+ withToken(token2, type = "Bearer") {
1773
+ return this.withHeader("Authorization", `${type} ${token2}`);
1710
1774
  }
1711
1775
  withBasicAuth(username, password) {
1712
1776
  return this.withToken(Buffer.from(`${username}:${password}`).toString("base64"), "Basic");
@@ -1899,13 +1963,13 @@ var TestCase = class extends InteractsWithConsole {
1899
1963
  // Fakes/support.ts
1900
1964
  import { currentContainer } from "@orieljs/container";
1901
1965
  import { facade } from "@orieljs/support";
1902
- function install(token, fake) {
1903
- facade(token).swap(fake);
1966
+ function install(token2, fake) {
1967
+ facade(token2).swap(fake);
1904
1968
  return fake;
1905
1969
  }
1906
- function bound(token) {
1970
+ function bound(token2) {
1907
1971
  const container = currentContainer();
1908
- return container !== void 0 && container.bound(token) ? container.make(token) : void 0;
1972
+ return container !== void 0 && container.bound(token2) ? container.make(token2) : void 0;
1909
1973
  }
1910
1974
  function nameOf(target2) {
1911
1975
  if (typeof target2 === "string") return target2;