ECMAScript 6介绍了该let声明.我听说它被描述为"本地"变量,但我仍然不太确定它与var关键字的行为有何不同.
有什么区别?何时应该let使用var?
我有一个服务类
Service.js
class Service {
}
export default new Service();
Run Code Online (Sandbox Code Playgroud)
我正在尝试为此提供模拟实现.如果我使用这样的东西:
jest.mock('./Service', () => { ... my mock stuff });
Run Code Online (Sandbox Code Playgroud)
它工作正常,但是我无法访问在mock之外声明的任何变量,这有点限制,因为我想重新配置mock返回的内容等.
我试过这个(受其他StackOverflow文章的启发:使用Jest模拟的服务导致"jest.mock()的模块工厂不允许引用任何超出范围的变量"错误)
import service from './Service';
jest.mock('./Service', () => jest.fn);
service.mockImplementation(() => {
return { ... mock stuff }
);
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我试图运行它时,我得到以下错误:
TypeError: _Service2.default.mockImplementation is not a function
Run Code Online (Sandbox Code Playgroud) 我有一个模拟对象,我用来模拟react-native:
const MyMock = {
MockA: {
methodA: jest.genMockFn()
},
MockB: {
ObjectB: {
methodA: jest.genMockFn(),
methodB: jest.genMockFn(),
}
}
};
jest.mock('react-native', () => {
return MyMock;
});
Run Code Online (Sandbox Code Playgroud)
我在外面声明了这个对象,jest.mock因为我以后在测试中也需要它:
describe('MyClass', () => {
beforeEach(() => {
MyMock.MockB.ObjectB.methodA.mockClear();
MyMock.MockB.ObjectB.methodB.mockClear();
});
//some other code
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
const MyMock = {
MockA: {
methodA: jest.genMockFn()
},
MockB: {
ObjectB: {
methodA: jest.genMockFn(),
methodB: jest.genMockFn(),
}
}
};
jest.mock('react-native', () => {
return MyMock;
});
Run Code Online (Sandbox Code Playgroud)
问题是我jest.mock()在外面宣布MyMock.但就我所见,我别无选择.
那么如何在保持 …
在具有 Composition API(和 Vue 3)的 .vue 文件中,设置路由器:
const router = useRouter()
Run Code Online (Sandbox Code Playgroud)
在玩笑测试中挂载 .vue 文件:
const wrapper = mount(Lookup)
Run Code Online (Sandbox Code Playgroud)
执行时,产生:
console.warn
[Vue warn]: injection "Symbol([vue-router]: router)" not found.
at <Anonymous ref="VTU_COMPONENT" >
at <VTUROOT>
Run Code Online (Sandbox Code Playgroud)
模拟它会产生相同的输出:
useRouter().push = jest.fn()
Run Code Online (Sandbox Code Playgroud)
设置提供相同输出的结果:
import { useRouter } from 'vue-router'
...
const wrapper = mount(Lookup, {
global: {
plugins: [useRouter],
provide: {
router: {},
},
},
})
Run Code Online (Sandbox Code Playgroud) 尝试运行yarn test并尝试了许多版本的节点(8 和 11),反应(57,58),但无法修复此错误。
FAIL __tests__/index.ios.js
? Test suite failed to run
/xyz/mobile-react/node_modules/react-native/jest/setup.js: babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: MockNativeMethods
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, …Run Code Online (Sandbox Code Playgroud) jestjs ×4
javascript ×3
react-native ×2
ecmascript-6 ×1
let ×1
scope ×1
unit-testing ×1
var ×1
vue-router ×1
vue.js ×1