Skip to content

Commit

Permalink
add node-esm integrationTests
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed Nov 4, 2021
1 parent 8173caa commit d62688e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 7 deletions.
11 changes: 11 additions & 0 deletions integrationTests/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ describe('Integration Tests', () => {
path.join(tmpDir, 'graphql.tgz'),
);

const esmDistDir = path.resolve('./npmEsmDist');
const esmArchiveName = exec(`npm --quiet pack ${esmDistDir}`, {
cwd: tmpDir,
});

fs.renameSync(
path.join(tmpDir, esmArchiveName),
path.join(tmpDir, 'graphql-esm.tgz'),
);

function testOnNodeProject(projectName) {
const projectPath = path.join(__dirname, projectName);

Expand All @@ -45,4 +55,5 @@ describe('Integration Tests', () => {
testOnNodeProject('ts');
testOnNodeProject('node');
testOnNodeProject('webpack');
testOnNodeProject('node-esm');
});
33 changes: 33 additions & 0 deletions integrationTests/node-esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { deepStrictEqual, strictEqual } from 'assert';
import { readFileSync } from 'fs';

// Regular import
import { graphqlSync } from 'graphql';
// import with explicit extension
import { version } from 'graphql/version.js';
// _/index.js import
import { buildSchema } from 'graphql/utilities';
// import without explicit extension
import { isPromise } from 'graphql/jsutils/isPromise';

deepStrictEqual(
version,
JSON.parse(readFileSync('./node_modules/graphql/package.json')).version,
);

const schema = buildSchema('type Query { hello: String }');

const result = graphqlSync({
schema,
source: '{ hello }',
rootValue: { hello: 'world' },
});

deepStrictEqual(result, {
data: {
__proto__: null,
hello: 'world',
},
});

strictEqual(isPromise(Promise.resolve()), true);
13 changes: 13 additions & 0 deletions integrationTests/node-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "module",
"description": "graphql-js should work on all supported node versions with ESM",
"scripts": {
"test": "node test.js"
},
"dependencies": {
"graphql": "file:../graphql-esm.tgz",
"node-12": "npm:[email protected]",
"node-14": "npm:[email protected]",
"node-16": "npm:[email protected]"
}
}
22 changes: 22 additions & 0 deletions integrationTests/node-esm/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { execSync } from 'child_process';
import { createRequire } from 'module';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const { dependencies } = createRequire(import.meta.url)('./package.json');

const nodeVersions = Object.keys(dependencies)
.filter((pkg) => pkg.startsWith('node-'))
.sort((a, b) => b.localeCompare(a));

for (const version of nodeVersions) {
console.log(`Testing on ${version} ...`);

const nodePath = join(
dirname(fileURLToPath(import.meta.url)),
'node_modules',
version,
'bin/node',
);
execSync(nodePath + ' index.js', { stdio: 'inherit' });
}
14 changes: 7 additions & 7 deletions resources/build-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ if (require.main === module) {
const destPath = path.join(distDirectory, filepath);

fs.mkdirSync(path.dirname(destPath), { recursive: true });
if (isFullESM && filepath === 'version.ts') {
const js = babelTransform(getVersionFileBody(packageJSON.version), {
envName: 'esm',
});
writeGeneratedFile(destPath.replace(/\.ts$/, '.js'), js);
} else if (filepath.endsWith('.ts')) {
if (filepath.endsWith('.ts')) {
if (isFullESM) {
const js = babelBuild(srcPath, { envName: 'esm' });
const js =
filepath === 'version.ts'
? babelTransform(getVersionFileBody(packageJSON.version), {
envName: 'esm',
})
: babelBuild(srcPath, { envName: 'esm' });
writeGeneratedFile(destPath.replace(/\.ts$/, '.js'), js);
} else {
const cjs = babelBuild(srcPath, { envName: 'cjs' });
Expand Down

0 comments on commit d62688e

Please sign in to comment.