Skip to content

Commit 2cf9bec

Browse files
dosachadosacha
authored andcommitted
test: cover isCompositeType identity across package entrypoints
1 parent 82da713 commit 2cf9bec

2 files changed

Lines changed: 133 additions & 8 deletions

File tree

Lines changed: 116 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
import assert from 'node:assert';
22

3-
import { GraphQLObjectType as ESMGraphQLObjectType } from 'graphql';
3+
import {
4+
GraphQLObjectType as ESMGraphQLObjectType,
5+
GraphQLString as ESMGraphQLString,
6+
isCompositeType as ESMIsCompositeType,
7+
} from 'graphql';
8+
import {
9+
GraphQLObjectType as ESMTypeGraphQLObjectType,
10+
GraphQLString as ESMTypeGraphQLString,
11+
isCompositeType as ESMTypeIsCompositeType,
12+
} from 'graphql/type';
413

5-
import { CJSGraphQLObjectType, cjsPath } from './cjs-importer.cjs';
14+
import {
15+
CJSGraphQLObjectType,
16+
CJSGraphQLString,
17+
CJSIsCompositeType,
18+
cjsPath,
19+
CJSTypeGraphQLObjectType,
20+
CJSTypeGraphQLString,
21+
CJSTypeIsCompositeType,
22+
cjsTypePath,
23+
} from './cjs-importer.cjs';
624

725
const moduleSync = process.env.MODULE_SYNC === 'true';
826
const expectedExtension = moduleSync ? '.mjs' : '.js';
9-
assert.ok(
10-
cjsPath.endsWith(expectedExtension),
11-
`require('graphql') should resolve to a file with extension "${expectedExtension}", but got "${cjsPath}".`,
12-
);
27+
const resolvedPaths = [
28+
["require('graphql')", cjsPath],
29+
["require('graphql/type')", cjsTypePath],
30+
];
31+
32+
for (const [specifier, resolvedPath] of resolvedPaths) {
33+
assert.ok(
34+
resolvedPath.endsWith(expectedExtension),
35+
`${specifier} should resolve to a file with extension "${expectedExtension}", but got "${resolvedPath}".`,
36+
);
37+
}
1338

1439
const isSameModule = ESMGraphQLObjectType === CJSGraphQLObjectType;
1540
assert.strictEqual(
@@ -18,4 +43,88 @@ assert.strictEqual(
1843
'ESM and CJS imports should be the same module instances.',
1944
);
2045

21-
console.log('Module identity and path checks passed.');
46+
assert.strictEqual(
47+
ESMGraphQLObjectType,
48+
ESMTypeGraphQLObjectType,
49+
'Root and graphql/type ESM imports should use the same GraphQLObjectType instance.',
50+
);
51+
assert.strictEqual(
52+
CJSGraphQLObjectType,
53+
CJSTypeGraphQLObjectType,
54+
'Root and graphql/type CJS imports should use the same GraphQLObjectType instance.',
55+
);
56+
assert.strictEqual(
57+
ESMIsCompositeType,
58+
ESMTypeIsCompositeType,
59+
'Root and graphql/type ESM imports should use the same isCompositeType predicate.',
60+
);
61+
assert.strictEqual(
62+
CJSIsCompositeType,
63+
CJSTypeIsCompositeType,
64+
'Root and graphql/type CJS imports should use the same isCompositeType predicate.',
65+
);
66+
assert.strictEqual(
67+
ESMIsCompositeType,
68+
CJSIsCompositeType,
69+
'ESM and CJS imports should use the same isCompositeType predicate.',
70+
);
71+
assert.strictEqual(
72+
ESMGraphQLString,
73+
ESMTypeGraphQLString,
74+
'Root and graphql/type ESM imports should use the same GraphQLString instance.',
75+
);
76+
assert.strictEqual(
77+
CJSGraphQLString,
78+
CJSTypeGraphQLString,
79+
'Root and graphql/type CJS imports should use the same GraphQLString instance.',
80+
);
81+
82+
const objectTypes = [
83+
[
84+
'ESM root GraphQLObjectType',
85+
new ESMGraphQLObjectType({
86+
name: 'ESMRootObjectType',
87+
fields: { field: { type: ESMGraphQLString } },
88+
}),
89+
],
90+
[
91+
'ESM graphql/type GraphQLObjectType',
92+
new ESMTypeGraphQLObjectType({
93+
name: 'ESMTypeObjectType',
94+
fields: { field: { type: ESMTypeGraphQLString } },
95+
}),
96+
],
97+
[
98+
'CJS root GraphQLObjectType',
99+
new CJSGraphQLObjectType({
100+
name: 'CJSRootObjectType',
101+
fields: { field: { type: CJSGraphQLString } },
102+
}),
103+
],
104+
[
105+
'CJS graphql/type GraphQLObjectType',
106+
new CJSTypeGraphQLObjectType({
107+
name: 'CJSTypeObjectType',
108+
fields: { field: { type: CJSTypeGraphQLString } },
109+
}),
110+
],
111+
];
112+
113+
const predicates = [
114+
['ESM root isCompositeType', ESMIsCompositeType],
115+
['ESM graphql/type isCompositeType', ESMTypeIsCompositeType],
116+
['CJS root isCompositeType', CJSIsCompositeType],
117+
['CJS graphql/type isCompositeType', CJSTypeIsCompositeType],
118+
];
119+
120+
for (const [objectTypeLabel, objectType] of objectTypes) {
121+
for (const [predicateLabel, isCompositeType] of predicates) {
122+
assert.strictEqual(
123+
isCompositeType(objectType),
124+
true,
125+
`${predicateLabel} should return true for ${objectTypeLabel}.`,
126+
);
127+
}
128+
}
129+
130+
console.log('Module identity, subpath identity, and path checks passed.');
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
'use strict';
22

3-
const { GraphQLObjectType } = require('graphql');
3+
const {
4+
GraphQLObjectType,
5+
GraphQLString,
6+
isCompositeType,
7+
} = require('graphql');
8+
const {
9+
GraphQLObjectType: TypeGraphQLObjectType,
10+
GraphQLString: TypeGraphQLString,
11+
isCompositeType: typeIsCompositeType,
12+
} = require('graphql/type');
413

514
const cjsPath = require.resolve('graphql');
15+
const cjsTypePath = require.resolve('graphql/type');
616

717
// eslint-disable-next-line import/no-commonjs
818
module.exports = {
919
CJSGraphQLObjectType: GraphQLObjectType,
20+
CJSGraphQLString: GraphQLString,
21+
CJSIsCompositeType: isCompositeType,
22+
CJSTypeGraphQLObjectType: TypeGraphQLObjectType,
23+
CJSTypeGraphQLString: TypeGraphQLString,
24+
CJSTypeIsCompositeType: typeIsCompositeType,
1025
cjsPath,
26+
cjsTypePath,
1127
};

0 commit comments

Comments
 (0)