Comments (9)
Could this be resolved by using babel
as per: https://jestjs.io/docs/28.x/getting-started#additional-configuration ?
from oas-tools.
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.
@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.
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);
| ^`
here's my babel and jest config.
and here's my devDependencies in the package.json
from oas-tools.
@pebo @raoulkent @alesancor1 do you have any more suggestions?
from oas-tools.
@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.
@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.
Awesome. Cheers, @alesancor1 @pebo then my current task can be unblocked.
from oas-tools.
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)
- [BUG] server property in swagger file no longer working HOT 9
- While running oas-tools with jest for unit test cases it throws error: 'request for ./middleware/index.js is not yet fulfilled' HOT 5
- Add a way for controllers to handle async exceptions HOT 1
- How to use http query params with oas tools? HOT 3
- @oas-tools/core typescript support HOT 9
- [Suggestion] Change middleware chain order (Swagger UI not working) HOT 4
- [BUG] Dynamic routes have higher priority over static routes HOT 6
- [BUG] oas tool 3.0 fail to treat optional field in schema as option. HOT 9
- [BUG] V3 Not Backwards Compatible with V2 JSON Specification File Code HOT 1
- [BUG] Cannot convert undefined or null to object oas param file has issue HOT 2
- [BUG] File spec loading error in legacy project HOT 2
- Align js-yaml version with @oas-tools/commons HOT 1
- [BUG] JSON double-encoding when using res.json() and validator.responseValidation HOT 3
- [BUG] oneof yaml specification is not valid json HOT 1
- [Question]: Controller & Service file naming convention HOT 2
- [SUGGESTION] Problem with default exports "Controller X does not have method Y" HOT 1
- [BUG] customHandler for errors is not working as expected HOT 2
- Basepath is removed when calling any endpoint on google cloud functions. HOT 1
- [SUGESTION] Add support for mutualTLS security handler HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oas-tools.