Skip to content

Commit

Permalink
Merge pull request #1363 from oasisprotocol/lw/cypress-console-log
Browse files Browse the repository at this point in the history
ts-web/*: Cypress forward console logs from browser to terminal
  • Loading branch information
lukaw3d authored May 11, 2023
2 parents 1c910a7 + 3213c2d commit 1f1c7af
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ jobs:
- name: Cypress run
working-directory: client-sdk/ts-web/core
run: npx --package cypress -c 'cypress run'
run: npm run-script test-e2e-cy

- name: Upload screenshot
if: failure()
Expand Down Expand Up @@ -263,7 +263,7 @@ jobs:

- name: Cypress run
working-directory: client-sdk/ts-web/ext-utils
run: npx --package cypress -c 'cypress run'
run: npm run-script test-e2e-cy

- name: Upload screenshot
if: failure()
Expand Down Expand Up @@ -333,7 +333,7 @@ jobs:
- name: Cypress run
working-directory: client-sdk/ts-web/rt
run: npx --package cypress -c 'cypress run'
run: npm run-script test-e2e-cy

- name: Upload screenshot
if: failure()
Expand Down
2 changes: 2 additions & 0 deletions client-sdk/ts-web/core/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {defineConfig} from 'cypress';
import * as outputConsoleLogs from './cypress/outputConsoleLogs';

export default defineConfig({
video: false,
e2e: {
supportFile: false,
setupNodeEvents: outputConsoleLogs.setupNodeEvents,
},
});
4 changes: 4 additions & 0 deletions client-sdk/ts-web/core/cypress/e2e/playground.cy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as outputConsoleLogs from './../outputConsoleLogs';

outputConsoleLogs.beforeWindowLoadListener();

describe('playground.cy.ts', () => {
it('should finish', () => {
cy.visit('http://localhost:8080/');
Expand Down
42 changes: 42 additions & 0 deletions client-sdk/ts-web/core/cypress/outputConsoleLogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/** Usage: add to cypress config */
export const setupNodeEvents: Cypress.Config['setupNodeEvents'] = (on, config) => {
on('task', {
consoleLog(stringifiedArray) {
console.log('console.log', ...JSON.parse(stringifiedArray))
return null
},
consoleWarn(stringifiedArray) {
console.warn('console.warn', ...JSON.parse(stringifiedArray))
return null
},
consoleError(stringifiedArray) {
console.error('console.error', ...JSON.parse(stringifiedArray))
return null
},
});
};

/** Usage: call before a test */
export function beforeWindowLoadListener() {
before(() => {
Cypress.on('window:before:load', (w) => {
cy.stub(w.console, 'log').callsFake((...args) => consoleTask('consoleLog', ...args));
cy.stub(w.console, 'warn').callsFake((...args) => consoleTask('consoleWarn', ...args));
cy.stub(w.console, 'error').callsFake((...args) => consoleTask('consoleError', ...args));
});
});
}

/**
* Workaround: `cy.task('consoleLog', stringifiedArray)` throws because cypress command is inside cypress command.
*/
function consoleTask(taskName: 'consoleLog' | 'consoleWarn' | 'consoleError', ...args: any[]) {
const stringifiedArray = JSON.stringify(args, (key, value) => (typeof value === 'bigint' ? `${value}n` : value), 2);

Cypress.emit(
'backend:request',
'task',
{ task: taskName, arg: stringifiedArray },
() => {},
);
}
5 changes: 3 additions & 2 deletions client-sdk/ts-web/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
],
"main": "dist/index.js",
"scripts": {
"prepare": "./compile-proto.sh && tsc",
"check-playground": "cd playground && tsc -p jsconfig.json",
"fmt": "prettier --write playground/src src test",
"lint": "prettier --check playground/src src test",
"playground": "cd playground && webpack s -c webpack.config.js",
"prepare": "./compile-proto.sh && tsc",
"test": "jest"
"test": "jest",
"test-e2e-cy": "cypress run"
},
"dependencies": {
"bech32": "^2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions client-sdk/ts-web/ext-utils/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {defineConfig} from 'cypress';
import * as outputConsoleLogs from './../core/cypress/outputConsoleLogs';

export default defineConfig({
video: false,
e2e: {
supportFile: false,
setupNodeEvents: outputConsoleLogs.setupNodeEvents,
},
});
4 changes: 4 additions & 0 deletions client-sdk/ts-web/ext-utils/cypress/e2e/playground.cy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as outputConsoleLogs from './../../../core/cypress/outputConsoleLogs';

outputConsoleLogs.beforeWindowLoadListener();

describe('playground.cy.ts', () => {
it('should finish', () => {
cy.visit('http://localhost:8080/?ext=http://localhost:8081&test_noninteractive=1');
Expand Down
3 changes: 2 additions & 1 deletion client-sdk/ts-web/ext-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
],
"main": "dist/index.js",
"scripts": {
"prepare": "tsc",
"check-sample-page": "cd sample-page && tsc -p jsconfig.json",
"check-sample-ext": "cd sample-ext && tsc -p jsconfig.json",
"fmt": "prettier --write sample-ext/src sample-page/src src",
"lint": "prettier --check sample-ext/src sample-page/src src",
"sample-page": "cd sample-page && webpack s -c webpack.config.js",
"sample-ext": "cd sample-ext && webpack -c webpack.config.js",
"fake-sample-ext": "cd sample-ext && webpack s -c webpack.config.js",
"prepare": "tsc"
"test-e2e-cy": "cypress run"
},
"dependencies": {
"@oasisprotocol/client": "^0.1.1-alpha.2"
Expand Down
2 changes: 2 additions & 0 deletions client-sdk/ts-web/rt/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {defineConfig} from 'cypress';
import * as outputConsoleLogs from './../core/cypress/outputConsoleLogs';

export default defineConfig({
video: false,
e2e: {
supportFile: false,
setupNodeEvents: outputConsoleLogs.setupNodeEvents,
},
});
4 changes: 4 additions & 0 deletions client-sdk/ts-web/rt/cypress/e2e/playground.cy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as outputConsoleLogs from './../../../core/cypress/outputConsoleLogs';

outputConsoleLogs.beforeWindowLoadListener();

describe('playground.cy.ts', () => {
it('should finish simple-keyvalue', () => {
cy.visit('http://localhost:8080/');
Expand Down
3 changes: 2 additions & 1 deletion client-sdk/ts-web/rt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"fmt": "prettier --write playground/src src test",
"lint": "prettier --check playground/src src test",
"playground": "cd playground && webpack s -c webpack.config.js",
"test": "jest"
"test": "jest",
"test-e2e-cy": "cypress run"
},
"dependencies": {
"@oasisprotocol/client": "^0.1.1-alpha.2",
Expand Down

0 comments on commit 1f1c7af

Please sign in to comment.