我将我的代码库(最初在ES6中使用AVA测试运行器)更改为TypeScript和Jest。这是存储库:https://github.com/jy95/mediaScan/tree/test-fix 。出于奇怪的原因,当我开玩笑时,2次测试失败。
以下是单元测试的代码:
// __tests__/methods/addNewPath.ts
// mock from jest
jest.mock('fs');
jest.mock('filehound');
// imports
import * as path from 'path';
import {files, folders, MediaScan} from '../__helpers__/_constants';
describe('addNewPath', () => {
beforeAll(() => {
// Set up some mocked out file info before each test
require('fs').__setMockPaths(folders);
require('filehound').__setResult(files);
});
// TESTS
/** @test {MediaScan#addNewPath} */
test('existent paths', async () => {
let libInstance = new MediaScan();
await expect(libInstance.addNewPath(...folders)).resolves;
expect(libInstance.hasPathsProvidedByUser()).toBeTruthy();
});
});
另一个:
// mock from jest
'use strict';
jest.mock('fs');
jest.mock('filehound');
// imports
import {basename} from 'path';
import {parse as nameParser} from 'parse-torrent-title';
import {files, folders, MediaScan} from '../__helpers__/_constants';
describe('toJSON', () => {
beforeAll(() => {
// Set up some mocked out file info before each test
require('fs').__setMockPaths(folders);
require('filehound').__setResult(files);
});
/** @test {MediaScan#toJSON} */
test('return a valid stringified JSON', async () => {
const expectedJsonString = JSON.stringify({
paths: folders,
allFilesWithCategory: [
[
files[2],
'MOVIES',
],
[
files[0],
'TV_SERIES',
],
[
files[1],
'TV_SERIES',
],
],
movies: [
Object.assign(nameParser(basename(files[2])), {
filePath: files[2],
}),
],
series: [
[
'The Blacklist',
[
Object.assign(nameParser(basename(files[0])), {
filePath: files[0],
}),
Object.assign(nameParser(basename(files[1])), {
filePath: files[1],
}),
],
],
],
});
let libInstance = new MediaScan();
await expect(libInstance.addNewPath(...folders)).resolves;
await expect(libInstance.scan()).resolves;
const dataFromInstance = libInstance.toJSON();
expect(JSON.stringify(JSON.parse(dataFromInstance))).toEqual(expectedJsonString);
});
});
模拟(fs.js):
// __mocks__/fs.js
'use strict';
const fs = jest.genMockFromModule('fs');
// temp files
let mockPaths = [];
function access(path, mode, callback) {
if (!mockPaths.includes(path)) {
callback(new Error("VAUDOU"));
}
callback();
}
function __setMockPaths(pathArray) {
mockPaths = pathArray;
}
fs.access = access;
fs.__setMockPaths = __setMockPaths;
module.exports = fs;
另一个mock(filehound.js):
// __mocks__/filehound.js
'use strict';
// need to manually mock these function as there is no solution : https://github.com/facebook/jest/issues/5589
// here ; a short truncated copy of babel stuff of this class
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
}
var _createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
// truncated version of this stuff
var FileHound = function () {
// temp files
let result;
function FileHound() {
var _this = _possibleConstructorReturn(this, (FileHound.__proto__ || Object.getPrototypeOf(FileHound)).call(this));
return _this;
}
_createClass(FileHound, [
{
key: 'paths',
value: function paths() {
return this;
}
},
{
key: 'ext',
value: function ext() {
return this;
}
},
{
key: 'find',
value: function find() {
if (result !== undefined) {
return Promise.resolve(result);
}
// if nothing provided , throw error
return Promise.reject("VAUDOU");
}
}], [{
key: 'create',
value: function create() {
return new FileHound();
}
}, {
key: '__setResult',
value: function (ExpectedResult) {
result = ExpectedResult;
}
}]);
return FileHound;
}();
module.exports = FileHound;
当您使用此功能的其他测试没有问题时,您能否帮助我找出为什么此测试失败?谢谢