jest-preset-angular
jest-preset-angular copied to clipboard
[Bug]: Must use import to load ES Module
Version
13.1.1
Steps to reproduce
- Checkout repository https://github.com/CSchulz/jest-preset-angular with branch esm-snapshot
- Switch to examples/example-app-v15
- Run
npm i
- Run
npm run test-esm
- See error in src/app/app.component.spec.ts
Expected behavior
Snapshot testing should work.
Actual behavior
Snapshot testing isn't working properly.
It seems that ng-snapshot.ts file is using the old require style resulting into the error:
https://github.com/thymikee/jest-preset-angular/blob/2437b4a78f4c18d4a9849115a9f79183cecfeabf/src/serializers/ng-snapshot.ts#L42-L51
Removing it from the ivyEnabled function in the ng-snapshot.js works.
Must use import to load ES Module: C:\Users\Ich\Daten\huk\web-lib-angular\node_modules@angular\core\fesm2020\core.mjs
46 |
47 | it('should create instance', () => {
> 48 | expect(fixture).toMatchSnapshot();
| ^
49 | });
50 |
51 | it('should upload file', () => {
at Runtime.requireModule (../../node_modules/jest-runtime/build/index.js:841:21)
at printPlugin (../../node_modules/pretty-format/build/index.js:272:16)
at format (../../node_modules/pretty-format/build/index.js:453:16)
at __EXTERNAL_MATCHER_TRAP__ (../../node_modules/expect/build/index.js:317:30)
at Object.throwingMatcher (../../node_modules/expect/build/index.js:318:15)
at ....spec.ts:48:21
at l.invoke (../../node_modules/zone.js/fesm2015/zone-testing-bundle.min.js:13:6400)
at ProxyZoneSpec.onInvoke (../../node_modules/zone.js/fesm2015/zone-testing-bundle.min.js:110:2047)
at l.invoke (../../node_modules/zone.js/fesm2015/zone-testing-bundle.min.js:13:6340)
at a.run (../../node_modules/zone.js/fesm2015/zone-testing-bundle.min.js:13:1812)
at Object.o (../../node_modules/zone.js/fesm2015/zone-testing-bundle.min.js:134:592)
Additional context
No response
Environment
System:
OS: Windows 10 10.0.19045
CPU: (12) x64 AMD Ryzen 5 5600G with Radeon Graphics
Binaries:
Node: 16.15.0 - C:\nodejs\node.EXE
Yarn: 1.22.19 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 8.5.5 - C:\nodejs\npm.CMD
npmPackages:
jest: ~29.5.0 => 29.5.0
Relates to #2016
That file tries to be commonjs but it also uses import type. I’m not sure should it be written as commonjs or as esm. We should also check what tsconfig says about it. In current state it can cause problems
@CSchulz I am having the same problem, My architecture is a monorepo one, and I used more or less the same configuration as the one at examples/monorepo and I run in the same error as the one here but I am using fesm2022. Did you find a workaround for this problem
@CSchulz I am having the same problem, My architecture is a monorepo one, and I used more or less the same configuration as the one at examples/monorepo and I run in the same error as the one here but I am using fesm2022. Did you find a workaround for this problem
I am using patch-package or similar system to patch the condition in the file f.e. if you are using Angular 15+ there is ivy only, so you can just return true.
const ivyEnabled = (): boolean => {
return true;
};