当使用类名(https://www.npmjs.com/package/classnames)库时,我在Jest上运行测试用例时遇到问题:
它抛出错误:classnames_1.default 不是函数
它正在网站上使用 webpack 本身..所以我不想改变导入。
我可以选择模拟类名以进行测试以提供类似的行为。有没有其他方法可以解决这个问题?
有没有人遇到过这个问题?
这是我在 package.json 中的开玩笑配置:
"jest": {
"globals": {
"API_HOST": "",
"DEV": false
},
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/XXX-framework/jestTypeScriptPreProcessor.js"
},
"testRegex": "(/tests/.*|\.(test|spec))\.(js|jsx|ts|tsx)$",
"transformIgnorePatterns": [],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"automock": false,
"clearMocks": true,
"resetMocks": true,
"coverageThreshold": {
"global": {
"branches": 50,
"functions": 50,
"lines": 50,
"statements": 50
}
},
"modulePaths": [
"<rootDir>/src",
"<rootDir>/node_modules/XXX-framework/src"
],
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react/",
"<rootDir>/node_modules/react-dom/",
"<rootDir>/node_modules/react-addons-test-utils/",
"<rootDir>/node_modules/enzyme/",
"<rootDir>/node_modules/XXX-framework/"
]
}
谢谢
我正在构建一个用于免费 wifi 访问的强制门户。当用户连接时,CNA 会在加载我的门户的屏幕中弹出。
问题: HTML5 视频未在 OS X 桌面上显示。iPhone 也可以显示,桌面则不行。我什至无法检查这个邪恶浏览器中的任何内容(它可以称为浏览器吗?我不这么认为)。
我的视频代码:
<video id="promo-video" height="100%" width="100%" preload="auto" autoplay="" muted="" webkit-playsinline="" playsinline="">
<source src="externalUrlToVideo" type="video/mp4">
</video>
Run Code Online (Sandbox Code Playgroud)
请记住,这段完全相同的代码在 iOS 上显示视频(但不会自动播放......目前这是一个小问题)。
一些屏幕截图*可能会对某些人有所帮助。视频应在右栏中播放:
CNA 无法加载视频
另外,当我右键单击然后单击“在新窗口中打开视频”时,它只显示一个视频播放器,但没有显示任何内容。显然,播放按钮不起作用......
我需要在单元测试中进行模拟process.env.NODE_ENV.
我正在使用webpack 2.0作为构建版本,jest-cli作为构建运行程序,以及mocha和测试
import { ENV } from '../index';
describe('environments configuration', () => {
describe('default environment', () => {
let config;
beforeAll(() => {
delete process.env.NODE_ENV;
process.env.NODE_ENV = ENV.DEFAULT;
config = require('../index');
});
it('should be default login url', () => {
expect(config.url.login).toEqual('http://localhost:8080/login');
});
it('should store token in local storage', () => {
expect(config.STORAGE.TOKEN.type).toEqual('localStorage');
});
});
describe('development environment', () => {
let config;
beforeAll(() => {
delete process.env.NODE_ENV;
process.env.NODE_ENV = ENV.DEVELOPMENT;
config = require('../index');
});
it('should be development login …Run Code Online (Sandbox Code Playgroud) 刚刚开始使用jest进行测试的某些节点应用程序上。express-generator用于脚手架。
在第一次测试中,我得到以下错误:
Jest已检测到以下3个打开的句柄,有可能阻止Jest退出
重现步骤:
git clone git@github.com:gandra/node-jest-err-demo.git
cd node-jest-err-demo
npm install
cp .env.example .env
npm run test
Run Code Online (Sandbox Code Playgroud)
npx envinfo --preset jest 输出:
npx: installed 1 in 1.896s
System:
OS: macOS High Sierra 10.13.4
CPU: x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz Binaries:
Node: 9.3.0 - /usr/local/bin/node
Yarn: 1.5.1 - /usr/local/bin/yarn
npm: 5.7.1 - /usr/local/bin/npm npmPackages:
jest: ^23.1.0 => 23.1.0
Run Code Online (Sandbox Code Playgroud)
知道如何解决吗?
这是github上的相关问题:https : //github.com/facebook/jest/issues/6446
我试图监视由另一个函数调用的函数,这两个函数都驻留在外部文件中并导入。
Funcs.spec.js:
import * as Funcs from './Funcs'
describe('funcA', () => {
it('calls funcB', () => {
jest.spyOn(Funcs, 'funcB')
Funcs.funcA()
expect(Funcs.funcB).toHaveBeenCalled()
}
}
Run Code Online (Sandbox Code Playgroud)
Funcs.js:
export const funcA = () => {
funcB()
}
export const funcB = () => {}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,间谍在 Funcs.js 的范围内不受尊重。我可以做什么来监视 funcB 以便我知道 funcA 调用了它?
我有一个猫鼬模型:
var mongoose = require("mongoose");
var transactionSchema = mongoose.Schema({
category: { type: String, required: [true, "Category is required."] },
amount: Number,
comment: String,
tags: Array,
currency: String
});
var Transaction = mongoose.model("Transaction", transactionSchema);
module.exports = Transaction;
Run Code Online (Sandbox Code Playgroud)
并使用mockgoose和进行简单的单元测试jest:
var { Mockgoose } = require("mockgoose");
var mongoose = require("mongoose");
var Transaction = require("./transaction");
var mockgoose = new Mockgoose(mongoose);
describe("transaction", function() {
afterEach(function() {
mockgoose.helper.reset().then(() => {
done();
});
});
it("category is required", function() {
mockgoose.prepareStorage().then(() => {
mongoose.connect("mongodb://foobar/baz"); …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的Mongoose模型创建一些测试,我无法弄清楚如何让Jest/Mockgoose测试通过我创建的用于检索的速记查询/聚合管道(参见下面的第一个代码块)来自一个集合的随机文档,该文档未在另一个集合中引用.
activitySchema.query.getUnused = function() {
return Activity.aggregate()
.lookup({
from: 'userActivity',
localField: '_id',
foreignField: 'activity',
as: 'matched_docs',
})
.match({ matched_docs: { $eq: [] } })
.sample(1)
.project({ matched_docs: 0, __v: 0 })
.exec()
}
Run Code Online (Sandbox Code Playgroud)
开玩笑测试
describe('Activity methods', () => {
test('Activity unused query executes', (done) => {
function createActivity() {
return Activity
.create({ activity: 'running' })
.then(activity => {
console.log('Activity is generated')
return Promise.resolve(true)
})
}
async function retrieveActivity() {
await createActivity()
Activity
.find()
.getUnused()
.then(unused => {
console.log(unused);
expect(unused).toHaveLength(1)
done() …Run Code Online (Sandbox Code Playgroud) 我得到TypeError: (0 , _expect2.default)(...).toBeDefined is not a function了,通常的解决方法是更改/'node_modules'/不使用引号.这没有任何帮助.输出是
$ jest
FAIL __tests__/test_fake_add_component.js
? Add fake tests › Add › Add requires onAdd prop
TypeError: (0 , _expect2.default)(...).toBeDefined is not a function
at Object.<anonymous> (__tests__/test_fake_add_component.js:20:54)
at process._tickCallback (internal/process/next_tick.js:109:7)
? Add fake tests › Add › Add renders button
TypeError: (0 , _expect2.default)(...).toBeDefined is not a function
at Object.<anonymous> (__tests__/test_fake_add_component.js:25:43)
at process._tickCallback (internal/process/next_tick.js:109:7)
PASS __tests__/test_BasicAnchorTag_enzyme.js
PASS __tests__/test_fake_list_component.js
PASS __tests__/test_hello_world_snapshot.js
Test Suites: 1 failed, 3 passed, 4 total
Tests: 2 failed, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个Jest模拟,但是在模拟本身之外定义模拟函数时似乎有问题。
我有一堂课:
myClass.js
class MyClass {
constructor(name) {
this.name = name;
}
methodOne(val) {
return val + 1;
}
methodTwo() {
return 2;
}
}
export default MyClass;
Run Code Online (Sandbox Code Playgroud)
和一个使用它的文件:
testSubject.js
import MyClass from './myClass';
const classInstance = new MyClass('Fido');
const testSubject = () => classInstance.methodOne(1) + classInstance.name;
export default testSubject;
Run Code Online (Sandbox Code Playgroud)
测试:testSubject.test.js
import testSubject from './testSubject';
const mockFunction = jest.fn(() => 2)
jest.mock('./myClass', () => () => ({
name: 'Name',
methodOne: mockFunction,
methodTwo: jest.fn(),
}))
describe('MyClass tests', () => {
it('test …Run Code Online (Sandbox Code Playgroud) 我有一系列功能,每个功能执行各种Firestore交互。如何使用Jest模拟这些Firestore调用?我想避免使用图书馆。
当我使用jest.mock("firebase/app")和jest.mock("firebase/firestore")其他版本时,我将得到null的TypeErrors或表明我仍在引用实际导入而不是模拟的错误:Error: ... make sure you call initializeApp()。
例如,我要测试的一个简单函数:
import firebase from "firebase/app";
import "firebase/firestore";
export const setDocData = (id, data) => {
const newDoc = {
created: firebase.firestore.FieldValue.serverTimestamp(),
...data
};
firebase
.firestore()
.doc("docs/" + id)
.set(newDoc);
};
Run Code Online (Sandbox Code Playgroud)
请注意,如何照常导入firebase,然后导入firestore的副作用。还要注意,如何首先将Firestore调用为函数,然后再引用为属性。我相信这是我麻烦的根源。
jestjs ×9
javascript ×4
unit-testing ×4
reactjs ×3
mockgoose ×2
mongoose ×2
node.js ×2
class-names ×1
ecmascript-6 ×1
enzyme ×1
es6-modules ×1
firebase ×1
html ×1
macos ×1
mocha.js ×1
mocking ×1
mongodb ×1
tdd ×1
video ×1
webpack ×1