Jest 平台
您可以選擇 Jest 的特定功能,並將它們用作獨立套件。以下是可用套件的清單
jest-changed-files
用於識別 git/hg 儲存庫中已修改檔案的工具。匯出兩個函式
getChangedFilesForRoots
傳回一個承諾,解析為包含已修改檔案和儲存庫的物件。findRepos
傳回一個承諾,解析為指定路徑中包含的儲存庫集合。
範例
const {getChangedFilesForRoots} = require('jest-changed-files');
// print the set of modified files since last commit in the current repo
getChangedFilesForRoots(['./'], {
lastCommit: true,
}).then(result => console.log(result.changedFiles));
您可以在 自述檔案 中閱讀更多關於 jest-changed-files
的資訊。
jest-diff
用於視覺化資料變化的工具。匯出一個函式,用於比較兩個任何類型的值,並傳回一個「美化列印」的字串,說明兩個參數之間的差異。
範例
const {diff} = require('jest-diff');
const a = {a: {b: {c: 5}}};
const b = {a: {b: {c: 6}}};
const result = diff(a, b);
// print diff
console.log(result);
jest-docblock
用於擷取和剖析 JavaScript 檔案頂端註解的工具。匯出各種函式,用於處理註解區塊中的資料。
範例
const {parseWithComments} = require('jest-docblock');
const code = `
/**
* This is a sample
*
* @flow
*/
console.log('Hello World!');
`;
const parsed = parseWithComments(code);
// prints an object with two attributes: comments and pragmas.
console.log(parsed);
您可以在 自述檔案中閱讀更多關於 jest-docblock
的資訊。
jest-get-type
用於識別任何 JavaScript 值的原始類型的模組。匯出一個函式,傳回一個字串,其中包含作為參數傳遞的值的類型。
範例
const {getType} = require('jest-get-type');
const array = [1, 2, 3];
const nullValue = null;
const undefinedValue = undefined;
// prints 'array'
console.log(getType(array));
// prints 'null'
console.log(getType(nullValue));
// prints 'undefined'
console.log(getType(undefinedValue));
jest-validate
用於驗證使用者提交的組態的工具。匯出一個函式,該函式採用兩個參數:使用者的組態和包含範例組態及其他選項的物件。傳回值是一個具有兩個屬性的物件
hasDeprecationWarnings
,一個布林值,表示提交的組態是否有不建議使用的警告,isValid
,一個布林值,表示組態是否正確。
範例
const {validate} = require('jest-validate');
const configByUser = {
transform: '<rootDir>/node_modules/my-custom-transform',
};
const result = validate(configByUser, {
comment: ' Documentation: http://custom-docs.com',
exampleConfig: {transform: '<rootDir>/node_modules/babel-jest'},
});
console.log(result);
您可以在 自述檔案中閱讀更多關於 jest-validate
的資訊。
jest-worker
用於任務平行化的模組。匯出一個類別 JestWorker
,該類別採用 Node.js 模組的路徑,並讓您呼叫模組的匯出方法,就像它們是類別方法一樣,傳回一個承諾,在指定的方法在分岔的程序中完成執行時解析。
範例
heavy-task.js
module.exports = {
myHeavyTask: args => {
// long running CPU intensive task.
},
};
main.js
async function main() {
const worker = new Worker(require.resolve('./heavy-task.js'));
// run 2 tasks in parallel with different arguments
const results = await Promise.all([
worker.myHeavyTask({foo: 'bar'}),
worker.myHeavyTask({bar: 'foo'}),
]);
console.log(results);
}
main();
您可以在 自述檔案中閱讀更多關於 jest-worker
的資訊。
pretty-format
匯出一個函式,將任何 JavaScript 值轉換為人類可讀的字串。支援所有內建的 JavaScript 類型,並允許透過使用者定義的外掛程式擴充套件特定應用程式的類型。
範例
const {format: prettyFormat} = require('pretty-format');
const val = {object: {}};
val.circularReference = val;
val[Symbol('foo')] = 'foo';
val.map = new Map([['prop', 'value']]);
val.array = [-0, Infinity, NaN];
console.log(prettyFormat(val));
您可以在 自述檔案 中閱讀更多關於 pretty-format
的資訊。