最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mock secure origin for auth0-spa-js in Jest tests - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 22

After 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');
发布评论

评论列表(0)

  1. 暂无评论