Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d62688e

Browse files
committedNov 4, 2021
add node-esm integrationTests
1 parent 8173caa commit d62688e

File tree

5 files changed

+86
-7
lines changed

5 files changed

+86
-7
lines changed
 

‎integrationTests/integration-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ describe('Integration Tests', () => {
2727
path.join(tmpDir, 'graphql.tgz'),
2828
);
2929

30+
const esmDistDir = path.resolve('./npmEsmDist');
31+
const esmArchiveName = exec(`npm --quiet pack ${esmDistDir}`, {
32+
cwd: tmpDir,
33+
});
34+
35+
fs.renameSync(
36+
path.join(tmpDir, esmArchiveName),
37+
path.join(tmpDir, 'graphql-esm.tgz'),
38+
);
39+
3040
function testOnNodeProject(projectName) {
3141
const projectPath = path.join(__dirname, projectName);
3242

@@ -45,4 +55,5 @@ describe('Integration Tests', () => {
4555
testOnNodeProject('ts');
4656
testOnNodeProject('node');
4757
testOnNodeProject('webpack');
58+
testOnNodeProject('node-esm');
4859
});

‎integrationTests/node-esm/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { deepStrictEqual, strictEqual } from 'assert';
2+
import { readFileSync } from 'fs';
3+
4+
// Regular import
5+
import { graphqlSync } from 'graphql';
6+
// import with explicit extension
7+
import { version } from 'graphql/version.js';
8+
// _/index.js import
9+
import { buildSchema } from 'graphql/utilities';
10+
// import without explicit extension
11+
import { isPromise } from 'graphql/jsutils/isPromise';
12+
13+
deepStrictEqual(
14+
version,
15+
JSON.parse(readFileSync('./node_modules/graphql/package.json')).version,
16+
);
17+
18+
const schema = buildSchema('type Query { hello: String }');
19+
20+
const result = graphqlSync({
21+
schema,
22+
source: '{ hello }',
23+
rootValue: { hello: 'world' },
24+
});
25+
26+
deepStrictEqual(result, {
27+
data: {
28+
__proto__: null,
29+
hello: 'world',
30+
},
31+
});
32+
33+
strictEqual(isPromise(Promise.resolve()), true);
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"type": "module",
3+
"description": "graphql-js should work on all supported node versions with ESM",
4+
"scripts": {
5+
"test": "node test.js"
6+
},
7+
"dependencies": {
8+
"graphql": "file:../graphql-esm.tgz",
9+
"node-12": "npm:node@12.x.x",
10+
"node-14": "npm:node@14.x.x",
11+
"node-16": "npm:node@16.x.x"
12+
}
13+
}

‎integrationTests/node-esm/test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { execSync } from 'child_process';
2+
import { createRequire } from 'module';
3+
import { dirname, join } from 'path';
4+
import { fileURLToPath } from 'url';
5+
6+
const { dependencies } = createRequire(import.meta.url)('./package.json');
7+
8+
const nodeVersions = Object.keys(dependencies)
9+
.filter((pkg) => pkg.startsWith('node-'))
10+
.sort((a, b) => b.localeCompare(a));
11+
12+
for (const version of nodeVersions) {
13+
console.log(`Testing on ${version} ...`);
14+
15+
const nodePath = join(
16+
dirname(fileURLToPath(import.meta.url)),
17+
'node_modules',
18+
version,
19+
'bin/node',
20+
);
21+
execSync(nodePath + ' index.js', { stdio: 'inherit' });
22+
}

‎resources/build-npm.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ if (require.main === module) {
3232
const destPath = path.join(distDirectory, filepath);
3333

3434
fs.mkdirSync(path.dirname(destPath), { recursive: true });
35-
if (isFullESM && filepath === 'version.ts') {
36-
const js = babelTransform(getVersionFileBody(packageJSON.version), {
37-
envName: 'esm',
38-
});
39-
writeGeneratedFile(destPath.replace(/\.ts$/, '.js'), js);
40-
} else if (filepath.endsWith('.ts')) {
35+
if (filepath.endsWith('.ts')) {
4136
if (isFullESM) {
42-
const js = babelBuild(srcPath, { envName: 'esm' });
37+
const js =
38+
filepath === 'version.ts'
39+
? babelTransform(getVersionFileBody(packageJSON.version), {
40+
envName: 'esm',
41+
})
42+
: babelBuild(srcPath, { envName: 'esm' });
4343
writeGeneratedFile(destPath.replace(/\.ts$/, '.js'), js);
4444
} else {
4545
const cjs = babelBuild(srcPath, { envName: 'cjs' });

0 commit comments

Comments
 (0)
Please sign in to comment.