Jest文件中如何正确要求? (带有模块模式js文件)

问题描述 投票:0回答:1

可能是一个愚蠢的问题

我开始使用JEST进行测试。

我有我的js应用程序:

var app={ init:function{ //some code}, ... }
module.exports = app;

还有我的app.test.js:

const {app} = require('../js/index.js')

test('type of variable', () => {
  expect(typeof app.someFunction(app.someVar)).toBe("'number");
});

而且我有经典错误:

TypeError: Cannot read property 'someFunction' of undefined

看起来很愚蠢,但是我从客户角度看不清楚这些要求...它与Jest getStarted示例完美配合enter image description here

我的arbo是

-js
----index.js
-tests
----app.text.js
jestjs require
1个回答
1
投票
module.exports = app

以上行返回object {},而您正在尝试从破坏行app中的对象中拾取var {app}

删除{}

const app = require('../js/index.js')

test('type of variable', () => {
  expect(typeof app.someFunction(app.someVar)).toBe("'number");
});
© www.soinside.com 2019 - 2024. All rights reserved.