I've just upgraded to Cypress 10 and am now receiving issues from the amplify auth library which I'm using to log interactive users into the site under test. I've made a cy
extension for this.
It's a known issue in the sdk that it uses this global
variable which is overcome in angular by polyfilling it with:
/**
* AWS Amplify - Currently, the newest versions of Angular (6+) do not provide the shim for the
* global object which was provided in previous versions.
*/
(window as any).global = window;
I've tried adding that in a number of places for Cypress 10:
But with no luck.
FWIW, the gist of the extension
This is the full stack trace:
ReferenceError The following error originated from your test code, not from Cypress.
global is not defined
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure. View stack trace Print to console at node_modules/amazon-cognito-identity-js/node_modules/buffer/index.js (http://localhost:4200/__cypress/tests?p=cypress\support\e2e.ts:12878:37) at __require2 (http://localhost:4200/__cypress/tests?p=cypress\support\e2e.ts:17:52) at eval (http://localhost:4200/__cypress/tests?p=cypress\support\e2e.ts:27843:31) at eval (http://localhost:4200/__cypress/tests?p=cypress\support\e2e.ts:33508:3) at eval () From previous event: at runScriptsFromUrls (http://localhost:4200/__cypress/runner/cypress_runner.js:165206:136) at Object.runScripts (http://localhost:4200/__cypress/runner/cypress_runner.js:165221:12) at $Cypress.onSpecWindow (http://localhost:4200/__cypress/runner/cypress_runner.js:153378:75)
I've tried adding this at the top of the extension file:
let global = {};
(window as any).global = window;
/**
* amplify-js / cognito auth helper
* specific personas are logged-in and their tokens are cached to save on round-trips.
*/
import Auth, { CognitoUser } from '@aws-amplify/auth';
import Amplify from '@aws-amplify/core';
The issue is with the new esbuild
bundler which is apparently WIP, but which some kind souls have polyfilled.
(what this all means I've no idea)
I had previously worked around this with the new npm overrides
directive:
"overrides": {
"@aws-amplify/auth": {
"amazon-cognito-identity-js": {
"buffer": "6.0.3"
}
}
}
which I don't like as it has potential side-effects now and in the future.
Polyfilling node is the better approach, which is discussed in more detail here, but in a nutshell, updating Cypress config thus:
export default defineConfig({
e2e: {
async setupNodeEvents(on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions): Promise<Cypress.PluginConfigOptions> {
// This is required for the preprocessor to be able to generate JSON reports after each run, and more,
await addCucumberPreprocessorPlugin(on, config);
const bundler = createBundler({
plugins: [
NodeModulesPolyfills(),
GlobalsPolyfills({
process: true,
buffer: true
}),
createEsbuildPlugin(config)
]
});
on('file:preprocessor', bundler);