簡體   English   中英

使用 babel-plugin-rewire 測試私有非引用函數

[英]Using babel-plugin-rewire to test private non-referenced functions

我在 main.test.js 中使用 babel-plugin-rewire ( https://www.npmjs.com/package/babel-plugin-rewire ) 來測試 main.js 中的非導出函數。 這一直在工作,除非在 main.js 中沒有引用該函數; 在這種情況下,我收到以下錯誤:TypeError: _get__(...) is not a function。

只有在 main.js 中添加對函數的引用后,我才能在測試文件中訪問它(即使我實際上沒有調用該函數,它也能工作)。 但是我不想對 main.js 進行任何更改。 這是 babel-plugin-rewire 的預期行為,是否有解決方法?

//main.js
function test123() {
    return true;
}
test123; //Cannot access function in test file unless I add this reference!

//main.test.js
const test123 = require('./main').__get__('test123');

test('test123', () => {
  expect(test123()).toEqual(true);
});

您可以在 main.js 中測試非導出函數,但非導出函數需要在同一文件中的至少一個導出函數中使用。

在您的情況下,這將在沒有參考的情況下工作。

//main.js
export default function foo() {
   test123();
}
function test123() {
    return true;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM