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

TypeError: Class extends value undefined is not a constructor or null #16

Closed
eternalmatt opened this issue Nov 1, 2019 · 11 comments
Closed

Comments

@eternalmatt
Copy link

I receive the following error while executing tests with jest in TypeScript

TypeError: Class extends value undefined is not a constructor or null
      at Object.<anonymous> (node_modules/fs-capacitor/lib/index.js:22:38)
      at Object.<anonymous> (node_modules/graphql-upload/lib/processRequest.js:10:20)

where fs-capacitor/lib/index.js contains

var _fs = _interopRequireDefault(require("fs"));
class ReadStream extends _fs.default.ReadStream {
//                                   ^ the error

my tsconfig.json is

{
  "include": ["**/*.spec.ts", "**/*.d.ts"],
  "compilerOptions": {
    "strictFunctionTypes": false,
    "lib": ["es2017", "esnext.asynciterable"],
    "module": "commonjs",
    "target": "es2017",
    "outDir": "../dist/",
    "sourceMap": true,
    "typeRoots": ["node_modules/@types"],
    "types": ["jest", "supertest", "node"]
  }
}
@mike-marcacci
Copy link
Owner

Hi @eternalmatt are you using node v13 by chance? The underlying machinery has changed in node, which I'm addressing today.

@eternalmatt
Copy link
Author

I'm a bit behind. node@10.14.0 and npm@6.11.3

@eternalmatt
Copy link
Author

Actually it appears I've solved this by adding to my jest config:

  moduleDirectories: ['<rootDir>/node_modules', '<rootDir>/server'],

https://jestjs.io/docs/en/configuration#moduledirectories-array-string

I'll close this issue now, but I'm still having issues with my project tests, where now Jest is not able to proceed past

Cannot find module 'core-js/features/array/flat' from 'array.js'
However, Jest was able to find:
'./array.d.ts'
'./array.d.ts.map'
'./array.js'
'./array.js.map'
You might want to include a file extension in your import, or update your 'moduleFileExtensions',
which is currently ['ts', 'tsx',  'js'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at Object. (node_modules/apollo-env/src/polyfills/array.ts:2:1)

@eternalmatt
Copy link
Author

eternalmatt commented Nov 1, 2019

I take that back. I installed core-js correctly and I'm back to having TypeError: Class extends value undefined is not a constructor or null

@eternalmatt eternalmatt reopened this Nov 1, 2019
@eternalmatt
Copy link
Author

Sorry I didn't mention this before, I am using fs-capacitor@2.0.4. I didn't see you're on 4.0.0 now.

$ npm ls fs-capacitor
my-project
`-- apollo-server-express@2.9.7
  `-- apollo-server-core@2.9.7
    `-- graphql-upload@8.1.0
      `-- fs-capacitor@2.0.4

It's very strange, I put a breakpoint in the generated index.js file as the test is running, and when I log JSON.stringify(Object.keys(obj)) (which itself is require('fs')), the output is just ["readFileSync"].

@mike-marcacci
Copy link
Owner

@eternalmatt - you're fine to use 2.0.4; I just made v3 and 4 over this past week (I didn't like an API ambiguity with v3, which I promptly fixed but in a breaking way). The impetuous for these was support for node v13, so as long as you aren't running it there, 2.0.4 should be just fine, and will continue to be installed by graphql-upload until this lands.

Are you by chance using ES modules in your code, and if so are you transpiring them to commonjs or running them with the --experimental-modules flag?

@eternalmatt
Copy link
Author

I'm using the commonjs module syntax and compiling to es2017.

  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",

@mike-marcacci
Copy link
Owner

Hmmm... would you mind pasting your whole contents of your tsconfig.json? In particular I'm curious if you have esModuleInterop enabled.

@eternalmatt
Copy link
Author

eternalmatt commented Nov 5, 2019

Thank you for giving this so much attention. Frankly, I'm not positive the root cause here is strictly with fs-capacitor, it must have to do with jest and ts-jest. Your library works at runtime, it's the tests that are not working. Somehow they are not understanding .mjs which seems to be only a new concept in my dependencies with introduction of graphql and apollo (I'm adding this now).

tsconfig.json

{
  "compilerOptions": {
    "strictFunctionTypes": false,
    "lib": ["es2017", "esnext.asynciterable"],
    "module": "commonjs",
    "target": "es2017",
    "outDir": "../dist/",
    "sourceMap": true,
    "typeRoots": ["node_modules/@types"]
  },
  "exclude": ["**/*.spec.ts"]

tsconfig.spec.js (referenced by jest.config.js)

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "strictFunctionTypes": false,
    "types": ["jest", "supertest", "node"]
  },
  "include": ["**/*.spec.ts", "**/*.d.ts"]
}

jest.config.js

const config = {
  rootDir: process.cwd(),
  verbose: true,
  name: 'server',
  preset: 'jest-preset-typescript',
  testEnvironment: 'node',
  globals: {
    'ts-jest': {
      tsConfig: '<rootDir>/server/tsconfig.spec.json',
    },
  },
  testPathIgnorePatterns: ['<rootDir>/src', '<rootDir>/test', '<rootDir>/dist'],
  reporters: [
    'default',
    [
      'jest-junit',
      {
        outputDirectory: '<rootDir>/reports/tests',
        outputName: 'unit-report.junit.xml',
      },
    ],
  ],
};
module.exports = config;

@mike-marcacci
Copy link
Owner

Hi @eternalmatt in versions 4 and now 5, I have moved the typescript definition files into this repo (it's actually just been rewritten in typescript generally).

This should resolve any silliness with typescript.

I see you're using this library through graphql-upload, which will upgrade to one of these new versions shortly. I'm going to close this, as I'm quite confident this will solve your issues. However, if you still have problems after everything gets updated, feel free to post back.

@tomeresk
Copy link

tomeresk commented Apr 22, 2020

Just ran into this issue with node v12.14.1, I'm using the latest version of apollo-server-core, which unfortunately still uses graphql-upload v8.1.0

After seeing this issue, I managed to work around it using npm-force-resolutions to force graphql-upload to v10.0.0 and it solved the issue, so I can confirm the newer versions solved it.

It is still problematic with the latest apollo-server-core (without using the work around), though that is unrelated to this repo since the new versions did solve it.
I'm posting this mainly in case somebody could use the work around in the interim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants