Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.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 开玩笑+;supertest:如何重置模拟依赖项_Javascript_Express_Jestjs_Integration Testing_Supertest - Fatal编程技术网

Javascript 开玩笑+;supertest:如何重置模拟依赖项

Javascript 开玩笑+;supertest:如何重置模拟依赖项,javascript,express,jestjs,integration-testing,supertest,Javascript,Express,Jestjs,Integration Testing,Supertest,我无法在一个依赖项被一次使用后重置它的jest Mock。我感谢任何帮助或暗示 以下是我对supertest的api测试: import request from 'supertest'; import router from '../index'; const app = require('express')(); app.use(router); jest.mock('config', () => ({})); jest.mock('express-request-proxy',

我无法在一个依赖项被一次使用后重置它的jest Mock。我感谢任何帮助或暗示

以下是我对supertest的api测试:

import request from 'supertest';
import router from '../index';
const app = require('express')();
app.use(router);

jest.mock('config', () => ({}));

jest.mock('express-request-proxy', () => data => (req, res, next) =>
  res.json(data),
);

beforeEach(() => {
  jest.resetAllMocks();
});

describe('GET /', () => {
  it('should get all stuff', () =>
    request(app)
      .get('')
      .expect(200)
      .expect('Content-Type', /json/)
      .then(response => {
        expect(response.body).toMatchSnapshot();             // all good here 
      }));

  it('should get all stuff when FOO=bar', async () => {
    jest.mock('config', () => ({
      FOO: 'bar',
      get: key => key,
    }));

    // >> HERE <<
    // config.FOO is still undefined!
    // reseting mocks did not work ...
    await request(app)
      .get('')
      .expect(200)
      .expect('Content-Type', /json/)
      .then(response => {
        expect(response.body.query).toHaveProperty('baz');   // test fails ...
      });
  });
});
不能在函数作用域中使用,应在模块作用域中使用。如果要在测试用例的功能范围内安排模拟,则应该使用

我们还需要在执行每个测试用例之前使用

重置模块注册表-所有必需模块的缓存

这意味着您的
/config
模块注册表将被重置,以便在
jest.doMock('./config',()=>{…})语句之后,当您需要时,它将为每个测试用例返回不同的模拟值

{virtual:true}
选项意味着我没有安装
express request proxy
包,因此它不存在于我的
npm_modules
目录中。如果已经安装,则可以删除此选项

以下是单元测试解决方案:

index.js

const router=require('express')。路由器({mergeParams:true});
从“/config”导入配置;
从“快速请求代理”导入代理;
路由器.get(“”,(…args)=>{
console.log(config);
让query={};
如果(config.FOO=='bar'){
query.baz=true;
}
返回代理({url:'/stuff',query})(…args);
});
导出默认路由器;
config.js

导出默认值{
福:'',
};
index.test.js

从“supertest”导入请求;
mock('express-request-proxy',()=>(data)=>(req,res,next)=>res.json(data),{virtual:true});
在每个之前(()=>{
jest.resetAllMocks();
jest.reset模块();
});
描述('GET/',()=>{
它('应该得到所有的东西',()=>{
jest.doMock('./config',()=>({}));
const router=require('./索引')。默认值;
const-app=require('express')();
应用程序使用(路由器);
退货申请(app)
.get(“”)
.expect(200)
.expect('Content-Type',/json/)
。然后((响应)=>{
expect(response.body).toMatchSnapshot();
});
});
它('应该在FOO=bar'时获取所有内容,'异步()=>{
jest.doMock('./config',()=>({
默认值:{
福:“酒吧”,
get:(键)=>键,
},
__艾斯莫杜勒:没错,
}));
const router=require('./索引')。默认值;
const-app=require('express')();
应用程序使用(路由器);
等待请求(应用程序)
.get(“”)
.expect(200)
.expect('Content-Type',/json/)
。然后((响应)=>{
expect(response.body.query).toHaveProperty('baz');
});
});
});
100%覆盖率的单元测试结果报告:

PASS stackoverflow/61828748/index.test.js(11.966s)
得到/
✓ 应该得到所有的东西(8710ms)
✓ 当FOO=bar(24毫秒)时,应获取所有内容
console.log
{}
在stackoverflow/61828748/index.js:7:11
console.log
{FOO:'bar',get:[函数:get]}
在stackoverflow/61828748/index.js:7:11
----------|---------|----------|---------|---------|-------------------
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s
----------|---------|----------|---------|---------|-------------------
所有文件| 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
测试套件:1个通过,共1个
测试:2次通过,共2次
快照:1个通过,共1个
时间:13.268秒
index.test.js.snap

// Jest Snapshot v1

exports[`GET / should get all stuff 1`] = `
Object {
  "query": Object {},
  "url": "/stuff",
}
`;
源代码:

// Jest Snapshot v1

exports[`GET / should get all stuff 1`] = `
Object {
  "query": Object {},
  "url": "/stuff",
}
`;