Utilizing the node-openid-client library for OpenIDConnect based authentication with an OpenID Provider.
Encountering challenges while attempting to write test cases for the program. The application runs smoothly from node CLI, obtaining the code and tokens successfully!
The specific error encountered is:
ERROR [source-reader.karma-typescript]: Error parsing code: Unexpected token (24:2)
in C:\VSCode\Projects\openid-client-test\javascript\node_modules\openid-client\lib\errors.js
at line 24, column 2:
... );
if (response) {
Object.defineProperty(th ...
Following this error, Karma runner performs the following activities:
INFO [compiler.karma-typescript]: Compiled 1 files in 3029 ms.
DEBUG [bundler.karma-typescript]: Project has 2 import/require statements, code will be bundled
DEBUG [es6-transform.karma-typescript]: Transforming C:\VSCode\Projects\openid-client-test\javascript\node_modules\openid-client\lib\index.js
These logs are generated by Karma when executed with LOG_DEBUG
configuration.
No utilization of Angular or any other UI frameworks.
Queries arising from these errors:
Q1: Curious as to why Karma parses the js file in the node_modules folder despite being excluded in tsconfig.json.
Observing the failure to bundle required files of the openid-client library prior to the error. Browserify bundle for the library succeeds, suggesting incorrect configuration in the listed files below.
Q2: Seeking assistance in identifying the misconfigured property causing such conflicts! Solution suggestions are greatly welcomed.
Q3: Concerned whether openid-client is incompatible with the karma test runner? Can such a limitation exist? No related issues found in GitHub repository.
Files modified inclusively to troubleshoot the error. These efforts led to introducing a minimal jasmine-targeted test case. Specified as follows:
probe.spec.ts file
import { Issuer } from 'openid-client';
describe('Hello', () => {
it('Checks', () => {
expect('hello').toBe('hello');
})
});
An experiment involved removing the first-line import. Resulting in successful execution reported by Karma!
Dependencies (including dev) specified in the package.json file
"dependencies": {
"@types/node": "^12.7.5",
"amazon-cognito-auth-js": "^1.3.2",
"atob": "^2.1.2",
"openid-client": "^3.7.2",
"typescript": "^3.6.3",
"xmlhttprequest": "^1.8.0"
},
"devDependencies": {
"@types/jasmine": "^3.4.1",
"jasmine-core": "^3.5.0",
"karma": "^4.3.0",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage-istanbul-reporter": "^2.1.0",
"karma-jasmine": "^2.0.1",
"karma-jasmine-html-reporter": "^1.4.2",
"karma-spec-reporter": "0.0.32",
"karma-typescript": "^4.1.1",
"karma-typescript-es6-transform": "^4.1.1"
}
tsconfig.json detailing:
{
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
],
"compilerOptions": {
"module": "commonjs",
"target": "es2016",
"noImplicitAny": false,
"strictNullChecks": true,
"moduleResolution": "node",
"sourceMap": true,
"importHelpers": true,
"outDir": "output",
"baseUrl": ".",
"typeRoots": [
"node_modules/@types"
],
"types": [
"@types/jasmine",
"@types/node"
],
"lib": [
"es2017",
"dom",
"es2015.generator",
"es2015.iterable",
"es2015.promise",
"es2015.symbol",
"es2015.symbol.wellknown",
"esnext.asynciterable"
]}
}
karma.conf.js structure provided:
module.exports = (config) => {
config.set({
frameworks: ['jasmine', 'karma-typescript'],
plugins: [
'karma-jasmine',
'karma-typescript',
'karma-chrome-launcher',
'karma-spec-reporter',
'karma-typescript-es6-transform'
],
karmaTypescriptConfig: {
tsconfig: "./tsconfig.json",
compilerOptions: {
allowJs: true
},
bundlerOptions: {
entrypoints: /\.spec\.(ts|tsx)$/,
addNodeGlobals: true,
transforms: [require("karma-typescript-es6-transform")()]
}
},
files: [{ pattern: 'src/**/*.+(js|ts)' }],
preprocessors: {
'src/**/*.+(js|ts)': ['karma-typescript']
},
client: {
clearContext: false
},
reporters: ['spec', 'karma-typescript'],
colors: true,
logLevel: config.LOG_DEBUG,
autoWatch: true,
browsers: ['Chrome'],
singleRun: true
})
}