[英]How to Start Unit Testing in React Native using jest for constant
I m new to Unit Test and currently writing Unit Test for my TypeScript app using Jest.我是单元测试的新手,目前正在使用 Jest 为我的 TypeScript 应用程序编写单元测试。 I m using a custom package named
common-packages
where i store all of my constant, something simple like this我正在使用一个名为
common-packages
的自定义 package 我存储所有常量,像这样简单
const CLIENT_ID = 1231123123;
const TEXT = {
Discover:"Discover",
loading: 'loading',
Type: "Testing",
}
const Constants = {
TEXT,
CLIENT_ID,
} as const;
export default Constants;
on my package, index.tsx.在我的 package 上,index.tsx。 i export the
Constant
我导出
Constant
export { default as Constants } from "./constants";
And the function that i want to test is this而我想测试的function就是这个
import {Constants} from 'common-packages';
export const lockNextCardIfUncompleted = (data: any[]) => {
if (data.length !== 0) {
const locked = data.filter((e: any) => e.Type === Constants.TEXT.Type );
if (locked.length === 0) return false;
return true;
}
return false;
};
i have tried writing a unit test for lodash like this and it is working我已经尝试像这样为 lodash 编写单元测试并且它正在工作
const lodash = require('lodash')
test("repeat", () => {
expect(lodash.repeat("A",3)).toBe("AAA");
})
But for the unit test that have common-packages, it failed但是对于具有 common-packages 的单元测试,它失败了
import {
lockNextCardIfUncompleted,
} from './fileName';
test('should return locked', () => {
expect(
lockNextCardIfUncompleted([
{
Type: 'A',
},
]),
).toBe(false);
expect(
lockNextCardIfUncompleted([
{
Type: 'Testing,
},
]),
).toBe(true);
});
It return TypeError: Cannot read property 'TEXT' of undefined
.它返回
TypeError: Cannot read property 'TEXT' of undefined
。 So the test didnt recognize the Constant from common-package
.所以测试没有从
common-package
识别常量。 can anybody please help me solve this issue?有人可以帮我解决这个问题吗? thank you
谢谢你
You are using Default Exports , NOT Named Exports .您正在使用Default Exports ,而不是Named Exports 。
So you need to import the constants like this:所以你需要像这样导入常量:
import Constants from 'common-packages';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.