我有一个带有许多辅助功能的文件:
// extract repo name from url
function getRepoName(url) {
let repoName = url.split("/");
repoName = repoName[repoName.length - 1];
repoName = repoName.replace(".git", "");
repoName = repoName.replace("\n", "");
return repoName;
}
// delete patch file
async function deletePatchFile() {
return new Promise((resolve, reject) => {
exec("rm myPatch.patch", error => {
if (error !== null) {
console.log("exec error deleting patch file: ", error);
reject(error);
}
resolve("successfully removed patch file");
});
});
}
async function getRepoInfo() {
return new Promise((resolve, reject) => {
exec("git config --get remote.origin.url", (error, stdout) => {
if (error !== null) {
console.log(`exec error: ${error}`);
reject(error);
}
const repoUrl = stdout.replace("\n", "");
resolve(repoUrl);
});
});
}
async function getGitEmail() {
return new Promise((resolve, reject) => {
exec("git config --global user.email", (error, stdout) => {
if (error !== null) {
console.log(`exec error: ${error}`);
reject(error);
} else {
resolve(stdout.replace("\n", ""));
}
});
});
}
async function getGitUser() {
return new Promise((resolve, reject) => {
exec("git config --global user.name", (error, stdout) => {
if (error !== null) {
console.log(`exec error: ${error}`);
reject(error);
} else {
resolve(stdout.replace("\n", ""));
}
});
});
我有一个测试每个功能的单元测试。除getRepoName
外,所有测试均通过。我收到一个错误:
serverScript.getRepoName不是函数。
在模块内部,我打印了typeof
作为功能参数,以确认它确实是字符串。
const serverScript = require('../../../scripts/app');
describe('Test Class', () => {
beforeAll(() => {
});
afterAll(() => {
});
beforeEach(() => {
});
afterEach(() => {
});
console.log("before tests")
it('getRepoName', () => {
const repoName = serverScript.getRepoName("git@github.com:me/repo");
execpt(repoName.toBe('repo'));
});
it('getGitUser', async () => {
serverScript.getGitUser = jest.fn().mockReturnValue(Promise.resolve('me'));
});
it('getGitEmail', async () => {
serverScript.getGitEmail = jest.fn().mockReturnValue(Promise.resolve('my@email.com'))
});
it('getRepoInfo', async () => {
serverScript.getRepoInfo = jest.fn().mockReturnValue(Promise.resolve('git@github.com:me/repo'));
});
it('getBranchName', async () => {
serverScript.getBranchName = jest.fn().mockReturnValue(Promise.resolve('test2'));
});
it('getGitCommits', async () => {
serverScript.getGitCommits = jest.fn().mockReturnValue(Promise.resolve(['+ 7c7ca2c4284719c278f66eb61301daaa133a51fb', '+ 60989f60b8addddbbfe8c8ee415812b6783ae00e']))
});
it('getGitPatchFromLocal', async () => {
serverScript.getGitPatchFromLocal = jest.fn().mockReturnValue(Promise.resolve( "diff --git a/Makefile b/Makefile\
index 411cc21..fd24145 100644\
--- a/Makefile\
+++ b/Makefile\
.....
"))
});
it('getGitPatchFromCommits', async () => {
serverScript.getGitPatchFromCommits = jest.fn().mockReturnValue(Promise.resolve("diff --git a/README.rst b/README.rst\
index 845a0ce..057c4bd 100644\
--- a/README.rst\
+++ b/README.rst\
...."))
});
it('deletePatchFile', async () => {
serverScript.deletePatchFile = jest.fn().mockReturnValue(Promise.resolve('successfully removed patch file'));
});
答案 0 :(得分:1)
这是工作示例:
helper.js
:
import { exec } from 'child_process';
// extract repo name from url
function getRepoName(url) {
let repoName = url.split('/');
repoName = repoName[repoName.length - 1];
repoName = repoName.replace('.git', '');
repoName = repoName.replace('\n', '');
return repoName;
}
// delete patch file
async function deletePatchFile() {
return new Promise((resolve, reject) => {
exec('rm myPatch.patch', (error) => {
if (error !== null) {
console.log('exec error deleting patch file: ', error);
reject(error);
}
resolve('successfully removed patch file');
});
});
}
async function getRepoInfo() {
return new Promise((resolve, reject) => {
exec('git config --get remote.origin.url', (error, stdout) => {
if (error !== null) {
console.log(`exec error: ${error}`);
reject(error);
}
const repoUrl = stdout.replace('\n', '');
resolve(repoUrl);
});
});
}
async function getGitEmail() {
return new Promise((resolve, reject) => {
exec('git config --global user.email', (error, stdout) => {
if (error !== null) {
console.log(`exec error: ${error}`);
reject(error);
} else {
resolve(stdout.replace('\n', ''));
}
});
});
}
async function getGitUser() {
return new Promise((resolve, reject) => {
exec('git config --global user.name', (error, stdout) => {
if (error !== null) {
console.log(`exec error: ${error}`);
reject(error);
} else {
resolve(stdout.replace('\n', ''));
}
});
});
}
module.exports = {
getRepoName,
deletePatchFile,
getRepoInfo,
getGitEmail,
getGitUser,
};
helper.test.js
:
const serverScript = require('./helper');
describe('Test Class', () => {
beforeAll(() => {});
afterAll(() => {});
beforeEach(() => {});
afterEach(() => {});
console.log('before tests');
it('getRepoName', () => {
const repoName = serverScript.getRepoName('git@github.com:me/repo');
expect(repoName).toBe('repo');
});
it('getGitUser', async () => {
serverScript.getGitUser = jest.fn().mockReturnValue(Promise.resolve('me'));
});
it('getGitEmail', async () => {
serverScript.getGitEmail = jest.fn().mockReturnValue(Promise.resolve('my@email.com'));
});
it('getRepoInfo', async () => {
serverScript.getRepoInfo = jest.fn().mockReturnValue(Promise.resolve('git@github.com:me/repo'));
});
it('getBranchName', async () => {
serverScript.getBranchName = jest.fn().mockReturnValue(Promise.resolve('test2'));
});
it('getGitCommits', async () => {
serverScript.getGitCommits = jest
.fn()
.mockReturnValue(
Promise.resolve(['+ 7c7ca2c4284719c278f66eb61301daaa133a51fb', '+ 60989f60b8addddbbfe8c8ee415812b6783ae00e']),
);
});
it('getGitPatchFromLocal', async () => {
serverScript.getGitPatchFromLocal = jest
.fn()
.mockReturnValue(
Promise.resolve(
'diff --git a/Makefile b/Makefile\
index 411cc21..fd24145 100644\
--- a/Makefile\
+++ b/Makefile\
..... \
',
),
);
});
it('getGitPatchFromCommits', async () => {
serverScript.getGitPatchFromCommits = jest
.fn()
.mockReturnValue(
Promise.resolve(
'diff --git a/README.rst b/README.rst\
index 845a0ce..057c4bd 100644\
--- a/README.rst\
+++ b/README.rst\
....',
),
);
});
it('deletePatchFile', async () => {
serverScript.deletePatchFile = jest.fn().mockReturnValue(Promise.resolve('successfully removed patch file'));
});
});
单元测试结果:
PASS src/stackoverflow/59311260/helper.test.js (12.245s)
Test Class
✓ getRepoName (3ms)
✓ getGitUser (1ms)
✓ getGitEmail (1ms)
✓ getRepoInfo (1ms)
✓ getBranchName (1ms)
✓ getGitCommits (1ms)
✓ getGitPatchFromLocal
✓ getGitPatchFromCommits (1ms)
✓ deletePatchFile (1ms)
console.log src/stackoverflow/59311260/helper.test.js:9
before tests
Test Suites: 1 passed, 1 total
Tests: 9 passed, 9 total
Snapshots: 0 total
Time: 13.961s
源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59311260