Skip to content

Commit

Permalink
Add Jest support
Browse files Browse the repository at this point in the history
  • Loading branch information
dandv committed Mar 27, 2020
1 parent 46d121c commit 14368e9
Show file tree
Hide file tree
Showing 9 changed files with 5,626 additions and 497 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ module.exports = {
},
plugins: [
'@typescript-eslint',
'jest',
],
env: {
node: true, // for `console`
'jest/globals': true, // describe, test, expect
},
extends: [
'eslint:recommended',
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,27 @@ Add `"type": "module"` to `package.json`, because [TypeScript can't generate fil
To be able to run `eslint`, we must create an `.eslintrc.cjs` file, rather than a `.js` one (due to `"type": "module"` in `package.json`). Then, install the required dependencies:

npm i -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser


# Jest

To fully support Jest with TypeScript and ESLint, we need to:

* `npm install --save-dev jest @types/jest eslint-plugin-jest`
* add `"jest"` to the `types` array in `tsconfig.json`
* add the `'jest'` plugin to `.eslintrc.cjs` and also add `'jest/globals': true` to its `env` key
* create [`jest.config.cjs`](jest.config.cjs)

Now if Jest supported ES Modules, we'd be done, but [it doesn't](https://github.com/facebook/jest/issues/4842), so we'll get this error when running `npm test`:

> SyntaxError: Cannot use import statement outside a module
To work around that, we need to add a `transform` key to `jest.config.cjs` and use Babel to transform the `import` statements from the `.js` files that TypeScript generates:

* `npm install --save-dev @babel/plugin-transform-modules-commonjs`
* create [`babel.config.cjs`](babel.config.cjs)

Normally, to run Jest from `package.json`, we'd add a `"test": "jest"` line. That won't be sufficient, because we need to pass the `--harmony` flag to node (for optional chaining support).
To pass parameters to Node when running Jest, we'll add the following `test` line:

"test": "node --harmony node_modules/.bin/jest"
6 changes: 6 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: [
// Because Jest still doesn't support native ES modules - https://github.com/facebook/jest/issues/4842
'@babel/plugin-transform-modules-commonjs',
],
};
15 changes: 15 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
testEnvironment: 'node',
testMatch: [
'**/?(*.)test.ts',
],
moduleFileExtensions: [
'js', // not used directly, but MANDATORY
'ts', // actually used
],
transform: {
// Transform .ts files, and also .js ones generated by TypeScript, which scripts load -
// https://github.com/microsoft/TypeScript/issues/18442#issuecomment-581738714
'^.+\\.(ts|js)$': 'babel-jest',
},
};
Loading

0 comments on commit 14368e9

Please sign in to comment.