0

I have several APIs where we pass in descriptions as part of the payload. I use OpenAPI to generate a Typescript Client and compile targeting esnext.

export interface V1alphaExample {
    /**
     * Description of the rule for backoffice use only.
     * @type {string}
     * @memberof V1alphaExample 
     */
    description?: string;
}

export function V1alphaExampleFromJSONTyped(json: any, ignoreDiscriminator: boolean): V1alphaExample {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    return {
        'description': !exists(json, 'description') ? undefined : json['description'],
    };
}

export function V1alphaExampleToJSON(value?: V1alphaExample | null): any {
    if (value === undefined) {
        return undefined;
    }
    if (value === null) {
        return null;
    }
    return {
        'description': value.description,
    };
}

compiled to JS:

export function V1alphaExampleFromJSONTyped(json, ignoreDiscriminator) {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    return {
        'description': !exists(json, 'description') ? undefined : json['description'],
    };
}
export function V1alphaExampleToJSON(value) {
    if (value === undefined) {
        return undefined;
    }
    if (value === null) {
        return null;
    }
    return {
        'description': value.description,
    };
}

During the compilation of my application (using babel + typescript), things work fine. However, when using Jest to test my application, I am getting a bunch of errors such as:

Cannot find module 'core-js/modules/es.symbol.description' from 'V1alphaExample.js'

I believe the value.description in the compiled JS is triggering Jest + Babel to think I'm using the Symbol.prototype.description feature when I'm not.

Is there a way to disable this when using Jest? Should description be considered a reserved keyword going forward and discouraged from use as object properties?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
someone1
  • 3,570
  • 2
  • 22
  • 35
  • None of your functions or parameters that have `json` in their names store, work with or return [JSON](http://json.org). – Andreas Dec 27 '19 at 15:39
  • 1
    *"I believe the `value.description` in the compiled JS is triggering Jest + Babel to think I'm using the `Symbol.prototype.description` feature when I'm not."* I think that's almost certainly a red herring. Just having a property called `description` is ***incredibly*** common. I can't imagine that that's causing Jest or Babel to get confused in that way. – T.J. Crowder Dec 27 '19 at 15:40
  • Re @Andreas's comment above, JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript or TypeScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Dec 27 '19 at 15:41
  • @Andreas - this is all auto-generated code from an OpenAPI specification (see [here](https://github.com/OpenAPITools/openapi-generator).) - I'm a little out of my depth on this one so maybe my guess for the cause is incorrect but there's some linkage here - the code was working fine prior to introducing the `description` property to my API – someone1 Dec 27 '19 at 15:42
  • Is there a `es.symbol.description` module at the mentioned location (because of this [question](https://stackoverflow.com/questions/55308769/module-not-found-error-cant-resolve-core-js-es6))? – Andreas Dec 27 '19 at 15:52
  • @Andreas - Yes, `node_modules/core-js/modules/es.symbol.description.js` exists. – someone1 Dec 27 '19 at 15:59

1 Answers1

0

I found a couple ways to fix the issue but only one way did it without borking the build step:

  1. I changed "main": "./dist/index.js", => "esnext": "./dist/index.js", in package.json - but then importing wouldn't resolve during build
  2. Install core-js as a dev dependency on the Typescript client package - I'm not sure why this is necessary. I have the client installed as a relative file package in my main app (e.g. my package json includes it as file:../../genclient/package).

The solutions above don't feel right (the first one obviously is wrong). If someone has a better way to modify some config files in my project to get Jest working properly, please share your answer so I may reward you the points! As of now, #2 is enough to let me continue development.

Other things I tried to no avail:

  • Wiping the package-lock.json & node_modules directory and installing from scratch
  • Updating virtually all my app's dependencies (both local and global cli helpers)
  • Adding the generated typescript client path to transformIgnorePatterns in my jest.config.js - this works for resolving this issue, but since its targeting esnext, it actually needs to be transpiled
someone1
  • 3,570
  • 2
  • 22
  • 35