尝试嘲笑第三方库时出错

时间:2019-09-19 03:25:16

标签: reactjs jestjs react-testing-library

在第三方库上执行 "use strict"; const MongoClient = require('mongodb').MongoClient; function createDb() { return new Promise((resolve, reject) => { // return createDb a promise MongoClient.connect("mongodb://167.114.36.160:27017/testlambda2", { useNewUrlParser: true, useUnifiedTopology: true }, function (err, db) { if (err) { return reject(err); }; console.log("Database created!"); resolve(db); // like return :) }); }) } module.exports.consumo_monofasico = async (event, context) => { let result = {}; try { var db = await createDb(); // now you can get a response what has been "pushed" in `resolve` // TODO: Do some things with db instance // success response result = { statusCode: 200, body: JSON.stringify( { message: 'Test mongo!', resultado: "Connected!" }, null, 2 ), }; // close db connection db.close(); } catch (err) { // err is a error what has been pushed in `reject` :| // error response result = { statusCode: 500, // http error code body: JSON.stringify( { message: 'Test mongo!', resultado: err }, null, 2 ), }; } return result; }; 时遇到以下错误。有问题的库是jest.Mock

react-email-editor

jest.mock("react-email-editor", () => { return { Editor: <div>Email Editor</div>, }; }); 出现错误

jest.mock

如果我将其更改为 file.js: babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables. Invalid variable access: React Whitelisted objects: Array, ArrayBuffer, Boolean, DataView, Date, Error, EvalError, Float32Array, Float64Array, Function, Generator, GeneratorFunction, Infinity, Int16Array, Int32Array, Int8Array, InternalError, Intl, JSON, Map, Math, NaN, Number, Object, Promise, Proxy, RangeError, ReferenceError, Reflect, RegExp, Set, String, Symbol, SyntaxError, TypeError, URIError, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray, WeakMap, WeakSet, arguments, expect, jest, require, undefined, console, DTRACE_NET_SERVER_CONNECTION, DTRACE_NET_STREAM_END, DTRACE_HTTP_SERVER_REQUEST, DTRACE_HTTP_SERVER_RESPONSE, DTRACE_HTTP_CLIENT_REQUEST, DTRACE_HTTP_CLIENT_RESPONSE, COUNTER_NET_SERVER_CONNECTION, COUNTER_NET_SERVER_CONNECTION_CLOSE, COUNTER_HTTP_SERVER_REQUEST, COUNTER_HTTP_SERVER_RESPONSE, COUNTER_HTTP_CLIENT_REQUEST, COUNTER_HTTP_CLIENT_RESPONSE, global, process, Buffer, clearImmediate, clearInterval, clearTimeout, setImmediate, setInterval, setTimeout. Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable names prefixed with `mock` are permitted. ,我会得到:

doMock

注意:由于我使用的是Typescript,而react email编辑器没有类型定义,因此必须在项目根Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. 中创建一个存根定义文件,如下所示:

.types/react-email-editor/index.d.ts

编辑:在调用jest.mock之前的导入列表

declare module "react-email-editor";

1 个答案:

答案 0 :(得分:0)

我认为应该是:

jest.mock("react-email-editor", () => () => <div>Email Editor</div>);