Javascript 在测试文件时遇到错误
我刚刚参加了一个关于Javascript和Jest的速成班,所以我对它还很陌生。我认为我对使用jest的测试文件的格式有一些问题 这是fizzbuzz的问题,我知道这很容易,但我的问题是关于笑话测试。它表明:Javascript 在测试文件时遇到错误,javascript,unit-testing,testing,jestjs,Javascript,Unit Testing,Testing,Jestjs,我刚刚参加了一个关于Javascript和Jest的速成班,所以我对它还很陌生。我认为我对使用jest的测试文件的格式有一些问题 这是fizzbuzz的问题,我知道这很容易,但我的问题是关于笑话测试。它表明: Jest encountered an unexpected token. This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaSc
Jest encountered an unexpected token. This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
我认为进出口的使用是正确的,对吗
[fizzbuzz.test.js]
import { fizzbuzz } from "./fizzbuzz";
describe("fizzbuzz", () => {
it("should return the numbers passed in that are not divisible by 3 or 5", () => {
expect(fizzbuzz(1)).toBe(1);
expect(fizzbuzz(2)).toBe(2);
expect(fizzbuzz(4)).toBe(4);
});
it("should return 'fizz' if the number passed in is divisible by 3", () => {
expect(fizzbuzz(3)).toBe("fizz");
expect(fizzbuzz(6)).toBe("fizz");
});
it("should return 'buzz' if the number passed in is divisible by 5", () => {
expect(fizzbuzz(5)).toBe("buzz");
expect(fizzbuzz(10)).toBe("buzz");
});
it("should return 'fizzbuzz' if the number passed in is divisible by 3 and 5", () => {
expect(fizzbuzz(15)).toBe("fizzbuzz");
});
});
作为参考,这是我的[fizzbuzz.js]
export function fizzbuzz() {
for (let i = 1; i <= 100; i++){
if(i%3 === 0 && i % 5 === 0){
console.log("Fizzbuzz");
} else if (i % 3 === 0){
console.log("Fizz");
} else if (i % 5 === 0){
console.log("Buzz");
} else {
console.log(i);
}
}
}
[cyber dojo.sh]
ln -s /etc/jest/node_modules ${CYBER_DOJO_SANDBOX}/node_modules
npm run lint
npm run test
[package.json]
{
"scripts": {
"lint": "eslint --config ${CYBER_DOJO_SANDBOX}/.eslintrc.json /**/*.js",
"test": "jest --coverage"
},
"jest": {
"coverageReporters": [ "text" ]
}
}
如进化箱所述;第2行有一个输入错误:
for (let i = 1, i <= 100; i++){
用于(假设i=1,我认为你似乎有一个输入错误。i=1,我的错误是否说明了意外标记是什么?SyntaxError:无法在模块外使用import语句。因此我认为我不能使用import,我应该如何修改它这意味着你需要编译javascript。你使用的是babel jest
?@evolutionxbox否。我认为我需要使用在那个开源网站上的ese配置。我正在尝试使用模块导出。感谢您指出。但是仍然存在相同的错误消息。请标记输入错误,而不是回答?
for (let i = 1, i <= 100; i++){
for (let i = 1; i <= 100; i++){