Cumulocity-IoT / cumulocity-cypress

Collection of commands and utilities to be used for automating tests for Cumulocity with Cypress.
Apache License 2.0
8 stars 2 forks source link

Fix mount @c8y/client dual package hazard #84

Closed thomaswinkler closed 6 months ago

thomaswinkler commented 6 months ago

The mount command does register a provider for FetchClient. In the current configuration, typescript generates the FetchClient import in mount.ts as require(), ngx-components however imports FetchClient with import from resulting in a so called "dual package hazard".

This potential comes from the fact that the pkgInstance created by const pkgInstance = require('pkg') is not the same as the pkgInstance created by import pkgInstance from 'pkg' (or an alternative main path like 'pkg/module'). This is the “dual package hazard,” where two versions of the same package can be loaded within the same runtime environment.

@c8y/client is provided as a single source npm module with different cjs and esm exports. This results in mount with require and ngx-components with import loading 2 different instances. With this, the FetchClient provider registered by mount is not used.

See Node.js Dual Package Hazard doc and Cypress Typescript doc

As a workaround mount is currently implemented as javascript, which is considered a workaround only and should be fixed. Changing typescript configuration results in error loading the plugin in Cypress node runtime. This might require to split plugin and commands to enable different build settings.

Related Cypress issues:

See here for guide on creating single source npm modules.

thomaswinkler commented 6 months ago

Started enabling eslint and typescript refactoring in #83 to prepare possible split of plugin and lib.

thomaswinkler commented 6 months ago

When building with es2020 module setting, the plugin does not load with the following error. See linked Cypress issues in the description.

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for .../digital-twin-manager/cypress.config.ts

There seems to be no fix / workaround for this to be working.

thomaswinkler commented 6 months ago

Fixed by #85. Using rollup, the plugin is now prepared to work with Cypress from es2020 build.