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

While running oas-tools with jest for unit test cases it throws error: 'request for ./middleware/index.js is not yet fulfilled' #342

Closed
vineetbro opened this issue Sep 19, 2022 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@vineetbro
Copy link

Describe the bug

I am using oas-tools 3.0.0 for my server and jest 29.0.0 for unit test case. My server runs as expected but when I ran my unit test cases it throws the error: 'request for ./middleware/index.js' is not yet fulfilled' and all test cases started failing. When I tried with oas-tools: 2.2.2 it works totally fine.

To Reproduce

Steps to reproduce the behavior:

  1. initialize the server with @oas-tools/core
  2. In jest configuration make sure the server files using @oas-tools/core is part of coverage.
  3. Write a unit test case such that the 'initialize(express(), config)' is getting called.
  4. Run the unit test cases using command: node --experimental-vm-modules node_modules/jest/bin/jest.js
  5. See error

Expected behavior

The Oas must have initialized properly when running unit test cases using jest

Logs

2022-09-19T06:44:01.320Z - info: Specification file dereferenced
2022-09-19T06:44:01.402Z - info: Registered OASParams middleware
2022-09-19T06:44:01.403Z - info: Registered OASRequestValidator middleware
2022-09-19T06:44:01.403Z - info: Registered OASResponseValidator middleware
2022-09-19T06:44:01.403Z - info: Registered OASRouter middleware
2022-09-19T06:44:01.403Z - info: Registered OASSwagger middleware
2022-09-19T06:44:01.425Z - info: OAS Initialized successfully

● Test suite failed to run
request for './middleware/index.js' is not yet fulfilled
at Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:767:5)

Desktop (please complete the following information):

  • OS: macOS Monteray 12.5
  • Node Version: 14.18.1, 16.17.0
@vineetbro vineetbro added the bug Something isn't working label Sep 19, 2022
@alesancor1
Copy link
Member

The initialize(app, cfg) function is asyncronous in v3.0.0, which may be the source of the problem. Are you using async/await to wait for the initialize promise to fulfill? I don't have much experience working with jest but with mocha for unit testing, take a look to OAS Tools tests, they may be similar to what you are trying to accomplish.

@alesancor1 alesancor1 self-assigned this Sep 19, 2022
@vineetbro
Copy link
Author

I have used the async/await to wait for initialization. The behaviour is such, with 'require(@oas-tools/core)' itself the error is thrown.

I am providing a zip file of project where the error can be reproduced. Here are the instructions to run and test the project

  1. Download and extract the project
  2. Run 'npm install' command
  3. To start server run: make server
  4. To execute unit test cases run: make test

Project ZIP
repro-server.zip:

Observation: The test cases will execute successfully but the Test Suit will fail because of the error:
● Test suite failed to run
request for './middleware/index.js' is not yet fulfilled
at Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:767:5)

@alesancor1
Copy link
Member

This seems to be an issue with jest itself and the way it deals with ESM packages (see the issue here), so there's not much that can be done at the moment.

The core package will migrate to TS in the future and hopefully it will solve this problem due to the code being transpiled into CommonJS. For the time being, you could use mocha + c8 for coverage (I tested your repro-server using mocha and it works) or update to Node.js v18, which has an integrated testing library that I believe is pretty similar to jest.

@johonIngka
Copy link

@vineetbro did you find any solution or work arounds for this issue?

@vineetbro
Copy link
Author

No, It is an issue with jest only. Right now I am using v2.2.2. The other option is to use mocha in place of jest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants