Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommonJS build is not available for testing #8181

Closed
yskkin opened this issue May 10, 2021 · 4 comments
Closed

CommonJS build is not available for testing #8181

yskkin opened this issue May 10, 2021 · 4 comments

Comments

@yskkin
Copy link

yskkin commented May 10, 2021

Intended outcome:

In vanilla (i.e. without react and webpack or any bundling tool), following testing code compiles

import { mockSingleLink } from "@apollo/client/testing";

Actual outcome:

  ● Test suite failed to run

    Cannot find module 'react' from 'node_modules/@apollo/client/testing/testing.cjs.js'

    Require stack:
      node_modules/@apollo/client/testing/testing.cjs.js
      __tests__/unit/models/Company.test.ts

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)
      at Object.<anonymous> (node_modules/@apollo/client/testing/testing.cjs.js:13:29)

How to reproduce the issue:

This is not a case for production code since import ... from "@apollo/client/core" can skip importing @apollo/client/react.
But, import { mockSingleLink } from "@apollo/client/utilities/testing/"; gives the following error since there is no CommonJS version of @apollo/client/utilities/testing.

    Details:

    /...../node_modules/@apollo/client/utilities/testing/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { MockedProvider } from "./mocking/MockedProvider.js";
                                                                                             ^^^^^^

    SyntaxError: Unexpected token export

      2 | snip
      3 |
    > 4 | import { mockSingleLink } from "@apollo/client/utilities/testing/";
        | ^
      5 |
      6 | snip
      7 | snip

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (xxxxxxxx.test.ts:4:1)

Versions
System:
OS: macOS Mojave 10.14.6
Binaries:
Node: 12.6.0 - ~/.nodebrew/current/bin/node
Yarn: 1.17.3 - ~/.nodebrew/current/bin/yarn
npm: 6.9.0 - ~/.nodebrew/current/bin/npm
Browsers:
Chrome: 90.0.4430.93
Firefox: 84.0
Safari: 14.0.3
npmPackages:
@apollo/client: ^3.3.16 => 3.3.16

@benjamn
Copy link
Member

benjamn commented May 13, 2021

@yskkin You're right, we ought to have a version of @apollo/client/testing that doesn't depend on React. The MockedProvider abstraction is of course React-specific, but perhaps we could confine the React-specific parts to a nested package like @apollo/client/testing/react or @apollo/client/react/testing.

@HoPGoldy
Copy link

I found a similar issue that looked like @apollo/client couldn't run with pure js.

I'm following tutorial to learn how to use this.

What did I do

  • run npm install @apollo/client graphql

  • add following code in index.js

import { ApolloClient, InMemoryCache } from '@apollo/client';

const client = new ApolloClient({
  uri: 'https://48p1r2roz4.sse.codesandbox.io',
  cache: new InMemoryCache()
});  
  • run node index.js

What did I receive

internal/modules/cjs/loader.js:885
  const err = new Error(message);
              ^

Error: Cannot find module 'react'
Require stack:
- E:\learn\apollo-learn\node_modules\@apollo\client\react\context\context.cjs.js
- E:\learn\apollo-learn\node_modules\@apollo\client\react\react.cjs.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)
    at Function.Module._load (internal/modules/cjs/loader.js:730:27)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (E:\learn\apollo-learn\node_modules\@apollo\client\react\context\context.cjs.js:7:29)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'E:\\learn\\apollo-learn\\node_modules\\@apollo\\client\\react\\context\\context.cjs.js',
    'E:\\learn\\apollo-learn\\node_modules\\@apollo\\client\\react\\react.cjs.js'
  ]
}

Did I miss something?

version

System:

  • OS: windows 10 1909

Binaries:

  • Node: 14.17.0
  • npm: 6.14.13

npmPackages:

  • @apollo/client: ^3.3.19
  • graphql: ^15.5.0

@bignimbus
Copy link
Contributor

This was resolved in #8190 - @HoPGoldy try importing from the core directory instead:

import { ApolloClient, InMemoryCache } from '@apollo/client/core';

🙏🏻 Thanks!

@github-actions
Copy link
Contributor

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants