Giter Site home page Giter Site logo

Comments (9)

raoulkent avatar raoulkent commented on May 24, 2024

Could this be resolved by using babel as per: https://jestjs.io/docs/28.x/getting-started#additional-configuration ?

from oas-tools.

alesancor1 avatar alesancor1 commented on May 24, 2024

This may be solved by #366, but I'm not sure.

Anyways, what @raoulkent proposes is the way to go. A migration to TS (instead of babel) is planned for v3.1.0 at #350, which should solve this issue with CommonJS/ESM compatibility

from oas-tools.

pebo avatar pebo commented on May 24, 2024

@Joshua-Tu As oas-tools v3 is an ESM-only module you need to run jest with the --experimental-vm-modules flag, e.g.

"test": "NODE_OPTIONS=--experimental-vm-modules jest",

See https://jestjs.io/docs/ecmascript-modules for more info.

However you currently won't be able to load oas-tools esm modules from jest due to a couple of bugs in jest/node/v8. See #342.

from oas-tools.

Joshua-Tu avatar Joshua-Tu commented on May 24, 2024

Thanks, @raoulkent @alesancor1 and @pebo I Just followed the steps in the doc and I still got this error.
`tests/controllers/apiDocs.spec.ts (6.944 s)
GET /api-specs
✕ should respond with 200 OK (67 ms)

● GET /api-specs › should respond with 200 OK

You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/ecmascript-modules

  15 | };
  16 |
> 17 | const router = sampleRouter(config);
     |                        ^
  18 |
  19 | export default router;
  20 |

  at invariant (node_modules/jest-runtime/build/index.js:2344:11)
  **at Object.<anonymous> (node_modules/@oas-tools/core/src/entrypoint.cjs:1:98)**
  at swaggerRouterFactory (node_modules/@sampleCompanyPacakge/sample-swagger-router.js:33:26)
  at Object.<anonymous> (sample-service/routes/v1/sampleRouter.ts:17:24)
  at Object.<anonymous> (sample-service/routes/v1/index.ts:1:1)
  at Object.<anonymous> (sample-service/routes/index.ts:1:1)
  at Object.<anonymous> (sample-service/app.ts:15:1)
  at Object.<anonymous> (__tests__/controllers/apiDocs.spec.ts:2:1)`

and then I added the "--experimental-vm-modules" in my npm test script.
I got this error:
(Use node --trace-warnings ...` to show where the warning was created)
FAIL tests/controllers/apiDocs.spec.ts (5.605 s)
GET /api-specs
✕ should respond with 200 OK (1391 ms)

● GET /api-specs › should respond with 200 OK

request for './middleware/index.js' is not yet fulfilled

  at async Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:698:5)

● GET /api-specs › should respond with 200 OK

expect(received).toBe(expected) // Object.is equality

Expected: 200
Received: 404

  19 |                 })
  20 |             );
> 21 |         expect(response.statusCode).toBe(200);
     |                                     ^`

image

Still looks like the module import issue.

here's my babel and jest config.
image
and here's my devDependencies in the package.json
image

from oas-tools.

Joshua-Tu avatar Joshua-Tu commented on May 24, 2024

@pebo @raoulkent @alesancor1 do you have any more suggestions?

from oas-tools.

pebo avatar pebo commented on May 24, 2024

@pebo @raoulkent @alesancor1 do you have any more suggestions?

It currently doesn't work with jest. There's a PR for adding jest support: #366. Until this issue is resolved jest won't work with oas-tools but you might have better luck with another test framework.

from oas-tools.

alesancor1 avatar alesancor1 commented on May 24, 2024

@Joshua-Tu I just merged @pebo's PR (#366). As soon as v3.1.0 is released you should be able to run jest. Once I release the new version I will close this. Please reopen if you have any problem.

from oas-tools.

Joshua-Tu avatar Joshua-Tu commented on May 24, 2024

Awesome. Cheers, @alesancor1 @pebo then my current task can be unblocked.

from oas-tools.

dogmatic69 avatar dogmatic69 commented on May 24, 2024

I've run into this issue today upgrading from 2.x to 3.x

Running oas-tools 3.1.0 in docker node:19.8.1-alpine I get the following error any time oas-tools is required.

    You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/ecmascript-modules

      3 | const configureOpenApiTools = async ({ app, config }) => {
      4 |   // eslint-disable-next-line global-require
    > 5 |   const oasTools = require('@oas-tools/core');
        |                    ^
      6 |   config.customLogger.info('OpenAPI init');
      7 |   await oasTools.initialize(app, config);

I've tried with experimental-vm-modules, babel and various other things but still getting this error in jest when the module is loaded.

package.json is:


  "scripts": {
    "test": "jest",
    "coverage": "nyc --reporter=html --all mocha --recursive --exit ./test",
    "lint": "eslint ./src --ext .js ",
    "lint:fix": "eslint ./src --ext .js --fix",
    "prettier:fix": "prettier -w -u  .",
    "prettier:check": "prettier -c -u .",
    "license": "license-checker"
  },
  "devDependencies": {
    "@babel/core": "^7.21.3",
    "@babel/preset-env": "^7.20.2",
    "babel-jest": "^29.5.0",
    "eslint": "8.36.0",
    "eslint-config-airbnb-base": "15.0.0",
    "eslint-config-prettier": "8.7.0",
    "eslint-plugin-import": "2.27.5",
    "eslint-plugin-prettier": "4.2.1",
    "jest": "29.5.0",
    "license-checker": "25.0.1",
    "prettier": "2.8.4",
    "supertest": "6.3.3"
  },
  "dependencies": {
    "@google-cloud/bigquery": "6.1.0",
    "@google-cloud/pubsub": "3.4.1",
    "@google-cloud/secret-manager": "^4.2.0",
    "@google-cloud/storage": "^6.9.1",
    "@oas-tools/core": "3.1.0",
    "body-parser": "1.20.2",
    "express": "4.18.2",
    "minimist": "1.2.8"
  },
  "resolutions": {
    "**/node-forge": "1.3.1",
    "minimist": "1.2.7",
    "multer": "1.4.5-lts.1"
  }

from oas-tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.