0

Trying to test an Angular 9 library with Jest, and I've installed the dependencies for Jest and Typescript within the local library package.json like this:

  "devDependencies": {
    "@types/jest": "^25.1.3",
    "jest": "^25.1.0",
    "ts-jest": "^25.2.1"
  },

And also setup jest.config.js like this:

module.exports = {
  "roots": [
    "<rootDir>/src/"
  ],
  testMatch: [
    "**/__tests__/**/*.+(ts|tsx|js)",
    "**/?(*.)+(spec|test).+(ts|tsx|js)"
  ],
  "transform": {
    "^.+\\.(ts|tsx)$": "ts-jest"
  }
}


However it looks as if jest is not running through the ts-jest transform as I'm getting errors like this:

    SyntaxError: Unexpected token export

    > 1 | import assertString from './util/assertString';
        | ^
      2 | import toString from './util/toString';```

1 Answer 1

1

Figured it out. I'm converting ValidatorJS into a Typescript project using the Angular Package Format, and I forget to change the file name extension on the utilities from .js to .ts, hence Jest was complaining about the import statement.

In case anyone else wants to setup their project to do this type of thing I wrote up a few articles here:

https://medium.com/@ole.ersoy/unit-testing-angular-with-jest-b65888ff33f6

https://medium.com/@ole.ersoy/unit-testing-your-angular-library-project-with-jest-42429a8716eb

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.