无法在 Jest 中使用 Sequelize 查询结果

问题描述

我正在使用 JEST 和 Sequelize 编写集成测试,但后来我注意到 console.log 值仅在测试完成后才出现。但是,我希望能够使用查询语句中的值并将其传递给端点以完成一个过程,但它总是失败。

下面是测试文件

require('dotenv').config()
process.env.NODE_ENV = 'local'
const db = require('../src/app/models')
const MerchantTemp = db.rest.models.MerchantTemp
const supertest = require('supertest')
const app = require('../app_test')
const bcrypt = require('bcrypt')
const faker = require('faker')
const { QueryTypes } = require('sequelize')


describe("Account Authorization",() => {

    let thisDb = db
    let email = 'xya@gmail.com'

    // Before any tests run,clear the DB and run migrations with Sequelize sync()
    beforeAll(async () => {
        await thisDb.rest.sync({ force: true })
    })

    // After all tests have finished,close the DB connection
    afterall(async () => {
        await thisDb.rest.close()
    })


    test("Just to test the Index Page",async () => {
        const response = await supertest(app).get('/v1')
        expect(response.body.statusCode).toBe(200)
    })

    test("Create New Merchant",async () => {
        const password = await bcrypt.hash('W3lc0m3123',10)
        const data = {
            "firstname": faker.random.alpha(10),"lastname": faker.random.alpha(10),"phone": '08035441234',email,"password": password
        }

        // App is used with supertest to simulate server request
        const response = await supertest(app)
            .post("/v1/account/create-merchant")
            .send(data)

        expect(response.body).toMatchObject({
            statusCode: 201,message: 'Account created successfully',})

    })

    //this is expected to fail
    test("Failed to Create New Merchant",10)
        //To test validation if it works
        const data = {
            "firstname": "","lastname": "","phone": faker.phone.phoneNumber,"email": faker.internet.email,"password": password
        }
        const response = await supertest(app)
            .post("/v1/account/create-merchant")
            .send(data)

        expect(response.body).toMatchObject({
            statusCode: 400,})
    })

    test("Confirm New Account Verification",async () => {

        const getCode = await thisDb.rest.query("SELECT * FROM MerchantTemp WHERE email= 'xya@gmail.com'",{ type: QueryTypes.SELECT })
        
        //I want to use the verification code and send to the verify endpoint below but it doesn't work for some unkNown reason
        console.log(getCode[0].verification_code)
        expect(getCode.length).toBeGreaterThan(0)

        const response = await supertest(app)
            .get("/v1/account/verify/" + getCode[0].verification_code)
        expect(response.body).toMatchObject({
            statusCode: 200,})
    })

})

这是我用来运行测试的命令

yarn jest _test_/account.test.js --silent=false --verbose

附上测试结果截图

enter image description here

非常感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)