Aik*_*Sat 3 javascript testing end-to-end node.js jestjs
我有一个任务是使用Jest测试 gRPC 客户端调用功能。典型的 Node.js 函数如下所示:
client.authenticate(request, meta, (error, response) => {
if (!error) {
console.log('REPLY FROM SERVER: ', response)
} else {
console.error(error)
}
})
Run Code Online (Sandbox Code Playgroud)
过程调用是回调函数,正如我们所见,我无法将响应对象导出到外部变量。上面的函数是我需要测试的函数。我需要检查该函数是否已被正确调用。我该如何用玩笑来做到这一点?现在已经挣扎了一段时间了。
您可以使用jest.spyOn(object, methodName)来模拟client.authenticate。
例如
\n\nindex.ts:
import { client } from \'./client\';\n\nexport function main() {\n const request = {};\n const meta = {};\n client.authenticate(request, meta, (error, response) => {\n if (!error) {\n console.log(\'REPLY FROM SERVER: \', response);\n } else {\n console.error(error);\n }\n });\n}\nRun Code Online (Sandbox Code Playgroud)\n\nclient.ts:
export const client = {\n authenticate(request, meta, callback) {\n console.log(\'real implementation\');\n },\n};\nRun Code Online (Sandbox Code Playgroud)\n\nindex.test.ts:
import { main } from \'./\';\nimport { client } from \'./client\';\n\ndescribe(\'62214949\', () => {\n it(\'should log correct response\', () => {\n const mResponse = \'mocked response\';\n const logSpy = jest.spyOn(console, \'log\');\n jest.spyOn(client, \'authenticate\').mockImplementationOnce((request, meta, callback) => {\n console.log(\'mocked implementation\');\n callback(null, mResponse);\n });\n main();\n expect(logSpy).toBeCalledWith(\'REPLY FROM SERVER: \', \'mocked response\');\n expect(client.authenticate).toBeCalledWith({}, {}, expect.any(Function));\n });\n\n it(\'should handle error\', () => {\n const mError = new Error(\'network\');\n const logSpy = jest.spyOn(console, \'error\');\n jest.spyOn(client, \'authenticate\').mockImplementationOnce((request, meta, callback) => {\n console.log(\'mocked implementation\');\n callback(mError);\n });\n main();\n expect(logSpy).toBeCalledWith(mError);\n expect(client.authenticate).toBeCalledWith({}, {}, expect.any(Function));\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n\n带有覆盖率报告的单元测试结果:
\n\n PASS stackoverflow/62214949/index.test.ts (10.557s)\n 62214949\n \xe2\x9c\x93 should log correct response (23ms)\n \xe2\x9c\x93 should handle error (8ms)\n\n console.log\n mocked implementation\n\n at CustomConsole.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)\n\n console.log\n REPLY FROM SERVER: mocked response\n\n at CustomConsole.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)\n\n console.log\n mocked implementation\n\n at CustomConsole.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)\n\n console.error\n Error: network\n at Object.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/stackoverflow/62214949/index.test.ts:18:20)\n at Object.asyncJestTest (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:100:37)\n at resolve (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/queueRunner.js:45:12)\n at new Promise (<anonymous>)\n at mapper (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/queueRunner.js:28:19)\n at promise.then (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/queueRunner.js:75:41)\n at process._tickCallback (internal/process/next_tick.js:68:7)\n\n 8 | console.log(\'REPLY FROM SERVER: \', response);\n 9 | } else {\n > 10 | console.error(error);\n | ^\n 11 | }\n 12 | });\n 13 | }\n\n at CustomConsole.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)\n at stackoverflow/62214949/index.ts:10:15\n at Object.<anonymous> (stackoverflow/62214949/index.test.ts:22:7)\n\n-----------|---------|----------|---------|---------|-------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n-----------|---------|----------|---------|---------|-------------------\nAll files | 90 | 100 | 66.67 | 90 | \n client.ts | 50 | 100 | 0 | 50 | 3 \n index.ts | 100 | 100 | 100 | 100 | \n-----------|---------|----------|---------|---------|-------------------\nTest Suites: 1 passed, 1 total\nTests: 2 passed, 2 total\nSnapshots: 0 total\nTime: 12.424s\nRun Code Online (Sandbox Code Playgroud)\n