Ole
Ole

Reputation: 46940

Using jest to test an Angular Library?

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';```

Upvotes: 0

Views: 1410

Answers (1)

Ole
Ole

Reputation: 46940

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

Upvotes: 1

Related Questions