Swagger-client 3.19.2 errors while testing in mocha
dchicchon opened this issue · 10 comments
Q&A (please complete the following information)
- OS: macOS
- Environment: Chrome 111, Nodejs v14.17.3
- Method of installation: npm
- Swagger-Client version: 3.19.2
- Swagger/OpenAPI version: OpenAPI 3.1.0
Content & configuration
In my OpenAPI.js file I have this. When using this in testing this one line fails
const Swagger = require('swagger-client');
Swagger/OpenAPI definition:
N/A
Swagger-Client usage:
const Swagger = require('swagger-client');
class OpenAPI {
constructor() {
console.log('this is my openapi implementation')
}
async makeRequest(modelSpec, authentication, request) {
const requestClient = await Swagger({
spec: modelSpec,
authorizations: authentication,
});
await requestClient.execute(request);
}
}
Describe the bug you're encountering
Here's the error I'm getting in my terminal while testing. Error does not occur when running app
Error: Cannot find module '/Users/dchicchon/Projects/myproject/master/mainProject/node_modules/@projectApp/submodule/node_modules/@swagger-api/apidom-reference/cjs/parse/parsers/binary/#buffer'
Require stack:
- /Users/dchicchon/Projects/myproject/master/mainProject/node_modules/@projectApp/submodule/node_modules/@swagger-api/apidom-reference/cjs/parse/parsers/binary/index-node.cjs
- /Users/dchicchon/Projects/myproject/master/mainProject/node_modules/@projectApp/submodule/node_modules/swagger-client/lib/resolver/strategies/openapi-3-1-apidom/resolve.js
- /Users/dchicchon/Projects/myproject/master/mainProject/node_modules/@projectApp/submodule/node_modules/swagger-client/lib/resolver/strategies/openapi-3-1-apidom/index.js
- /Users/dchicchon/Projects/myproject/master/mainProject/node_modules/@projectApp/submodule/node_modules/swagger-client/lib/index.js
- /Users/dchicchon/Projects/myproject/master/mainProject/node_modules/@projectApp/submodule/node_modules/swagger-client/lib/commonjs.js
- /Users/dchicchon/Projects/myproject/master/mainProject/node_modules/@projectApp/submodule/srcfiles/swaggers/OpenAPI.js
- /Users/dchicchon/Projects/myproject/master/mainProject/node_modules/@projectApp/submodule/test/unit/srcfiles/swaggers/OpenAPITest.js
To reproduce...
Steps to reproduce the behavior:
Unsure how to reproduce. Only able to produce this in my main project
Expected behavior
Expected no errors to occur while testing
Screenshots
N/A
Additional context or thoughts
The only "workaround" to this solution would be to comment out an import or replace it with the actual library in the @swagger-api/apidom-reference file
/Users/dchicchon/Projects/project/release/myproject/node_modules/@myproject/submodule/node_modules/@swagger-api/apidom-reference/cjs/parse/parsers/binary/index-node.cjs
"use strict";
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
// var _buffer = require("#buffer"); // commented out import here
// or
var _buffer = require('buffer'); // replaced with dependency rather than subpath import
...
I noticed that this error arose when trying to find the import for #buffer which is an internal import for the apidom-reference
dependency. I think the issue could be happening where the import is erroring. I found a stackoverflow question that is similar to my problem: https://stackoverflow.com/questions/68869339/jest-cannot-resolve-node-modules-subpath-pattern-imports
Hi @dchicchon,
This is an issue with older version of Jest - jestjs/jest#12270.
Latest version of jest does support export and import package.json fields. After updating to latest version you shouldn't
see any issue.
For older Jest versions you have introduce following Jest config:
moduleNameMapper: {
/**
* This is here only until next version of Jest is released - 29.4.x.
* Jest doesn't support package.json imports fields now, so we have
* to provide this workaround.
*
* More information in https://github.com/facebook/jest/issues/12270.
*
*/
'#buffer':
'<rootDir>/node_modules/@swagger-api/apidom-reference/cjs/util/polyfills/buffer/protocol-import.cjs',
'#fs':
'<rootDir>/node_modules/@swagger-api/apidom-reference/cjs/util/polyfills/fs/protocol-import.cjs',
'#util':
'<rootDir>/node_modules/@swagger-api/apidom-reference/cjs/util/polyfills/util/protocol-import.cjs',
},
@dchicchon what version of mocha?
ApiDOM monorepo itself uses mocha@10 without any issues.
Here I'm using "mocha": "^10.2.0",
@dchicchon what Node.js version are you using?
Using node 18.5.0 and npm 9.6.2
After more digging it looks like it could be similar to an issue like this: ardatan/graphql-tools#2542
I cannot reproduce.
I've create a repo https://github.com/char0n/swagger-client-mocha where you can try that everything works as expected.
Clone the repo, run npm i
and npm test
. I've created tests both for CommonJS and ESM usecases.
I'm sorry but I cannot help more given all the information you provided. This looks like a local issue with your setup or tooling. SwaggerClient + ApiDOM works as expected on Node.js >= 12.20.0.
The only thing I can do is to remove the imports
field, but first I need to understand why this happens and in what situations.
No problem, appreciate your help. i'll post a solution if i find one
@dchicchon ok ping me (by my github ID mention) when you'll find out more. But whatever it is, it's interfering with how Node.js works since >= 12.20.0 version.