Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

‼️ NOTICE: CDK tests breaks with Jest v25.5.0 [Error: "TypeError: Cannot read property 'map' of undefined'] #7657

Closed
NetaNir opened this issue Apr 29, 2020 · 2 comments · Fixed by #7667
Assignees
Labels
management/tracking Issues that track a subject or multiple issues p0

Comments

@NetaNir
Copy link
Contributor

NetaNir commented Apr 29, 2020

Please add your +1 👍 to let us know you have encountered this


Status: RESOLVED

Overview:

When testing CDK apps using jest v25.5.0 it throws:

TypeError: Cannot read property 'map' of undefined

Complete Error Message:


    TypeError: Cannot read property 'map' of undefined

       8 |     const stack = new Dummy.DummyStack(app, 'MyTestStack');
       9 |     // THEN
    > 10 |     expectCDK(stack).to(matchTemplate({
         |     ^
      11 |       "Resources": {}
      12 |     }, MatchStyle.EXACT))
      13 | });

      at findNpmPackage (node_modules/@aws-cdk/core/lib/private/runtime-info.ts:66:27)
      at Object.collectRuntimeInformation (node_modules/@aws-cdk/core/lib/private/runtime-info.ts:14:17)
      at App.synth (node_modules/@aws-cdk/core/lib/app.ts:144:39)
      at Function._synthesizeWithNested (node_modules/@aws-cdk/assert/lib/synth-utils.ts:57:54)
      at Object.expect (node_modules/@aws-cdk/assert/lib/expect.ts:8:90)
      at Object.<anonymous>.test (test/dummy.test.ts:10:5)

Workaround:

Add the following snippet to your test files

beforeEach(() => {
  jest.resetModules();
});

Jest documentation

Solution:

We released a patch that resolves the issue. Please use 1.36.1

Related Issues:

Environment

  • CDK CLI Version: All
  • Module Version: All
  • OS: All
  • Language: Typescript and Javascript

Other information

@NetaNir NetaNir added needs-triage This issue or PR still needs to be triaged. p0 and removed needs-triage This issue or PR still needs to be triaged. labels Apr 29, 2020
@NetaNir NetaNir pinned this issue Apr 29, 2020
@NetaNir NetaNir added the management/tracking Issues that track a subject or multiple issues label Apr 29, 2020
@NetaNir NetaNir changed the title ‼️ NOTICE: CDK tests breaks with Jest v25.5.0 [Error: "TypeError: Cannot read property 'map' of undefined"] title: "‼️ NOTICE:e‼️ NOTICE: CDK tests breaks with Jest v25.5.0 [Error: "TypeError: Cannot read property 'map' of undefined']" Apr 29, 2020
@NetaNir NetaNir changed the title title: "‼️ NOTICE:e‼️ NOTICE: CDK tests breaks with Jest v25.5.0 [Error: "TypeError: Cannot read property 'map' of undefined']" ‼️ NOTICE: CDK tests breaks with Jest v25.5.0 [Error: "TypeError: Cannot read property 'map' of undefined'] Apr 29, 2020
@NetaNir
Copy link
Contributor Author

NetaNir commented Apr 29, 2020

Temporarily configure dependabot to ignore jest upgrades: #7662

@iliapolo
Copy link
Contributor

This issue seems to stem from the following commit to jest: jestjs/jest@470ef2d

iliapolo added a commit that referenced this issue Apr 29, 2020
Couple of things here:

**require.cache**

In version `v25.5.0`, jest introduced an [implementation](jestjs/jest#9841) of their own to the `require.cache` object. It seems that it doesn't handle caching `json` modules properly, which causes our code to fail because `mod.paths` is `undefined` when we are querying for `json` files that were required (for example `package.json` or `cloud-assembly.schema.json`).

https://github.com/aws/aws-cdk/blob/07fe642e38118e24837b492fa182737fc41bb429/packages/%40aws-cdk/core/lib/private/runtime-info.ts#L66  

```console
TypeError: Cannot read property 'map' of undefined
```

The "fix" was to add a null check to prevent failure when looking up modules who don't have the `paths` property defined. 

Note that this was only observed in test environments using `jest > v25.5.0`, not during actual runtime of `cdk synth`. This is because `jest` manipulates the built-in `require` module of `nodejs`.

**graceful-fs**

In version `v25.5.0`, jest [added](jestjs/jest#9443) a dependency on the `graceful-fs` package. The version they depend on (`4.2.4`) differs from the version that we bring (`4.2.3`). This caused the `graceful-fs` dependency that comes from `jest` no to be deduped by node at installation. In turn, this caused multiple copies of the library to be installed on disk. 

The `graceful-fs` package monkey patches the `process.chdir` and `process.cwd` functions with a cached version for better performance.

For reasons not completely clear to us, the existence of multiple copies of the module, caused `process.cwd()` to return incorrect cached results, essentially always returning the directory that the process was started from, without consideration to calls to `process.chdir`. This broke `decdk` (and possibly many more) tests which rely on `process.chdir`.  

Fixes #7657
iliapolo added a commit that referenced this issue Apr 29, 2020
Couple of things here:

**require.cache**

In version `v25.5.0`, jest introduced an [implementation](jestjs/jest#9841) of their own to the `require.cache` object. It seems that it doesn't handle caching `json` modules properly, which causes our code to fail because `mod.paths` is `undefined` when we are querying for `json` files that were required (for example `package.json` or `cloud-assembly.schema.json`).

https://github.com/aws/aws-cdk/blob/07fe642e38118e24837b492fa182737fc41bb429/packages/%40aws-cdk/core/lib/private/runtime-info.ts#L66

```console
TypeError: Cannot read property 'map' of undefined
```

The "fix" was to add a null check to prevent failure when looking up modules who don't have the `paths` property defined.

Note that this was only observed in test environments using `jest > v25.5.0`, not during actual runtime of `cdk synth`. This is because `jest` manipulates the built-in `require` module of `nodejs`.

**graceful-fs**

In version `v25.5.0`, jest [added](jestjs/jest#9443) a dependency on the `graceful-fs` package. The version they depend on (`4.2.4`) differs from the version that we bring (`4.2.3`). This caused the `graceful-fs` dependency that comes from `jest` no to be deduped by node at installation. In turn, this caused multiple copies of the library to be installed on disk.

The `graceful-fs` package monkey patches the `process.chdir` and `process.cwd` functions with a cached version for better performance.

For reasons not completely clear to us, the existence of multiple copies of the module, caused `process.cwd()` to return incorrect cached results, essentially always returning the directory that the process was started from, without consideration to calls to `process.chdir`. This broke `decdk` (and possibly many more) tests which rely on `process.chdir`.

Fixes #7657
@shivlaks shivlaks unpinned this issue May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
management/tracking Issues that track a subject or multiple issues p0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants