如何用玩笑测试线性节点脚本?
how to test a linear node script with jest?
我正在启动一个简单的脚本来为 Ionic 项目签署 APK,我想用它进行一些 TDD。问题?我无法找到执行此操作的方法,因为我没有导出任何功能,我只会做 ts-node my-script
有办法吗?或者我是否必须将所有内容包装在一个导出的函数中,然后从另一个文件中调用它来测试它?
例如,到目前为止的结构是:
import * as colors from 'colors';
import * as prompCreator from 'prompt-sync';
const prompt = prompCreator();
const password = prompt('Sign apk password?: ').trim();
if (!password || password.length === 0) {
console.error(colors.bgRed.yellow(' You need a password to sign the APK '));
process.exit(0);
}
(之后我决定先写规范测试)
您可以模拟 prompt-sync
包的 prompCreator
和 prompt
方法以及 colors.bgRed.yellow
方法。
例如
index.js
:
import colors from 'colors';
import prompCreator from 'prompt-sync';
const prompt = prompCreator();
const password = prompt('Sign apk password?: ').trim();
if (!password || password.length === 0) {
console.error(colors.bgRed.yellow(' You need a password to sign the APK '));
process.exit(0);
}
index.test.js
:
import prompCreator from 'prompt-sync';
import colors from 'colors';
jest.mock(
'colors',
() => {
return {
bgRed: {
yellow: jest.fn(),
},
};
},
{ virtual: true },
);
jest.mock(
'prompt-sync',
() => {
const mPrompt = jest.fn();
return jest.fn(() => mPrompt);
},
{ virtual: true },
);
describe('61950048', () => {
afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});
afterAll(() => {
jest.resetAllMocks();
});
it('should pass', () => {
jest.spyOn(process, 'exit').mockImplementation();
colors.bgRed.yellow.mockReturnValueOnce('network');
const errorSpy = jest.spyOn(console, 'error').mockImplementation();
const mPrompt = prompCreator();
mPrompt.mockReturnValueOnce('');
require('./');
expect(errorSpy).toBeCalledWith('network');
expect(colors.bgRed.yellow).toBeCalledWith(' You need a password to sign the APK ');
expect(process.exit).toBeCalledWith(0);
});
});
带有覆盖率报告的单元测试结果:
PASS Whosebug/61950048/index.test.js (9.582s)
61950048
✓ should pass (8ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 50 | 100 | 100 |
index.js | 100 | 50 | 100 | 100 | 8
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.196s
我正在启动一个简单的脚本来为 Ionic 项目签署 APK,我想用它进行一些 TDD。问题?我无法找到执行此操作的方法,因为我没有导出任何功能,我只会做 ts-node my-script
有办法吗?或者我是否必须将所有内容包装在一个导出的函数中,然后从另一个文件中调用它来测试它?
例如,到目前为止的结构是:
import * as colors from 'colors';
import * as prompCreator from 'prompt-sync';
const prompt = prompCreator();
const password = prompt('Sign apk password?: ').trim();
if (!password || password.length === 0) {
console.error(colors.bgRed.yellow(' You need a password to sign the APK '));
process.exit(0);
}
(之后我决定先写规范测试)
您可以模拟 prompt-sync
包的 prompCreator
和 prompt
方法以及 colors.bgRed.yellow
方法。
例如
index.js
:
import colors from 'colors';
import prompCreator from 'prompt-sync';
const prompt = prompCreator();
const password = prompt('Sign apk password?: ').trim();
if (!password || password.length === 0) {
console.error(colors.bgRed.yellow(' You need a password to sign the APK '));
process.exit(0);
}
index.test.js
:
import prompCreator from 'prompt-sync';
import colors from 'colors';
jest.mock(
'colors',
() => {
return {
bgRed: {
yellow: jest.fn(),
},
};
},
{ virtual: true },
);
jest.mock(
'prompt-sync',
() => {
const mPrompt = jest.fn();
return jest.fn(() => mPrompt);
},
{ virtual: true },
);
describe('61950048', () => {
afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});
afterAll(() => {
jest.resetAllMocks();
});
it('should pass', () => {
jest.spyOn(process, 'exit').mockImplementation();
colors.bgRed.yellow.mockReturnValueOnce('network');
const errorSpy = jest.spyOn(console, 'error').mockImplementation();
const mPrompt = prompCreator();
mPrompt.mockReturnValueOnce('');
require('./');
expect(errorSpy).toBeCalledWith('network');
expect(colors.bgRed.yellow).toBeCalledWith(' You need a password to sign the APK ');
expect(process.exit).toBeCalledWith(0);
});
});
带有覆盖率报告的单元测试结果:
PASS Whosebug/61950048/index.test.js (9.582s)
61950048
✓ should pass (8ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 50 | 100 | 100 |
index.js | 100 | 50 | 100 | 100 | 8
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.196s