我在 client/index.js
中有一个函数这是使用 axios 发出请求
import axios from "axios";
const createRequest = async (url, method) => {
const response = await axios({
url: url,
method: method
});
return response;
};
export default { createRequest };
我想用 jest
测试这个功能,所以我创建了 client/index.test.js
import { jest } from "@jest/globals";
import axios from "axios";
import client from "./";
jest.doMock('axios', () => jest.fn(() => Promise.resolve()));
describe("Client", () => {
it("should call axios and return a response", async () => {
const response = await client.createRequest('http://localhost/', 'GET');
expect(axios).toHaveBeenCalled();
})
})
但是当我尝试运行它时,测试失败了,我收到了这个错误connect ECONNREFUSED 127.0.0.1:80
如果我使用 mock 而不是 doMock,那么我会收到此错误 -ReferenceError: /Users/project/src/client/index.test.js: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: jest
package.json
-{
"name": "project",
"version": "0.0.1",
"main": "index.js",
"author": "author",
"license": "MIT",
"private": false,
"type": "module",
"scripts": {
"start": "node --experimental-json-modules --experimental-specifier-resolution=node ./src/index.js",
"start:dev": "nodemon --experimental-json-modules --experimental-specifier-resolution=node ./src/index.js",
"test": "node --experimental-vm-modules node_modules/.bin/jest",
"test:dev": "node --experimental-vm-modules node_modules/.bin/jest --watch",
"test:coverage": "node --experimental-vm-modules node_modules/.bin/jest --coverage",
"lint": "eslint --fix .",
"pretty": "prettier --write ."
},
"dependencies": {
"axios": "^0.21.1",
"express": "^4.17.1"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"eslint": "^7.23.0",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"supertest": "^6.1.3"
},
"jest": { "testEnvironment": "node" }
}
我在 Node 环境中运行它, Node 版本是 14.16.0
, Jest 版本是 26.6.3
.请帮助确定这种方法有什么问题以及如何解决它。
最佳答案
jest.doMock(moduleName, factory, options)
方法不会自动提升到代码块的顶部。这意味着 axios
createRequest
中使用的函数功能仍将是原始功能。
您需要使用 jest.mock()
.
例如。index.js
:
import axios from 'axios';
const createRequest = async (url, method) => {
const response = await axios({
url: url,
method: method,
});
return response;
};
export default { createRequest };
index.test.js
:import axios from 'axios';
import client from './';
jest.mock('axios', () => jest.fn(() => Promise.resolve('teresa teng')));
describe('Client', () => {
it('should call axios and return a response', async () => {
const response = await client.createRequest('http://localhost/', 'GET');
expect(axios).toHaveBeenCalled();
expect(response).toEqual('teresa teng');
});
});
单元测试结果: PASS examples/67101502/index.test.js (11.503 s)
Client
✓ should call axios and return a response (4 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 13.62 s
关于javascript - 如何用 Jest 模拟 Axios?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67101502/