我正在尝试将功能从上下文导出到另一个文件以供玩笑

时间:2019-09-23 10:48:32

标签: reactjs unit-testing jestjs

我想开玩笑地在测试文件中运行某些功能。我是新来的,所以请客气。我正在从应用程序的上下文文件中导入此功能。

let isProduction2 = () => {
    if (production) {
        return true
    } else {
        return false
    }
}

export {
    ProductProvider,
    ProductConsumer,
    ProductContext,
    isProduction2
};

import {
    isProduction2
} from './context'

test('Fake Test', () => {
    expect(isProduction2).toBeTruthy();
});
//Error   Jest encountered an unexpected token

这通常意味着您正在尝试导入Jest无法解析的文件,例如这不是普通的JavaScript。

默认情况下,如果Jest看到Babel配置,它将使用该配置来转换文件,而忽略“ node_modules”。

1 个答案:

答案 0 :(得分:0)

问题是您没有在Expect语句中执行函数(即isProduction2)。您目前正在传递参考。

代替

test('Fake Test', () => {
    expect(isProduction2).toBeTruthy();
});

应该是

test('Fake Test', () => {
    expect(isProduction2()).toBeTruthy();
    //note the `()` after function name. This executes it and returns the result.
});