[英]JavaScript Jest test Cannot use import statement outside a module
[英]Cannot use import statement outside a module in Jest Test module
我是 Jest 的新手。 我嘗試為基本 function 編寫測試,這是
export const queryValidate = (query) => {
const str = query.replace(/\s+/g, "");
const conditionsArray = [
str === "",
str === "{",
str === "}",
str === "{}",
];
if (conditionsArray.includes(true)) {
return true;
} else {
return false;
}
};
在我的 Jest 測試文件中
import { queryValidate } from "./components/QueryValidate";
console.log(queryValidate("{"));
我收到此錯誤消息:
從“./components/QueryValidate”導入 { queryValidate };
SyntaxError: Cannot use import statement outside a module
我無法理解這是關於 Jest 錯誤或 React 模塊錯誤。 我嘗試編寫虛擬測試,例如: test("Fake test", () => { expect(true).toBeTruthy(); });
這是工作。 有人能幫我嗎?
我沒有足夠的聲譽來添加評論,因此我正在添加一個答案。 不久前我也遇到了同樣的問題。 正如 Estus 在評論中提到的,您需要更改 Jest 配置。 您還可以查看babel-jest ,然后設置一個 babel 配置文件/.babelrc,以及預設的 env。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.