用Jest测试Keystone和猫鼬

时间:2019-03-17 12:00:25

标签: node.js mongoose jestjs keystone

我正在使用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.

我已经尝试使用“从'keystone'导入{初始化,开始,猫鼬};”而是使用笑话async()和await。我也跑得“开玩笑--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();
    });
});

然后我将其扩展以测试猫鼬

1 个答案:

答案 0 :(得分:0)

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