Skip to content

Commit 14368e9

Browse files
committed
Add Jest support
1 parent 46d121c commit 14368e9

File tree

9 files changed

+5626
-497
lines changed

9 files changed

+5626
-497
lines changed

.eslintrc.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ module.exports = {
66
},
77
plugins: [
88
'@typescript-eslint',
9+
'jest',
910
],
1011
env: {
1112
node: true, // for `console`
13+
'jest/globals': true, // describe, test, expect
1214
},
1315
extends: [
1416
'eslint:recommended',

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,27 @@ Add `"type": "module"` to `package.json`, because [TypeScript can't generate fil
7575
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:
7676

7777
npm i -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser
78+
79+
80+
# Jest
81+
82+
To fully support Jest with TypeScript and ESLint, we need to:
83+
84+
* `npm install --save-dev jest @types/jest eslint-plugin-jest`
85+
* add `"jest"` to the `types` array in `tsconfig.json`
86+
* add the `'jest'` plugin to `.eslintrc.cjs` and also add `'jest/globals': true` to its `env` key
87+
* create [`jest.config.cjs`](jest.config.cjs)
88+
89+
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`:
90+
91+
> SyntaxError: Cannot use import statement outside a module
92+
93+
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:
94+
95+
* `npm install --save-dev @babel/plugin-transform-modules-commonjs`
96+
* create [`babel.config.cjs`](babel.config.cjs)
97+
98+
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).
99+
To pass parameters to Node when running Jest, we'll add the following `test` line:
100+
101+
"test": "node --harmony node_modules/.bin/jest"

babel.config.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
plugins: [
3+
// Because Jest still doesn't support native ES modules - https://github.com/facebook/jest/issues/4842
4+
'@babel/plugin-transform-modules-commonjs',
5+
],
6+
};

jest.config.cjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
testMatch: [
4+
'**/?(*.)test.ts',
5+
],
6+
moduleFileExtensions: [
7+
'js', // not used directly, but MANDATORY
8+
'ts', // actually used
9+
],
10+
transform: {
11+
// Transform .ts files, and also .js ones generated by TypeScript, which scripts load -
12+
// https://github.com/microsoft/TypeScript/issues/18442#issuecomment-581738714
13+
'^.+\\.(ts|js)$': 'babel-jest',
14+
},
15+
};

0 commit comments

Comments
 (0)