I was trying to test the SDK instance of my app with
sinonlibrary stubs and spies. I was trying to make sure a middleware
that retrievs userapp_metadataand then do some logic on them is working as expected.
`it('should through if roles do not match', async function () {
// stub getUser
const stubGetUser = sinon.stub(Auth0ManagementAPI, 'getUser');
stubGetUser.resolves({ app_metadata: { roles: ['this role'] } });
// stub getSession
const stubGetSession = sinon.stub(auth0, 'getSession');
stubGetSession.returns({ user: { sub: 'somerandomsub' } });
// stub checkUserRole
const stubCheckUserRole = sinon.stub({ chekUserRole }, 'chekUserRole');
// fake HTTP req,res objects
const req: any = {};
const res: any = {};
await stubCheckUserRole(req, res, 'is not like this role');
expect(stubCheckUserRole).to.throw();
});`
UnhandledPromiseRejectionWarning: TypeError: "secret" is required
at Object.get (/home/gistcodee/dev/swagy/node_modules/@auth0/nextjs-auth0/src/auth0-session/get-config.ts:158:11)
at Object.getConfig (/home/gistcodee/dev/swagy/node_modules/@auth0/nextjs-auth0/src/config.ts:431:22)
at initAuth0 (/home/gistcodee/dev/swagy/node_modules/@auth0/nextjs-auth0/src/index.ts:64:38)
at Object.<anonymous> (/home/gistcodee/dev/swagy/lib/backend/auth/nextAuht0.ts:14:16)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Module._compile (/home/gistcodee/dev/swagy/node_modules/pirates/lib/index.js:99:24)
at Module.m._compile (/home/gistcodee/dev/swagy/node_modules/ts-node/src/index.ts:1056:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at require.extensions.<computed> (/home/gistcodee/dev/swagy/node_modules/ts-node/src/index.ts:1059:12)
at Object.newLoader [as .ts] (/home/gistcodee/dev/swagy/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:903:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/gistcodee/dev/swagy/test/try.spec.ts:5:1)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Module._compile (/home/gistcodee/dev/swagy/node_modules/pirates/lib/index.js:99:24)
at Module.m._compile (/home/gistcodee/dev/swagy/node_modules/ts-node/src/index.ts:1056:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at require.extensions.<computed> (/home/gistcodee/dev/swagy/node_modules/ts-node/src/index.ts:1059:12)
at Object.newLoader [as .ts] (/home/gistcodee/dev/swagy/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:903:19)
at require (internal/modules/cjs/helpers.js:74:18)
at /home/gistcodee/dev/swagy/node_modules/mocha/lib/mocha.js:427:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/gistcodee/dev/swagy/node_modules/mocha/lib/mocha.js:424:14)
at Mocha.run (/home/gistcodee/dev/swagy/node_modules/mocha/lib/mocha.js:1003:10)
at run (/home/gistcodee/dev/swagy/node_modules/mocha/lib/cli/watch-run.js:265:20)
at rerun (/home/gistcodee/dev/swagy/node_modules/mocha/lib/cli/watch-run.js:293:5)
at Object.scheduleRun (/home/gistcodee/dev/swagy/node_modules/mocha/lib/cli/watch-run.js:286:7)
at FSWatcher.<anonymous> (/home/gistcodee/dev/swagy/node_modules/mocha/lib/cli/watch-run.js:188:14)
at FSWatcher.emit (events.js:314:20)
at FSWatcher.EventEmitter.emit (domain.js:483:12)
at FSWatcher.emitWithAll (/home/gistcodee/dev/swagy/node_modules/chokidar/index.js:535:32)
at FSWatcher._emit (/home/gistcodee/dev/swagy/node_modules/chokidar/index.js:626:8)
at listener (/home/gistcodee/dev/swagy/node_modules/chokidar/lib/nodefs-handler.js:370:20)
(node:27222) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
Please provide the following:
sinonFinally, Any good guide on how to test the SDK in general.
Update: It was the Auth0ManagementAPI in the code above is what have caused the error. What it does in my code is it creates a management client to use the Management API with node-auth0 package :
`mport MGMT from 'auth0';
const Auth0ManagementAPI = new MGMT.ManagementClient({
domain: process.env.AUTH0_TENANT_DOMAIN,
clientId: process.env.AUTH0_NON_ACTIVE_CLIENT_ID,
clientSecret: process.env.AUTH0_NON_ACTIVE_CLIENT_SECRET,
scope: 'read:users',
});
export default Auth0ManagementAPI;``
Hi @mustafaKamal-fe
UnhandledPromiseRejectionWarning: TypeError: "secret" is required at Object.get (/home/gistcodee/dev/swagy/node_modules/@auth0/nextjs-auth0/src/auth0-session/get-config.ts:158:11) at
This error is being thrown from nextjs-auth0 - when you call initAuth0 (I'm assuming this is what you're doing since I can't see the code, but I see an auth0 instance) you're creating an instance of the SDK, and this requires a secret (used to encrypt the session cookie) - for the purposes of testing you can pass initAuth0 a mock secret to supress that error.
Also, for an example of testing a nextjs-auth0 app, take a look at the sample app here https://github.com/auth0-samples/auth0-nextjs-samples/blob/main/Sample-01
Thank you. i will check this and get back to you.
Hi @mustafaKamal-fe - let me know if you have any other questions or you're happy for me to close this, thanks
Hi @mustafaKamal-fe - let me know if you have any other questions or you're happy for me to close this, thanks
Hi, I was able to get rid of this error.. Obviously it was some strange sinon error that caused it. In my code above sinon was actually not wrapping the instance i have in my app in order to stub it or do whatever i want.. it was always trying to create a new instance with legit data (clientId, secret,etc...) that is actually not available under test (process.env.NODE_ENV was equal to undefiend which i do not know why)... so i fixed that sinon stuf by importing every possible function i could use as a deafult export and import it as import * as someFunction..For the SDK part, there are zero-issues so far and I love it