在下面的Jest输出中,您可以看到有关模块分配的看似矛盾的信息:
ReferenceError: ajaxUrl is not def FAIL views/admin/__tests__/CurrentCouponListingSection.test.js
✕ getCountOfCurrentTargets (8ms)
✓ adjustResultMarker (5ms)
● getCountOfCurrentTargets
ReferenceError: ajaxUrl is not defined
9 |
10 | jQuery.ajax(
> 11 | ajaxUrl,
| ^
12 | {
13 | method: 'POST',
14 | async: false,
at Object.ajaxUrl [as getCountOfCurrentTargets] (views/admin/CurrentCouponListingSection.js:11:5)
at Object.getCountOfCurrentTargets (views/admin/__tests__/CurrentCouponListingSection.test.js:5:19)
console.log views/admin/CurrentCouponListingSection.js:4
http://test.com =====ajaxUrl=====
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 0.448s, estimated 1s
Ran all test suites related to changed files.
这是Jest调用的代码。为什么节点将module
视为未定义?
const testFile = require("../CurrentCouponListingSection.js");
test('getCountOfCurrentTargets', () => {
expect(testFile.getCountOfCurrentTargets($))
.toEqual(14);
});
。
if (typeof 'module' !== undefined) {
const ajaxUrl = 'http://test.com';
console.log(ajaxUrl, `=====ajaxUrl=====`); // http://test.com
}
const getCountOfCurrentTargets = (jQuery) => {
let countOfCurrentTargets = null;
jQuery.ajax(
ajaxUrl,
{
method: 'POST',
async: false,
dataType: 'json',
data: {
action: 'getCountOfCurrentTargets',
},
success: response => {
countOfCurrentTargets = response;
},
error: error => console.log(error, `=====error=====`),
}
);
return countOfCurrentTargets;
};
if (typeof 'module' !== undefined) {
module.exports.getCountOfCurrentTargets = getCountOfCurrentTargets;
}