gpt4 book ai didi

javascript - Jest 模拟函数的内部变量并改变函数的行为?

转载 作者:行者123 更新时间:2023-11-28 20:37:41 25 4
gpt4 key购买 nike

我对 Jest 经验很少,因为我正在尝试更改函数中的变量,因为如果函数的内部变量(baseUrl)更改为 null,我们预计会抛出错误,我的函数:

buildUrl.js:

export function buildUrl(contentId, data, options = {}) {
let baseUrl = config.has('imgBaseUrl') && config.get('imgBaseUrl');
if (!baseUrl) {
throw new Error('some error');
}
}

例如,我需要将 baseUrl 模拟为 null 值并进行测试

buildUrl.test.js

import {buildUrl} from "./buildURL";
....
it('throw an error when "baseUrl" is not configured', () => {

let mockBaseUrl = {baseUrl: null};
jest.mock('./buildURL', ()=> mockBaseUrl);
// jest.mock('../../../config/test', ()=>mockImgBaseUrl); // or mock the config to be used by the function?

expect(()=> buildUrl('1.44444', data[0], defaultOptions)).toThrow(
'some error'
);
});

使用 jest.fn() 的另一种方法没有按预期工作,也许我在这里遗漏了一些东西...

最佳答案

这是单元测试解决方案:

baseUrl.js:

import config from './config';

export function buildUrl(contentId, data, options = {}) {
let baseUrl = config.has('imgBaseUrl') && config.get('imgBaseUrl');
if (!baseUrl) {
throw new Error('some error');
}
console.log(baseUrl);
}

config.js:

export default {
config: {},
has(key) {
return !!config[key];
},
get(key) {
return config[key];
}
};

baseUrl.spec.js:

import { buildUrl } from './buildUrl';
import config from './config';

describe('buildUrl', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('should throw error when baseUrl is null', () => {
const defaultOptions = {};
const data = ['fake data'];
const hasSpy = jest.spyOn(config, 'has').mockReturnValueOnce(true);
const getSpy = jest.spyOn(config, 'get').mockReturnValueOnce(null);
expect(() => buildUrl('1.44444', data[0], defaultOptions)).toThrowError('some error');
expect(hasSpy).toBeCalledWith('imgBaseUrl');
expect(getSpy).toBeCalledWith('imgBaseUrl');
});

it('should log baseUrl', () => {
const defaultOptions = {};
const data = ['fake data'];
const hasSpy = jest.spyOn(config, 'has').mockReturnValueOnce(true);
const getSpy = jest.spyOn(config, 'get').mockReturnValueOnce('https://github.com/mrdulin');
const logSpy = jest.spyOn(console, 'log');
buildUrl('1.44444', data[0], defaultOptions);
expect(hasSpy).toBeCalledWith('imgBaseUrl');
expect(getSpy).toBeCalledWith('imgBaseUrl');
expect(logSpy).toBeCalledWith('https://github.com/mrdulin');
});
});

单元测试结果:

 PASS  src/stackoverflow/48006588/buildUrl.spec.js (8.595s)
buildUrl
✓ should throw error when baseUrl is null (9ms)
✓ should log baseUrl (7ms)

console.log node_modules/jest-mock/build/index.js:860
https://github.com/mrdulin

-------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------|----------|----------|----------|----------|-------------------|
All files | 80 | 83.33 | 33.33 | 77.78 | |
buildUrl.js | 100 | 83.33 | 100 | 100 | 3 |
config.js | 33.33 | 100 | 0 | 33.33 | 4,7 |
-------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 9.768s

源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/48006588

关于javascript - Jest 模拟函数的内部变量并改变函数的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48006588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com