1 | # Troubleshooting
|
2 |
|
3 | ## Running ts-jest on CI tools
|
4 |
|
5 | ### PROBLEM
|
6 |
|
7 | Cannot find module "" from ""
|
8 |
|
9 | ### SOLUTION
|
10 |
|
11 | - Check if `rootDir` is referenced correctly. If not add this on your existing jest configuration.
|
12 |
|
13 | ```javascript
|
14 | module.exports = {
|
15 | ...
|
16 | roots: ["<rootDir>"]
|
17 | }
|
18 | ```
|
19 |
|
20 | - Check if module directories are included on your jest configuration. If not add this on your existing jest configuration.
|
21 |
|
22 | ```javascript
|
23 | module.exports = {
|
24 | ...
|
25 | moduleDirectories: ["node_modules","<module-directory>"],
|
26 | modulePaths: ["<path-of-module>"],
|
27 | }
|
28 | ```
|
29 |
|
30 | - Check if module name is properly mapped and can be referenced by jest. If not, you can define moduleNameMapper for your jest configuration.
|
31 |
|
32 | ```javascript
|
33 | module.exports = {
|
34 | ...
|
35 | moduleNameMapper: {
|
36 | "<import-path>": "<rootDir>/<real-physical-path>",
|
37 | },
|
38 | }
|
39 | ```
|
40 |
|
41 | - Check github folder names if its identical to you local folder names. Sometimes github never updates your folder names even if you rename it locally. If this happens rename your folders via github or use this command `git mv <source> <destination>` and commit changes.
|
42 |
|
43 | ## Transform (node)-module explicitly
|
44 |
|
45 | ### PROBLEM
|
46 |
|
47 | SyntaxError: Cannot use import statement outside a module
|
48 |
|
49 | ### SOLUTION
|
50 |
|
51 | One of the node modules hasn't the correct syntax for Jests execution step. It needs to
|
52 | be transformed first.
|
53 |
|
54 | There is a good chance that the error message shows which module is affected:
|
55 |
|
56 | ```shell
|
57 | SyntaxError: Cannot use import statement outside a module
|
58 | > 22 | import Component from "../../node_modules/some-module/lib";
|
59 | | ^
|
60 | ```
|
61 |
|
62 | In this case **some-module** is the problem and needs to be transformed.
|
63 | By adding the following line to the configuration file it will tell Jest which modules
|
64 | shouldnt be ignored during the transformation step:
|
65 |
|
66 | ```javascript
|
67 | module.exports = {
|
68 | ...
|
69 | transformIgnorePatterns: ["node_modules/(?!(some-module|another-module))"]
|
70 | };
|
71 | ```
|
72 |
|
73 | **some-module** and **another-module** will be transformed.
|
74 |
|
75 | For more information see [here](https://stackoverflow.com/questions/63389757/jest-unit-test-syntaxerror-cannot-use-import-statement-outside-a-module) and [here](https://stackoverflow.com/questions/52035066/how-to-write-jest-transformignorepatterns).
|