@warp-drive/holodeck 0.0.0-alpha.85 → 0.0.0-alpha.87

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.
Files changed (2) hide show
  1. package/README.md +146 -0
  2. package/package.json +6 -6
package/README.md CHANGED
@@ -94,6 +94,152 @@ and `brotli` minification in a way that can be replayed over and over again.
94
94
 
95
95
  Basically, pay the cost when you write the test. Forever after skip the cost until you need to edit the test again.
96
96
 
97
+ ## Setup
98
+
99
+ ### Use with WarpDrive
100
+
101
+ First, you will need to add the holodeck handler to the request manager chain prior to `Fetch` (or any equivalent handler that proceeds to network).
102
+
103
+ For instance:
104
+
105
+ ```ts
106
+ import RequestManager from '@ember-data/request';
107
+ import Fetch from '@ember-data/request/fetch';
108
+ import { MockServerHandler } from '@warp-drive/holodeck';
109
+
110
+ const manager = new RequestManager();
111
+ manager.use([new MockServerHandler(testContext), Fetch]);
112
+ ```
113
+
114
+ From within a test this might look like:
115
+
116
+ ```ts
117
+ import RequestManager from '@ember-data/request';
118
+ import Fetch from '@ember-data/request/fetch';
119
+ import { MockServerHandler } from '@warp-drive/holodeck';
120
+ import { module, test } from 'qunit';
121
+
122
+ module('my module', function() {
123
+ test('my test', async function() {
124
+ const manager = new RequestManager();
125
+ manager.use([new MockServerHandler(this), Fetch]);
126
+ });
127
+ });
128
+ ```
129
+
130
+ Next, you will need to configure holodeck to understand your tests contexts. For qunit and diagnostic
131
+ in a project using Ember this is typically done in `tests/test-helper.js`
132
+
133
+ #### With Diagnostic
134
+
135
+ ```ts
136
+ import { setupGlobalHooks } from '@warp-drive/diagnostic';
137
+ import { setConfig, setTestId } from '@warp-drive/holodeck';
138
+
139
+ // if not proxying the port / set port to the correct value here
140
+ const MockHost = `https://${window.location.hostname}:${Number(window.location.port) + 1}`;
141
+
142
+ setConfig({ host: MockHost });
143
+
144
+ setupGlobalHooks((hooks) => {
145
+ hooks.beforeEach(function (assert) {
146
+ setTestId(this, assert.test.testId);
147
+ });
148
+ hooks.afterEach(function () {
149
+ setTestId(this, null);
150
+ });
151
+ });
152
+ ```
153
+
154
+ #### With QUnit
155
+
156
+ ```ts
157
+ import * as QUnit from 'qunit';
158
+ import { setConfig, setTestId } from '@warp-drive/holodeck';
159
+
160
+ // if not proxying the port / set port to the correct value here
161
+ const MockHost = `https://${window.location.hostname}:${Number(window.location.port) + 1}`;
162
+
163
+ setConfig({ host: MockHost });
164
+
165
+ QUnit.hooks.beforeEach(function (assert) {
166
+ setTestId(assert.test.testId);
167
+ });
168
+ QUnit.hooks.afterEach(function (assert) {
169
+ setTestId(null);
170
+ });
171
+ ```
172
+
173
+ ### Testem
174
+
175
+ You can integrate holodeck with Testem using testem's [async config capability](https://github.com/testem/testem/blob/master/docs/config_file.md#returning-a-promise-from-testemjs):
176
+
177
+ ```ts
178
+ module.exports = async function () {
179
+ const holodeck = (await import('@warp-drive/holodeck')).default;
180
+ await holodeck.launchProgram({
181
+ port: 7373,
182
+ });
183
+
184
+ process.on('beforeExit', async () => {
185
+ await holodeck.endProgram();
186
+ });
187
+
188
+ return {
189
+ // ... testem config
190
+ };
191
+ };
192
+ ```
193
+
194
+ If you need the API mock to run on the same port as the test suite, you can use Testem's [API Proxy](https://github.com/testem/testem/tree/master?tab=readme-ov-file#api-proxy)
195
+
196
+ ```ts
197
+ module.exports = async function () {
198
+ const holodeck = (await import('@warp-drive/holodeck')).default;
199
+ await holodeck.launchProgram({
200
+ port: 7373,
201
+ });
202
+
203
+ process.on('beforeExit', async () => {
204
+ await holodeck.endProgram();
205
+ });
206
+
207
+ return {
208
+ "proxies": {
209
+ "/api": {
210
+ // holodeck always runs on https
211
+ // the proxy is transparent so this means /api/v1 will route to https://localhost:7373/api/v1
212
+ "target": "https://localhost:7373",
213
+ // "onlyContentTypes": ["xml", "json"],
214
+ // if test suite is on http, set this to false
215
+ // "secure": false,
216
+ },
217
+ }
218
+ };
219
+ };
220
+ ```
221
+
222
+ ### Diagnostic
223
+
224
+ holodeck can be launched and cleaned up using the lifecycle hooks in the launch config
225
+ for diagnostic in `diagnostic.js`:
226
+
227
+ ```ts
228
+ import launch from '@warp-drive/diagnostic/server/default-setup.js';
229
+ import holodeck from '@warp-drive/holodeck';
230
+
231
+ await launch({
232
+ async setup(options) {
233
+ await holodeck.launchProgram({
234
+ port: options.port + 1,
235
+ });
236
+ },
237
+ async cleanup() {
238
+ await holodeck.endProgram();
239
+ },
240
+ });
241
+ ```
242
+
97
243
  ### ♥️ Credits
98
244
 
99
245
  <details>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@warp-drive/holodeck",
3
3
  "description": "⚡️ Simple, Fast HTTP Mocking for Tests",
4
- "version": "0.0.0-alpha.85",
4
+ "version": "0.0.0-alpha.87",
5
5
  "license": "MIT",
6
6
  "author": "Chris Thoburn <runspired@users.noreply.github.com>",
7
7
  "repository": {
@@ -44,8 +44,8 @@
44
44
  "sync-hardlinks": "bun run sync-dependencies-meta-injected"
45
45
  },
46
46
  "peerDependencies": {
47
- "@ember-data/request": "5.4.0-alpha.99",
48
- "@warp-drive/core-types": "0.0.0-alpha.85"
47
+ "@ember-data/request": "5.4.0-alpha.101",
48
+ "@warp-drive/core-types": "0.0.0-alpha.87"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@babel/core": "^7.24.5",
@@ -53,9 +53,9 @@
53
53
  "@babel/preset-env": "^7.24.5",
54
54
  "@babel/preset-typescript": "^7.24.1",
55
55
  "@babel/runtime": "^7.24.5",
56
- "@ember-data/request": "5.4.0-alpha.99",
57
- "@warp-drive/core-types": "0.0.0-alpha.85",
58
- "@warp-drive/internal-config": "5.4.0-alpha.99",
56
+ "@ember-data/request": "5.4.0-alpha.101",
57
+ "@warp-drive/core-types": "0.0.0-alpha.87",
58
+ "@warp-drive/internal-config": "5.4.0-alpha.101",
59
59
  "pnpm-sync-dependencies-meta-injected": "0.0.14",
60
60
  "typescript": "^5.4.5",
61
61
  "vite": "^5.2.11"