Skip to content

Commit

Permalink
docs(devs-infra): troubleshoot import statement (#2812)
Browse files Browse the repository at this point in the history
  • Loading branch information
greenbourne277 authored Aug 10, 2021
1 parent bacdf55 commit 12aaa91
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Cannot find module "" from ""

- Check if `rootDir` is referenced correctly. If not add this on your existing jest configuration.

```javascipt
```javascript
module.exports = {
...
roots: ["<rootDir>"]
Expand All @@ -19,7 +19,7 @@ module.exports = {

- Check if module directories are included on your jest configuration. If not add this on your existing jest configuration.

```javascipt
```javascript
module.exports = {
...
moduleDirectories: ["node_modules","<module-directory>"],
Expand All @@ -29,7 +29,7 @@ module.exports = {

- Check if module name is properly mapped and can be referenced by jest. If not, you can define moduleNameMapper for your jest configuration.

```javascipt
```javascript
module.exports = {
...
moduleNameMapper: {
Expand All @@ -39,3 +39,37 @@ module.exports = {
```

- 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.

## Transform (node)-module explicitly

### PROBLEM

SyntaxError: Cannot use import statement outside a module

### SOLUTION

One of the node modules hasn't the correct syntax for Jests execution step. It needs to
be transformed first.

There is a good chance that the error message shows which module is affected:

```shell
SyntaxError: Cannot use import statement outside a module
> 22 | import Component from "../../node_modules/some-module/lib";
| ^
```

In this case **some-module** is the problem and needs to be transformed.
By adding the following line to the configuration file it will tell Jest which modules
shouldnt be ignored during the transformation step:

```javascript
module.exports = {
...
transformIgnorePatterns: ["node_modules/(?!(some-module|another-module))"]
};
```

**some-module** and **another-module** will be transformed.

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).

0 comments on commit 12aaa91

Please sign in to comment.