这是我的测试文件
// /imports/components/main.test.js
import React from 'react'
import { shallow, mount } from 'enzyme'
import Main from './main'
import TextInput from "/imports/ui/textInput"
...
Run Code Online (Sandbox Code Playgroud)
而 main.js 有
// /imports/components/main.js
import { action1 } from "/imports/actions/myAction"
Run Code Online (Sandbox Code Playgroud)
但是当我运行测试时它会抛出一个错误,说
Cannot find module '/imports/actions/myAction' from 'main.js'
如果我评论import './main',导入 TextInput 也会发生同样的事情。我在 node_modules 中导入模块没有问题。
如何告诉 Jest 或 webpack 使用项目目录(即import Foo from /imports/...)中的绝对路径导入组件?
我正在测试我的 graphql 端点,该端点通过 Jest 的 sequelize 对 DB 进行查询。但有时我发现我的测试文件有点竞争条件。我的意思是,对于每个测试文件,我都有一个设置 DB 块,例如
beforeAll(async () => {
await database.sequelize.sync({force: true})
await database.User.create(user)
})
Run Code Online (Sandbox Code Playgroud)
以便在测试文件中我可以轻松预测数据库中的内容。有时这会导致每个测试文件尝试执行.sync(). 一个测试文件将创建数据库,而另一个执行删除数据库。
即使我已经使用await了我的测试,这看起来并不能保证测试文件将等待另一个测试完成。
这里最好的方法是确保每个测试文件都可以通过拥有干净的数据库来预测数据库中的内容,同时又不与其他测试发生冲突?让测试等待其他人完成实际上是一个好主意吗?对我来说似乎没有优化,因为运行整个测试套件需要更多时间。
我有一个由kaminari生成的url案例,paginate因为url取决于条件.这是我的路线
concern :search do
scope '/search', as: :search do
get '/', to: 'users#search'
get '/schedule/:id', to: 'schedules#doctor', as: :schedule
end
end
concerns :search
scope :dashboard, as: :dashboard do
concerns :search
end
Run Code Online (Sandbox Code Playgroud)
如您所见,:search可通过/search和访问/dashboard/search.问题是,paginate @doctors给/search它基本上都会去search_path,即使我在dashboard_search_path(它应该给出/dashboard/search路径).
我的问题是,我如何通过自定义路径paginate?我想paginate用search_path,当我打开/search和使用dashboard_search_path,当我在/dashboard/search/path.
您不必提供如何决定,/search或者/dashboard/search我只需要知道如何将其paginate作为参数传递.钽
假设条件是用户有很多项目
var User = sequelize.define('user', {
id: { type: Sequelize.INTEGER, primaryKey: true},
name: Sequelize.STRING,
}, {
underscored: true,
constraints: false
})
var Project = sequelize.define('project', {
id: { type: Sequelize.INTEGER, primaryKey: true},
title: Sequelize.STRING,
}, {
underscored: true,
constraints: false
})
Project.belongsTo(User)
User.hasMany(Project)
Run Code Online (Sandbox Code Playgroud)
如果我已经在数据库中有用户记录
{ id: 1, name: "spondbob" }
Run Code Online (Sandbox Code Playgroud)
如何与该用户创建项目?我在某个地方看到我们可以在创建项目时执行此操作
{
id: 1,
title: "Project 1",
user_id: 1
}
Run Code Online (Sandbox Code Playgroud)
但是,除了直接提及外键之外,还有其他优雅的方式来设置用户吗?
我也知道我可以创建项目并执行,project.setUser(user)但问题是user_id外键设置为 NOT NULL。在这种情况下,我必须user_id在创建项目时添加,而不是在创建后设置它。
另外,如果我需要创建多个项目并将其分配给一个用户,我该如何实现呢?
如何在生产中禁用graphiql但仍能在生产中访问它?
使用express-graphql,我们可以做类似的事情
app.use('/graphql', graphqlHTTP({
schema: MySessionAwareGraphQLSchema,
graphiql: process.env.NODE_ENV === 'development',
}));
Run Code Online (Sandbox Code Playgroud)
使用apollo服务器,我的设置是
import {graphqlExpress, graphiqlExpress} from 'graphql-server-express'
const app = new Express()
app
.all('/graphql', bodyParser.json())
.all('/graphql', graphqlExpress({
schema
)
.all('/graphiql', graphiqlExpress({
endpointURL: 'http://localhost/graphql'
})
)
Run Code Online (Sandbox Code Playgroud)
我找不到传递给NODE_ENV来启用/禁用graphiql的方法.
如果我有一个user和post集合
{"_id": 1, "name": "User 1"}
{"_id": 2, "name": "User 2"}
{"_id": 1, "title": "Post 1", "userId": 1, "createdAt": ISODate("2017-07-24T04:12:54.255Z")}
{"_id": 2, "title": "Post 2", "userId": 1, "createdAt": ISODate("2017-07-25T04:12:54.255Z")}
{"_id": 3, "title": "Post 1", "userId": 2, "createdAt": ISODate("2017-07-24T04:12:54.255Z")}
Run Code Online (Sandbox Code Playgroud)
如何列出所有用户的最新帖子?会是这样的
{
"_id": 1,
"name": "User 1",
"post": {
"_id": 2,
"title": "Post 2",
"userId": 1,
"createdAt": ISODate("2017-07-25T04:12:54.255Z")
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以很容易地使用$ lookup,$ unwind post,然后$ sort by post.createdAt,但是这给我留下了冗余用户(用户1将在Post 1和Post 2中列出两次).
我不知道如何使用$ group删除重复项,同时保持其他字段(名称,post.title等)
基本上我想抓住<Route path="/:slug" />但除了/not-found. 我尝试使用看起来像/:slug(^(?!.*not-found).*$)但它不起作用的路径到正则表达式的自定义匹配参数。它在这里不起作用http://forbeslindesay.github.io/express-route-tester/,但它在https://regex101.com/中起作用
我想知道当我试图捕捉时如何排除路径/:slug?
创建记录和用户更新密码时,我试图为用户哈希密码。在创作时,我可以做类似的事情
User.beforeCreate((user, options) => {
user.password = encryptPassword(user.password)
})
Run Code Online (Sandbox Code Playgroud)
这将很容易执行,并为新用户哈希密码。但是更新密码时出现问题。如果我只是做
User.beforeUpdate((user, options) => {
user.password = encryptPassword(user.password)
})
Run Code Online (Sandbox Code Playgroud)
然后,每次用户更新其记录(即更新名称,地址等)时,都会触发该钩子并重新哈希该密码。
如何知道何时更改密码,以便触发挂钩?另外,除了拥有这两个钩子之外,我如何才能使用它beforeSave来获得相同的结果?
更新
根据要求,我的用户定义非常简单
sequelize.define(
'user',
{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
},
emailAddress: {
field: 'email_address',
type: Sequelize.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: {
args: true,
msg: "Email is not valid"
}
},
},
password: {
type: Sequelize.STRING,
allowNull: false,
validate: {
min: {
args: 6,
msg: "Password must be more than 6 characters" …Run Code Online (Sandbox Code Playgroud) sequelize.js ×3
jestjs ×2
enzyme ×1
kaminari ×1
mongodb ×1
react-router ×1
reactjs ×1
unit-testing ×1
webpack ×1