Trying to setup jest tests around auth0-spa-js but getting error 'auth0-spa-js must run on a secure origin' when attempting to await createAuth0Client(config). Whats the best way to mock secure origin in jest tests?
Trying to setup jest tests around auth0-spa-js but getting error 'auth0-spa-js must run on a secure origin' when attempting to await createAuth0Client(config). Whats the best way to mock secure origin in jest tests?
Share Improve this question edited Feb 24, 2020 at 23:13 Maxim Suponya asked Feb 24, 2020 at 5:38 Maxim SuponyaMaxim Suponya 1,5143 gold badges22 silver badges48 bronze badges2 Answers
Reset to default 22After digging into auth0 code to see what it wants, i gave it global.crypto.subtle = {} and it seems to have satisfied it, the error has gone, the tests pass:
const JSDOM = require('jsdom').JSDOM
Object.defineProperty(global.self, 'crypto', {
value: {
getRandomValues: arr => crypto.randomBytes(arr.length)
}
})
global.crypto.subtle = {} // this gets around the 'auth0-spa-js must run on a secure origin' error
Mocking the module worked great for me.
jest.mock('@auth0/auth0-spa-js');