From d62688e14e5a3722e48ada9e1c2b24dfb4c793fc Mon Sep 17 00:00:00 2001 From: PabloSzx Date: Thu, 4 Nov 2021 14:06:27 -0300 Subject: [PATCH] add node-esm integrationTests --- integrationTests/integration-test.js | 11 +++++++++ integrationTests/node-esm/index.js | 33 ++++++++++++++++++++++++++ integrationTests/node-esm/package.json | 13 ++++++++++ integrationTests/node-esm/test.js | 22 +++++++++++++++++ resources/build-npm.js | 14 +++++------ 5 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 integrationTests/node-esm/index.js create mode 100644 integrationTests/node-esm/package.json create mode 100644 integrationTests/node-esm/test.js diff --git a/integrationTests/integration-test.js b/integrationTests/integration-test.js index aa5eac2f2e7..0680cf494f8 100644 --- a/integrationTests/integration-test.js +++ b/integrationTests/integration-test.js @@ -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); @@ -45,4 +55,5 @@ describe('Integration Tests', () => { testOnNodeProject('ts'); testOnNodeProject('node'); testOnNodeProject('webpack'); + testOnNodeProject('node-esm'); }); diff --git a/integrationTests/node-esm/index.js b/integrationTests/node-esm/index.js new file mode 100644 index 00000000000..5e7da4c7d7c --- /dev/null +++ b/integrationTests/node-esm/index.js @@ -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); diff --git a/integrationTests/node-esm/package.json b/integrationTests/node-esm/package.json new file mode 100644 index 00000000000..c2cefb2bf33 --- /dev/null +++ b/integrationTests/node-esm/package.json @@ -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:node@12.x.x", + "node-14": "npm:node@14.x.x", + "node-16": "npm:node@16.x.x" + } +} diff --git a/integrationTests/node-esm/test.js b/integrationTests/node-esm/test.js new file mode 100644 index 00000000000..611fc3ddaf2 --- /dev/null +++ b/integrationTests/node-esm/test.js @@ -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' }); +} diff --git a/resources/build-npm.js b/resources/build-npm.js index 95de0933655..97c7dda297f 100644 --- a/resources/build-npm.js +++ b/resources/build-npm.js @@ -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' });