When running jest in combination with angular-auth-oidc I am getting an error telling me that the jsrsasign-reduced is not plain javascript
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/node_modules/jsrsasign-reduced/jsrsasign.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){var navigator = {};navigator.userAgent = false;var window = {};if(YAHOO===undefined){var YAHOO={}}YAHOO.lang={extend:function(g,h,f){if(!h||!g){throw new Error("YAHOO.lang.extend failed, please check that all dependencies are included.")}var d=function(){};d.prototype=h.prototype;g.prototype=new d();g.prototype.constructor=g;g.superclass=h.prototype;if(h.prototype.constructor==Object.prototype.constructor){h.prototype.constructor=h}if(f){var b;for(b in f){g.prototype[b]=f[b]}var e=function(){},c=["toString","valueOf"];try{if(/MSIE/.test(navigator.userAgent)){e=function(j,i){for(b=0;b<c.length;b=b+1){var l=c[b],k=i[l];if(typeof k==="function"&&k!=Object.prototype[l]){j[l]=k}}}}}catch(a){}e(g.prototype,f)}}};var CryptoJS=CryptoJS||(function(e,g){var a={};var b=a.lib={};var j=b.Base=(function(){function n(){}return{extend:
SyntaxError: Unexpected token export
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
at node_modules/angular-auth-oidc-client/bundles/angular-auth-oidc-client.umd.js:2:113
at Object.<anonymous> (node_modules/angular-auth-oidc-client/bundles/angular-auth-oidc-client.umd.js:5:2)
Any idea how to solve this?
Hi @taairsupport don't know what jest is :) If you want to build the code directly, you can replace the jsrsasign-reduced with jsrsasign. Just change the imports to jsrsasign, and change the package dependency to jsrsasign in the packages.json and you can compile in any way you want.
https://github.com/damienbod/angular-auth-oidc-client/blob/master/package.json#L35
to
https://www.npmjs.com/package/jsrsasign
and
import { hextob64u, KEYUTIL, KJUR } from 'jsrsasign-reduced';
to
import { hextob64u, KEYUTIL, KJUR } from 'jsrsasign';
Greetings Damien
Hi Damien,
Thanks for getting back. I will try the solution that you have mentioned.
Hi @taairsupport,
I am having the same issue with jest, did above solution work for you? How do you transform above solution to jest?
Thanks in advance
Hi @damienbod ,
Thanks for the solution above. I tried it and built the lib in my local, and manually copied build code over to my project node_modules. It works nicely with Jest!
Just got one question:
is there any particular reason to use 'jsrsasign-reduced' rather than 'jsrsasign' ? Looks like 'jsrsasign-reduced' is the culprit that causes this issue.
Thank you!
Hi @taairsupport don't know what jest is :) If you want to build the code directly, you can replace the jsrsasign-reduced with jsrsasign. Just change the imports to jsrsasign, and change the package dependency to jsrsasign in the packages.json and you can compile in any way you want.
https://github.com/damienbod/angular-auth-oidc-client/blob/master/package.json#L35
to
https://www.npmjs.com/package/jsrsasign
and
import { hextob64u, KEYUTIL, KJUR } from 'jsrsasign-reduced';
to
import { hextob64u, KEYUTIL, KJUR } from 'jsrsasign';
Greetings Damien
Hi @yuchaosydney
The only reason for using jsrsasign-reduced is the package size. I removed the things from jsrsasign which are not required for this lib to work.
But this causes 2 problems now
I will either fix this, or revert back to jsrsasign
Not sure yet how to solve this.
Greetings Damien
Hi @damienbod
Thanks for your reply.
I will use the manual build code for now and keep an eye on future release.
Thank you very much!
Yuchao
adding transformIgnorePatterns: ['node_modules/(?!jsrsasign-reduced)'] to your jest.config.js and allowJs:true to your tsconfig compileroptions solves the issue.
@remkoboschker Thanks!
Most helpful comment
adding
transformIgnorePatterns: ['node_modules/(?!jsrsasign-reduced)']to your jest.config.js andallowJs:trueto your tsconfig compileroptions solves the issue.