Skip to content

Commit

Permalink
test(fix): fix handling errors tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swouf committed Jan 20, 2023
1 parent 7978932 commit 1c410a3
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cypress/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ declare global {
database,
currentMember,
appContext,
errors,
}: {
database?: Partial<Database>;
currentMember?: Member;
appContext?: Partial<LocalContext>;
errors?: object;
}): Chainable<Element>;
}
}
Expand Down
3 changes: 1 addition & 2 deletions cypress/e2e/BuilderView.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,11 @@ describe('<BuilderView />', () => {
permission: PermissionLevel.Admin,
context: Context.BUILDER,
},
// errors: { deleteAppDataShouldThrow: true }, // TODO: Fix this !
errors: { deleteAppDataShouldThrow: true },
});
cy.visit('/');

deleteFile({ id });
// cy.wait(1000);

checkRow(data, true);
});
Expand Down
3 changes: 1 addition & 2 deletions cypress/e2e/PlayerView.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ describe('<PlayerView />', () => {
cy.setUpApi({
database: { appData: [data] },
appContext: { memberId: creator },
// errors: { deleteAppDataShouldThrow: true }, // TODO: fix this !
errors: { deleteAppDataShouldThrow: true },
});
cy.visit('/');

deleteFile({ id });
// cy.wait(1000);

checkRow(data);
});
Expand Down
22 changes: 18 additions & 4 deletions cypress/plugins/index.js → cypress/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <reference types="cypress" />
/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
Expand All @@ -15,10 +15,24 @@
/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
require('@cypress/code-coverage/task')(on, config);
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
return config;
const newConfig = {
...config,
env: {
...config.env,
REACT_APP_API_HOST: process.env.REACT_APP_API_HOST,
REACT_APP_MOCK_API: process.env.REACT_APP_MOCK_API,
REACT_APP_GRAASP_DEVELOPER_ID: process.env.REACT_APP_GRAASP_DEVELOPER_ID,
REACT_APP_GRAASP_APP_ID: process.env.REACT_APP_GRAASP_APP_ID,
REACT_APP_VERSION: process.env.REACT_APP_VERSION,
},
};
require('@cypress/code-coverage/task')(on, newConfig);
// include any other plugin code...

// It's IMPORTANT to return the config object
// with any changed environment variables
return newConfig;
};
6 changes: 5 additions & 1 deletion cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// <reference types="../../src/window" />
import 'cypress-file-upload';

import { MOCK_SERVER_API_HOST } from '../fixtures/appData';
import { CURRENT_MEMBER, MEMBERS } from '../fixtures/members';
import { MOCK_SERVER_ITEM } from '../fixtures/mockItem';
Expand Down Expand Up @@ -31,7 +33,7 @@ import { MOCK_SERVER_ITEM } from '../fixtures/mockItem';

Cypress.Commands.add(
'setUpApi',
({ currentMember = CURRENT_MEMBER, database, appContext } = {}) => {
({ currentMember = CURRENT_MEMBER, database, appContext, errors } = {}) => {
// mock api and database
Cypress.on('window:before:load', (win: Window) => {
// eslint-disable-next-line no-param-reassign
Expand All @@ -49,6 +51,8 @@ Cypress.Commands.add(
apiHost: Cypress.env('REACT_APP_API_HOST') || MOCK_SERVER_API_HOST,
...appContext,
};
// eslint-disable-next-line no-param-reassign
win.apiErrors = errors ?? { deleteAppDataShouldThrow: false };
});
},
);
4 changes: 3 additions & 1 deletion cypress/support/index.js → cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import '@cypress/code-coverage/support';

import './commands';

Cypress.on('uncaught:exception', (err, runnable, promise) => {
Cypress.on('uncaught:exception', (err, runnable, promise): boolean => {
// returning false here prevents Cypress from failing the test
// this is necessary to accept wanted error from mirage to fail the test
if (promise) {
// eslint-disable-next-line no-console
console.error(
'This error is caught by cypress and was configured to not throw.',
);
return false;
}
return true;
});
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if (MOCK_API) {
database: window.Cypress
? window.database
: buildDatabase(mockContext, mockMembers),
errors: window.apiErrors,
});
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"types": ["node", "cypress", "i18next"]
"types": ["node", "cypress", "i18next", "cypress-file-upload"]
},
"include": ["src"]
}

0 comments on commit 1c410a3

Please sign in to comment.