@graphql-codegen/testing 5.0.3 → 5.0.4-alpha-20260922123710-6d837c7a9c52993dcfdb6690e0a2a63ca0e02400

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/cjs/typescript.js CHANGED
@@ -4,6 +4,7 @@ exports.validateTs = validateTs;
4
4
  exports.compileTs = compileTs;
5
5
  const tslib_1 = require("tslib");
6
6
  const path_1 = require("path");
7
+ const url_1 = require("url");
7
8
  const LZString = tslib_1.__importStar(require("lz-string")); // lz-string is a package which has CJS/ESM issues. So, we cannot do `import { something } from 'lz-string'`
8
9
  const typescript_1 = require("typescript");
9
10
  function validateTs(pluginOutput, options = {
@@ -51,7 +52,8 @@ function validateTs(pluginOutput, options = {
51
52
  ...(pluginOutput.append || []),
52
53
  ]),
53
54
  ].join('\n');
54
- const testFile = `test-file.${isTsx ? 'tsx' : 'ts'}`;
55
+ const cwd = resolveCallerDirectory();
56
+ const testFile = (0, path_1.join)(cwd, `test-file.${isTsx ? 'tsx' : 'ts'}`);
55
57
  const errors = [];
56
58
  if (compileProgram) {
57
59
  const host = (0, typescript_1.createCompilerHost)(options);
@@ -71,7 +73,7 @@ function validateTs(pluginOutput, options = {
71
73
  return filename;
72
74
  },
73
75
  getCurrentDirectory() {
74
- return '';
76
+ return cwd;
75
77
  },
76
78
  getNewLine() {
77
79
  return '\n';
@@ -151,7 +153,8 @@ function compileTs(contents, options = {
151
153
  return;
152
154
  }
153
155
  try {
154
- const testFile = `test-file.${isTsx ? 'tsx' : 'ts'}`;
156
+ const cwd = resolveCallerDirectory();
157
+ const testFile = (0, path_1.join)(cwd, `test-file.${isTsx ? 'tsx' : 'ts'}`);
155
158
  const host = (0, typescript_1.createCompilerHost)(options);
156
159
  const program = (0, typescript_1.createProgram)([testFile], options, {
157
160
  ...host,
@@ -169,7 +172,7 @@ function compileTs(contents, options = {
169
172
  return filename;
170
173
  },
171
174
  getCurrentDirectory() {
172
- return '';
175
+ return cwd;
173
176
  },
174
177
  getNewLine() {
175
178
  return '\n';
@@ -202,6 +205,54 @@ function compileTs(contents, options = {
202
205
  throw e;
203
206
  }
204
207
  }
208
+ /**
209
+ * Resolve the directory of the test file that called into this module.
210
+ *
211
+ * The file `validateTs`/`compileTs` type-check only exists in memory, but TypeScript still needs a
212
+ * real directory to anchor Node module resolution to. The host used to report `''` as the current
213
+ * directory, so nothing resolved at all and every resulting `Cannot find module` was swallowed by
214
+ * the filter below -- leaving generics to silently degrade to `never`.
215
+ *
216
+ * `process.cwd()` is not a usable anchor either: it is the repo root, and under pnpm's isolated
217
+ * layout a package's dependencies live in that package's own `node_modules`. For example
218
+ * `@graphql-typed-document-node/core` is only resolvable from `packages/presets/client`. Anchoring
219
+ * at the caller puts the synthetic file where the imports it contains can actually be found.
220
+ *
221
+ * Falls back to `process.cwd()` when the stack cannot be parsed.
222
+ */
223
+ const resolveCallerDirectory = () => {
224
+ const stack = new Error().stack;
225
+ if (!stack) {
226
+ return process.cwd();
227
+ }
228
+ // Frames look like ` at fn (/abs/path/file.ts:12:34)` or ` at /abs/path/file.ts:12:34`,
229
+ // and vitest's own frames use `file://` URLs. The first frame with a location is this module
230
+ // (this helper); frames from this module are skipped until the calling file is reached.
231
+ let selfFile;
232
+ for (const frame of stack.split('\n').slice(1)) {
233
+ const match = frame.match(/\(?([^()]+):\d+:\d+\)?\s*$/);
234
+ if (!match) {
235
+ continue;
236
+ }
237
+ // `at ` survives the capture for parenthesis-less frames (` at /abs/path.ts:1:2`).
238
+ const location = match[1].trim().replace(/^at\s+/, '');
239
+ let file;
240
+ try {
241
+ file = location.startsWith('file://') ? (0, url_1.fileURLToPath)(location) : location;
242
+ }
243
+ catch {
244
+ continue;
245
+ }
246
+ if (selfFile === undefined) {
247
+ selfFile = file;
248
+ continue;
249
+ }
250
+ if (file !== selfFile) {
251
+ return (0, path_1.dirname)(file);
252
+ }
253
+ }
254
+ return process.cwd();
255
+ };
205
256
  /**
206
257
  * Resolve the `@types` directory that actually contains `@types/node`.
207
258
  *
package/esm/typescript.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { dirname, join } from 'path';
2
+ import { fileURLToPath } from 'url';
2
3
  import * as LZString from 'lz-string'; // lz-string is a package which has CJS/ESM issues. So, we cannot do `import { something } from 'lz-string'`
3
4
  import { createCompilerHost, createProgram, createSourceFile, flattenDiagnosticMessageText, JsxEmit, ModuleKind, ModuleResolutionKind, ScriptKind, ScriptTarget, version as tsVersion, } from 'typescript';
4
5
  export function validateTs(pluginOutput, options = {
@@ -46,7 +47,8 @@ export function validateTs(pluginOutput, options = {
46
47
  ...(pluginOutput.append || []),
47
48
  ]),
48
49
  ].join('\n');
49
- const testFile = `test-file.${isTsx ? 'tsx' : 'ts'}`;
50
+ const cwd = resolveCallerDirectory();
51
+ const testFile = join(cwd, `test-file.${isTsx ? 'tsx' : 'ts'}`);
50
52
  const errors = [];
51
53
  if (compileProgram) {
52
54
  const host = createCompilerHost(options);
@@ -66,7 +68,7 @@ export function validateTs(pluginOutput, options = {
66
68
  return filename;
67
69
  },
68
70
  getCurrentDirectory() {
69
- return '';
71
+ return cwd;
70
72
  },
71
73
  getNewLine() {
72
74
  return '\n';
@@ -146,7 +148,8 @@ export function compileTs(contents, options = {
146
148
  return;
147
149
  }
148
150
  try {
149
- const testFile = `test-file.${isTsx ? 'tsx' : 'ts'}`;
151
+ const cwd = resolveCallerDirectory();
152
+ const testFile = join(cwd, `test-file.${isTsx ? 'tsx' : 'ts'}`);
150
153
  const host = createCompilerHost(options);
151
154
  const program = createProgram([testFile], options, {
152
155
  ...host,
@@ -164,7 +167,7 @@ export function compileTs(contents, options = {
164
167
  return filename;
165
168
  },
166
169
  getCurrentDirectory() {
167
- return '';
170
+ return cwd;
168
171
  },
169
172
  getNewLine() {
170
173
  return '\n';
@@ -197,6 +200,54 @@ export function compileTs(contents, options = {
197
200
  throw e;
198
201
  }
199
202
  }
203
+ /**
204
+ * Resolve the directory of the test file that called into this module.
205
+ *
206
+ * The file `validateTs`/`compileTs` type-check only exists in memory, but TypeScript still needs a
207
+ * real directory to anchor Node module resolution to. The host used to report `''` as the current
208
+ * directory, so nothing resolved at all and every resulting `Cannot find module` was swallowed by
209
+ * the filter below -- leaving generics to silently degrade to `never`.
210
+ *
211
+ * `process.cwd()` is not a usable anchor either: it is the repo root, and under pnpm's isolated
212
+ * layout a package's dependencies live in that package's own `node_modules`. For example
213
+ * `@graphql-typed-document-node/core` is only resolvable from `packages/presets/client`. Anchoring
214
+ * at the caller puts the synthetic file where the imports it contains can actually be found.
215
+ *
216
+ * Falls back to `process.cwd()` when the stack cannot be parsed.
217
+ */
218
+ const resolveCallerDirectory = () => {
219
+ const stack = new Error().stack;
220
+ if (!stack) {
221
+ return process.cwd();
222
+ }
223
+ // Frames look like ` at fn (/abs/path/file.ts:12:34)` or ` at /abs/path/file.ts:12:34`,
224
+ // and vitest's own frames use `file://` URLs. The first frame with a location is this module
225
+ // (this helper); frames from this module are skipped until the calling file is reached.
226
+ let selfFile;
227
+ for (const frame of stack.split('\n').slice(1)) {
228
+ const match = frame.match(/\(?([^()]+):\d+:\d+\)?\s*$/);
229
+ if (!match) {
230
+ continue;
231
+ }
232
+ // `at ` survives the capture for parenthesis-less frames (` at /abs/path.ts:1:2`).
233
+ const location = match[1].trim().replace(/^at\s+/, '');
234
+ let file;
235
+ try {
236
+ file = location.startsWith('file://') ? fileURLToPath(location) : location;
237
+ }
238
+ catch {
239
+ continue;
240
+ }
241
+ if (selfFile === undefined) {
242
+ selfFile = file;
243
+ continue;
244
+ }
245
+ if (file !== selfFile) {
246
+ return dirname(file);
247
+ }
248
+ }
249
+ return process.cwd();
250
+ };
200
251
  /**
201
252
  * Resolve the `@types` directory that actually contains `@types/node`.
202
253
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/testing",
3
- "version": "5.0.3",
3
+ "version": "5.0.4-alpha-20260922123710-6d837c7a9c52993dcfdb6690e0a2a63ca0e02400",
4
4
  "description": "GraphQL Codegen Testing Utils",
5
5
  "peerDependencies": {
6
6
  "jest-diff": "^23.0.0 || ^24.0.0 || ^25.0.0 || ^26.0.0 || ^27.0.0 || ^28.0.0 || ^29.0.0 || ^30.0.0",