Encounter errors in jest for testing files

Viewed 103

I just had a very crash course on Javascript and Jest, so I'm pretty new to it. I thought I had some issue on the format of test file using jest.

It is the fizzbuzz problem, I know it is easy, but my problem is about the jest testing. It shows:

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".

I think the use of import and export are correct, right?

[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");
  });
});

for reference, this is my [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);
   } 
  }
}

I solved it on an open-source website, so it has some configurations. [.eslintrc.json]

{
    "parserOptions": {
        "ecmaVersion": 8,
        "sourceType": "module"
    },
    "rules": {
        "semi": "error"
    }
}

[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" ]
  }
}
1 Answers

Like evolutionbox stated; you have a typo at line 2:

  for (let i = 1, i <= 100; i++){

Should be

  for (let i = 1; i <= 100; i++){

Notice the change from , to ;

Related