gpt4 book ai didi

javascript - Jest 返回一个表未找到错误与 sqlite

转载 作者:行者123 更新时间:2023-12-03 22:18:29 25 4
gpt4 key购买 nike

我已经编写了一组测试,并使用 Sequelize 作为我的 ORM 和 Jest 作为我的测试运行器。

我的 Sequelize 配置如下所示:

{
"development": {
"dialect": "sqlite",
"storage": "dev.sqlite"
},
"test": {
"dialect": "sqlite",
"storage": ":memory:"
},
}

我的 package.json 如下所示:
    "test": "jest --setupFiles dotenv/config",
"pretest": "yarn migrate && yarn seed",
"migrate": "cd server && npx sequelize-cli db:migrate",
"migrate:reset": "cd server && npx sequelize-cli db:migrate:undo:all --env test && npm run migrate --env test",
"seed": "cd server && npx sequelize-cli db:seed:all --env test",
"test:debug": "node --inspect node_modules/.bin/jest",

我的笑话设置文件只是确保 Node 环境是 test

一切都按预期工作,迁移运行没有问题,种子文件也是如此。但是当测试开始时,我得到以下信息:
SequelizeDatabaseError: SQLITE_ERROR: no such table: Users 。这对于使用的每个表都是相同的。

我怀疑问题在于当连接关闭时,内存数据库终止,所以我尝试在每个测试的基础上运行数据库迁移和种子,但这没有用。同样用于使用实际的 test.sqlite 文件。

有什么建议吗?

我的注册测试示例:
import { getMaxTokenAge } from '../../../utils/auth'
import sinon from 'sinon'
import mutation from '../register'
import models from '../../../models/'

import query from '../getUser'

describe('register', () => {
describe('resolver', () => {
const res = {
cookie: jest.fn(),
}
const userInfo = {
email: 'test@example.com',
firstName: 'First',
lastName: 'Last',
phone: '0123456789',
orgName: 'Org Name',
organization: 'none',
password: 'secret123',
newOrganization: {
name: 'Org Name',
type: 'buyer',
},
}

test('guests can register', async () => {
// Get the role and type of a buyer
const buyerRole = await models.UserRole.findOne({
where: {
name: 'buyer',
},
raw: true,
})

const someBuyerType = await models.UserType.findOne({
where: {
name: 'somebuyer',
},
raw: true,
})

// Take the ids for role and type and add them to our user info to submit
userInfo.role = someRole.id
userInfo.type = someType.id

await mutation({}, userInfo, {
context: {},
models,
req: {},
res,
})

expect(
await query({}, { email: userInfo.email }, `{ email, id }`),
).toMatchSnapshot({
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
id: expect.any(String),
password: expect.any(String),
UserRoleId: expect.any(String),
UserTypeId: expect.any(String),
})

expect(res.cookie).toHaveBeenCalledWith('token', expect.any(String), {
maxAge: getMaxTokenAge(),
httpOnly: true,
})
})

test('throws an error when trying to register with an email that belongs to an existing user', async () => {
models.User.findOne = sinon.stub().resolves(true)

await expect(
mutation({}, userInfo, {
models,
req: {},
res,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"A user already exists with this email address."`,
)
})
})
})

和注册码:
/**
* @param {*} parent
* @param {string} email
* @param {string} password
* @param {*} args
* @param {*} context
*
* @example
* ```
* register(
* email: "${user.email}"
* password: "secret123"
* organization: "none"
* firstName: "${user.firstName}"
* lastName: "${user.lastName}"
* phone: "0123456789"
* newOrganization: {
* name: "${user.orgName}"
* type: "buyer"
* }
* ) {
* token
* user {
* email
* }
* }
* ```
*/
const register = async (
root,
{
email,
password,
firstName,
lastName,
phone,
organization,
newOrganization,
role,
type,
},
{ models, res, req },
) => {
const encryptedPassword = await bcrypt.hash(password, 10)

const exists = await models.User.findOne({ where: { email } })

if (exists) {
throw new DatabaseError('A user already exists with this email address.')
}

const user = await models.User.create({
email,
password: encryptedPassword,
firstName,
lastName,
phone,
})

await user.setUserRole(role)

await user.setUserType(type)

const token = jwt.sign(
{
userId: user.id,
},
process.env.JWT_SECRET,
)


res.cookie('token', token, {
maxAge: getMaxTokenAge(),
httpOnly: true,
})

req.user = user

return user
}

export default register

最佳答案

我在测试我的数据库表和模型时遇到了类似的问题。我通过添加以下代码解决了它:

    await User.sync({ force: true });

关于javascript - Jest 返回一个表未找到错误与 sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59787446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com