【问题标题】:Testing Keystone and Mongoose with Jest用 Jest 测试 Keystone 和 Mongoose
【发布时间】:2019-08-07 23:04:22
【问题描述】:

我正在使用Keystone 在一个项目中实施单元测试,但它总是在测试时抛出错误:

an all test suites.
/Project/node_modules/mongodb/lib/mongo_client.js:433
      throw err
      ^

TypeError: require(...) is not a function
at /Project/node_modules/keystone/lib/core/start.js:64:44
at /Project/node_modules/async/dist/async.js:3880:24
at eachOfArrayLike (/Project/node_modules/async/dist/async.js:1069:9)
at eachOf (/Project/node_modules/async/dist/async.js:1117:5)
at _parallel (/Project/node_modules/async/dist/async.js:3879:5)
at Object.parallelLimit [as parallel] (/Project/node_modules/async/dist/async.js:3962:5)
at /Project/node_modules/keystone/lib/core/start.js:60:9
at connected (/Project/node_modules/keystone/lib/core/openDatabaseConnection.js:78:5)
at NativeConnection.<anonymous> (/Project/node_modules/keystone/lib/core/openDatabaseConnection.js:85:4)
at Object.onceWrapper (events.js:273:13)
at NativeConnection.emit (events.js:182:13)
at /Project/node_modules/mongoose/lib/connection.js:860:13
at connectCallback (/Project/node_modules/mongodb/lib/mongo_client.js:527:5)
at /Project/node_modules/mongodb/lib/mongo_client.js:430:11
at process._tickCallback (internal/process/next_tick.js:61:11)
npm ERR! Test failed.  See above for more details.

我已经尝试过使用“import { init, start, mongoose } from 'keystone';”相反,使用 jest async() 和等待。我也跑“jest --clearCache”。笑话版本:“笑话”:“^24.3.1”

我的.babelrc:

{
"presets": [
    ["@babel/preset-env",
        {"targets": {
            "node": "current"
        }}
    ], 
    "@babel/preset-react"
]    
}

test.js:

const Keystone = require('keystone');

beforeAll( done => {
    Keystone.init({
        'name': 'Keystone CMS',
        'cookie secret': 'My_Biscuit',
    });
    Keystone.start();
    done();
})

describe('Test Keystone CMS own features', () => {
    test('Keystone connects to MongoDB', (done) => {
        expect(Keystone.mongoose.connections._listening).toBeTruthy;
        done();
    });
});

然后我会扩展它来测试猫鼬

【问题讨论】:

    标签: node.js mongoose jestjs keystonejs


    【解决方案1】:

    目前看来,这是不可能的: https://github.com/keystonejs/keystone/issues/3564

    【讨论】:

      猜你喜欢
      • 2018-08-07
      • 2019-04-26
      • 2018-12-23
      • 2021-07-16
      • 2019-10-03
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 2022-01-06
      相关资源
      最近更新 更多