在 Jest 测试中模拟 auth0-spa-js 的安全来源

Max*_*nya 3 javascript node.js jestjs auth0

尝试围绕 auth0-spa-js 设置玩笑测试,但在尝试等待 createAuth0Client(config) 时收到错误“auth0-spa-js 必须在安全源上运行”。在玩笑测试中模拟安全来源的最佳方法是什么?

Max*_*nya 13

在深入研究 auth0 代码以查看它想要什么后,我给了它 global.crypto.subtle = {} 并且它似乎已经满足了它,错误消失了,测试通过了:

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
Run Code Online (Sandbox Code Playgroud)


JJS*_*JJS 0

模拟模块对我来说非常有效。

jest.mock('@auth0/auth0-spa-js');
Run Code Online (Sandbox Code Playgroud)