"Missing semicolon" error raised when mocking a class in Jest using Typescript
Asked Answered
S

2

15

I'm using Typescript and trying to test my code with Jest, and I made a type casting on a class so I can mock it.

Unfortunately, when I ran the test suite I got the following error:

 SyntaxError: C:\Projects\sim_editor\server\src\tests\routes.test.ts: Missing semicolon (7:37)

Here is my code:

describe('Unit test api routes', () => {
    jest.mock('../controllers')
    const mkControllers = Controllers as jest.MockedClass<typeof Controllers>

    beforeEach(() => {
        mkControllers.mockClear()
    })

    .
    .
    .
    The rest of my test suite
 })

The error was referring the line where I declared "mkControllers".

Here is a deeper log of the error:

at Parser._raise (node_modules/@babel/parser/src/parser/error.js:97:45)
  at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:92:17)
  at Parser.raise (node_modules/@babel/parser/src/parser/error.js:41:17)
  at Parser.semicolon (node_modules/@babel/parser/src/parser/util.js:131:10)
  at Parser.parseVarStatement (node_modules/@babel/parser/src/parser/statement.js:707:10)
  at Parser.parseStatementContent (node_modules/@babel/parser/src/parser/statement.js:223:21)
  at Parser.parseStatement (node_modules/@babel/parser/src/parser/statement.js:163:17)
  at Parser.parseBlockOrModuleBlockBody (node_modules/@babel/parser/src/parser/statement.js:880:25)
  at Parser.parseBlockBody (node_modules/@babel/parser/src/parser/statement.js:856:10)
  at Parser.parseBlock (node_modules/@babel/parser/src/parser/statement.js:826:10)

Thanks.

Shumate answered 2/3, 2021 at 0:21 Comment(5)
You don't have any semicolons at all. All statements in Typescript must end with a semicolon.Huskey
Have a look here. Notice that, for the most part, each line ends with a semicolon?Huskey
Semicolons aren't mandatory in Typescript. I also tried to add them to the test suite, still the same error.Shumate
@DvirBartov Have you tried adding a semicolon at the end? It's true that semicolons can usually be skipped, but sometimes there are ASI rules that require one. They're rare, but they do happen.Mastoidectomy
Yes, I tried adding semicolons, including at the end of the test suite.Shumate
S
17

Apparently I didn't configure Babel to work with Typescript, as it is essential for Jest to use it. Make sure you follow the instructions here, under "Using Babel" and "Using Typescript"

Shumate answered 2/3, 2021 at 9:2 Comment(0)
G
2

Jest supports TypeScript, via Babel. Follow these steps to configure.


Step 1: Add the dependencies by pasting this command in your console.

npm install --save-dev babel-jest @babel/core @babel/preset-env @babel/preset-typescript

Step 2: Create a new file in the root of your project named babel.config.js.

Step 3: Paste this text in the babel.config.js file.

module.exports = {
  presets: [
    ['@babel/preset-env', {targets: {node: 'current'}}],
    '@babel/preset-typescript',
  ],
};
Ganymede answered 26/9, 2023 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.