Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jest.unmack测试承诺_Javascript_Reactjs_Jestjs_Es6 Promise - Fatal编程技术网

Javascript 使用jest.unmack测试承诺

Javascript 使用jest.unmack测试承诺,javascript,reactjs,jestjs,es6-promise,Javascript,Reactjs,Jestjs,Es6 Promise,我正在使用Auth0管理React应用程序的身份验证。下面是我要测试的函数: login(username: string, password: string) { return new Promise((resolve, reject) => { this.auth0.client.login({ realm: 'Username-Password-Authentication', username,

我正在使用Auth0管理React应用程序的身份验证。下面是我要测试的函数:

login(username: string, password: string) {
    return new Promise((resolve, reject) => {
        this.auth0.client.login({
            realm: 'Username-Password-Authentication',
            username,
            password,
        }, (err, authResult) => {
            if (err) {
                reject(err);
            }
            if (authResult && authResult.idToken && authResult.accessToken) {
                resolve(authResult.idToken);
            }
        });
    });
}
auth0
按以下方式实例化:

constructor(clientID: string, domain: string, redirectUri: string) {
    // Configure auth0
    this.auth0 = new auth0.WebAuth({
        clientID,
        domain,
        responseType: 'token id_token',
        redirectUri,
    });
}
我有以下两项测试:

  • 我想看看是否可以实例化AuthService类
  • 我想看看我是否可以使用用户名/密码组合登录
以下是我编写的测试文件:

jest.unmock('../AuthService');

import AuthService from '../AuthService';

describe('Auth0 Library', () => {
    test('Should be able to instantiate', () => {
        const auth0 = new AuthService('clientID', 'domain');
        expect(auth0).toEqual(expect.anything());
    });
});

describe('Auth0 Login', () => {
    test('Fetch token for existing user', () => {
        const auth0 = new AuthService('clientID', 'domain');
        auth0.login('email', 'pw')
            .then((idToken) => {
                console.log('idToken ', idToken);
                expect(auth0).toEqual(expect.anything());
            });
    });
});
第一个测试按预期运行。然而,第二个测试永远不起作用。返回的承诺似乎从未得到解决,我也从未看到console.log


有人能给我解释一下我做错了什么吗?说到写笑话测试,我还是个新手

下面是一个单元测试解决方案,用于同时使用
Promise
和error-first回调

文件夹结构:

。
├── AuthService.spec.ts
├── AuthService.ts
└── auth0
├── WebAuth.ts
└── 索引
1个目录,4个文件
例如

AuthService.ts

import*作为auth0从“/auth0”导入;
导出类身份验证服务{
私人授权0:任何;
构造函数(clientID:string、domain:string、redirectUri:string){
this.auth0=new auth0.WebAuth({
clientID,
领域,
responseType:“令牌id\u令牌”,
重定向URI
});
}
公共登录(用户名:string,密码:string){
返回新承诺((解决、拒绝)=>{
this.auth0.client.login(
{
领域:'用户名密码身份验证',
用户名,
密码
},
(错误,authResult)=>{
如果(错误){
拒绝(错误);
}
if(authResult&&authResult.idToken&&authResult.accessToken){
解析(authResult.idToken);
}
}
);
});
}
}
auth0/WebAuth.ts

导出类WebAuth{
公共客户端={
登录(选项、回调){
回调(null,{});
}
};
构造函数(…args:any){}
}
auth0/index.ts

从“/WebAuth”导出*;
AuthService.spec.ts

从“/AuthService”导入{AuthService};
const authService=new authService('clientid','domain','redirectUri');
描述('AuthService',()=>{
描述(“#登录”,()=>{
之后(()=>{
开玩笑。恢复记忆();
});
它('应成功登录并返回id令牌',异步完成=>{
让loginCallback;
jest.spyOn(authService['auth0']['client'],'login').mockImplementation((选项,回调)=>{
loginCallback=回调;
完成();
});
const actualValue=authService.login('username','password');
constMauthResult={idToken:'123',accessToken:'321'};
loginCallback(空,mAuthResult);
wait expect(实际值).resolves.toBe('123');
});
它('should login failed',async done=>{
让loginCallback;
jest.spyOn(authService['auth0']['client'],'login').mockImplementation((选项,回调)=>{
loginCallback=回调;
完成();
});
const actualValue=authService.login('username','password');
const mError=新错误(“网络错误”);
loginCallback(mError,null);
等待期望(实际值)。拒绝。TothRower错误(mError);
});
});
});
AuthService.ts的100%覆盖率的单元测试结果

PASS src/stackoverflow/42137891/AuthService.spec.ts
授权服务
#登录
✓ 应成功登录并返回id令牌(5ms)
✓ 如果登录失败(2ms)
---------------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
---------------------|----------|----------|----------|----------|-------------------|
所有文件| 95 | 100 | 87.5 | 94.12 ||
42137891待办事项| 100 | 100 | 100 | 100 ||
AuthService.ts | 100 | 100 | 100 | 100 ||
42137891待办事项/授权0 | 85.71 | 100 | 66.67 | 83.33 ||
WebAuth.ts | 83.33 | 100 | 66.67 | 80 | 4|
index.ts | 100 | 100 | 100 | 100 ||
---------------------|----------|----------|----------|----------|-------------------|
测试套件:1个通过,共1个
测试:2次通过,共2次
快照:共0个
时间:3.728秒

源代码:

这是一起使用
Promise
和error-first回调的单元测试解决方案

文件夹结构:

。
├── AuthService.spec.ts
├── AuthService.ts
└── auth0
├── WebAuth.ts
└── 索引
1个目录,4个文件
例如

AuthService.ts

import*作为auth0从“/auth0”导入;
导出类身份验证服务{
私人授权0:任何;
构造函数(clientID:string、domain:string、redirectUri:string){
this.auth0=new auth0.WebAuth({
clientID,
领域,
responseType:“令牌id\u令牌”,
重定向URI
});
}
公共登录(用户名:string,密码:string){
返回新承诺((解决、拒绝)=>{
this.auth0.client.login(
{
领域:'用户名密码身份验证',
用户名,
密码
},
(错误,authResult)=>{
如果(错误){
拒绝(错误);
}
if(authResult&&authResult.idToken&&authResult.accessToken){
解析(authResult.idToken);
}
}
);
});
}
}
auth0/WebAuth.ts

导出类WebAuth{
公共客户端={
登录(选项、回调){
回调(null,{});
}
};
构造函数(…args:any){}
}
auth0/ind