开玩笑地窥探模块功能



我正在用jest编写测试,其中我想监视一些lodash函数,这些函数我在模块中单独导入(而不是将整个lodash模块作为_导入(,例如

/** matrix.js **/
import shuffle from 'lodash/shuffle'
import pick from 'lodash/pick'
// ...
/**
* Shuffles the order of the rows in the matrix. If a column/variable name
* is specified, only the rows in this column are shuffled.
*
* @export
* @param {array} matrix The matrix to be shuffled
* @param {array} columns  Array containing the variable/column to be shuffled
* @returns {array}
*/
export function shuffleVert (matrix, columns) {
if (typeof (columns) === 'undefined' || (isArray(columns) && columns.length === 0)) {
return shuffle(matrix)
} else if (!isArray(columns)) {
throw new TypeError('Invalid argument for columns specified to shuffleVert. Expects an array containing column names')
} else {
let grouped = unstack(matrix)
let cols = pick(grouped, columns)
cols = Object.entries(cols).reduce((prev, [key, values]) => {
prev[key] = shuffle(values)
return prev
}, {})
return stack({ ...grouped, ...cols })
}

shuffleVert函数搅乱矩阵的所有行,或者只搅乱指定列的行。因为用随机输出测试函数很困难,据我所知,我只想测试lodash的shuffle和pick函数是否在测试函数中被调用。

我目前已经在我的测试模块中实现了一个工作间谍程序,但我不认为它是传统的或有效的,我只是认为必须有更好的方法来做到这一点。。。

/* matrix.test.js */
import {
shuffleVert,
} from 'matrix'
/** Generate a mock functions to spy on lodash */
const mockShuffle = jest.fn()
jest.mock('lodash/shuffle', () => a => {
const shuffle = jest.requireActual('lodash/shuffle')
mockShuffle()
return shuffle(a)
})
const mockPick = jest.fn()
jest.mock('lodash/pick', () => (a, b) => {
const pick = jest.requireActual('lodash/pick')
mockPick()
return pick(a, b)
})
describe('reverseRows', () => {
let srcMatrix
beforeEach(() => {
srcMatrix = [
{ number: 1, word: 'one' },
{ number: 2, word: 'two' },
{ number: 3, word: 'three' }
]
mockShuffle.mockClear()
mockPick.mockClear()
})
it('should shuffle the rows of the entire matrix with no argument for columns', () => {
shuffleVert(srcMatrix)
// 2 is weird, but seems correct.
// It appears the shuffle function calls itself recursively
expect(mockShuffle).toHaveBeenCalledTimes(2)
})
it('should only shuffle the rows of columns that were specified', () => {
shuffleVert(srcMatrix, ['word'])
expect(mockShuffle).toHaveBeenCalledTimes(2)
expect(mockPick).toHaveBeenCalledTimes(2)
})
})

我知道jest有一个spyOn功能,但它似乎只适用于对象方法,因此

import * as lodash from 'lodash'
const shuffleSpy = jest.spyOn(lodash, 'shuffle')

导致错误Cannot spyOn on a primitive value; undefined given

通常,窥探模块方法的最佳方式是什么?有没有更好的实现方式来实现我想要实现的目标?还是已经是这样了?

请尝试一下,并让我知道它是否适用于

import { shuffleVert } from 'matrix';
const shuffleVertRef = {
shuffleVert
};
let spyShuffleVert;
beforeEach(() => {
spyShuffleVert = jest.spyOn(shuffleVertRef, "shuffleVert");
}
afterEach(() => {
jest.clearAllMocks();
}
it('should shuffle the rows of the entire matrix with no argument for columns', () = {
let srcMatrix = [
{ number: 1, word: 'one' },
{ number: 2, word: 'two' },
{ number: 3, word: 'three' }
];
shuffleVert(srcMatrix);
expect(spyShuffleVert).toHaveBeenCalledTimes(2);
}

然后,如果您想检查任何其他函数的执行时间,只需将它们添加到/the常量中,并在下面spyOn即可。

我也没有完全理解这一点,但从我读到的一点来看,最新版本的JavaScript有一个被称为"单一真相来源"的原则,这是某些事情正常工作所必需的。

最新更新