-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: SSR E2E Test Scenarios (#17666)
- Loading branch information
Showing
19 changed files
with
475 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "../../.eslintrc.json", | ||
"ignorePatterns": ["**/*.d.ts"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts"], | ||
"rules": { | ||
"no-console": "off" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ssr.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { pathsToModuleNameMapper } = require('ts-jest'); | ||
const { compilerOptions } = require('./tsconfig.json'); | ||
const { defaultTransformerOptions } = require('jest-preset-angular/presets'); | ||
|
||
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'jest-preset-angular', | ||
globalSetup: 'jest-preset-angular/global-setup', | ||
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths || {}, { | ||
prefix: '<rootDir>/', | ||
}), | ||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], | ||
testMatch: ['**/+(*.)+(spec).+(ts)'], | ||
transform: { | ||
'^.+\\.(ts|js|mjs|html|svg)$': [ | ||
'jest-preset-angular', | ||
{ | ||
...defaultTransformerOptions, | ||
tsconfig: '<rootDir>/tsconfig.json', | ||
}, | ||
], | ||
}, | ||
|
||
collectCoverage: false, | ||
coverageReporters: ['json', 'lcov', 'text', 'clover'], | ||
coverageDirectory: '<rootDir>/../../coverage/ssr-tests', | ||
coverageThreshold: { | ||
global: { | ||
statements: 90, | ||
branches: 74, | ||
functions: 90, | ||
lines: 90, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "ssr-tests", | ||
"description": "Spartacus SSR Tests", | ||
"keywords": [ | ||
"spartacus", | ||
"ssr", | ||
"tests" | ||
], | ||
"author": "SAP, Spartacus team", | ||
"license": "Apache-2.0", | ||
"private": true, | ||
"scripts": { | ||
"test": "../../node_modules/.bin/jest --config ./jest.config.js" | ||
}, | ||
"dependencies": { | ||
"tslib": "^2.6.2" | ||
}, | ||
"peerDependencies": { | ||
"http-proxy": "^1.18.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "ssr-tests", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "projects/ssr-tests/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@angular-eslint/builder:lint", | ||
"options": { | ||
"lintFilePatterns": ["projects/ssr-tests/**/*.ts"] | ||
} | ||
}, | ||
"test-jest": { | ||
"executor": "nx:run-commands", | ||
"options": { | ||
"command": "npm run test", | ||
"cwd": "projects/ssr-tests" | ||
} | ||
} | ||
}, | ||
"tags": ["type:util"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import 'jest-preset-angular/setup-jest'; | ||
import 'zone.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* Contains methods pertaining to reading, writing and asserting of the ssr log | ||
* generated by a running ssr server for the sake of testing ssr. | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
|
||
/** | ||
* Path where SSR log file from server will be generated and read from. | ||
*/ | ||
const SSR_LOG_PATH = './ssr.log'; | ||
|
||
/** | ||
* Writes no characters to log to clear log file. | ||
*/ | ||
export function clearSsrLogFile(): void { | ||
fs.writeFileSync(SSR_LOG_PATH, ''); | ||
} | ||
|
||
/** | ||
* Returns all text in the log as a single string. | ||
*/ | ||
export function getLogText(): string { | ||
return fs.readFileSync(SSR_LOG_PATH).toString(); | ||
} | ||
|
||
/** | ||
* Reads log and returns messages as string array. | ||
*/ | ||
export function getLogMessages(): string[] { | ||
const data = fs.readFileSync(SSR_LOG_PATH).toString(); | ||
return ( | ||
data | ||
.toString() | ||
.split('\n') | ||
// We're interested only in JSON logs from Spartacus SSR app. | ||
// We ignore plain text logs coming from other sources, like `Node Express server listening on http://localhost:4200` | ||
.filter((text: string) => text.charAt(0) === '{') | ||
.map((text: any) => JSON.parse(text).message) | ||
); | ||
} | ||
|
||
/** | ||
* Check that log contains expected messages in string array. | ||
* Fail test if log does not contain expected messages. | ||
*/ | ||
export function assertMessages(expected: string[]): void { | ||
const messages = getLogMessages(); | ||
for (const message of expected) { | ||
expect(messages).toContain(message); | ||
} | ||
} | ||
|
||
/** | ||
* Check log every interval to see if log contains text. | ||
* Keeps waiting until log contains text or test times out. | ||
*/ | ||
export async function waitUntilLogContainsText( | ||
text: string, | ||
checkInterval = 500 | ||
): Promise<true> { | ||
return new Promise((resolve) => { | ||
if (doesLogContainText(text)) { | ||
return resolve(true); | ||
} | ||
return setTimeout( | ||
() => resolve(waitUntilLogContainsText(text)), | ||
checkInterval | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns true if log contains string. | ||
*/ | ||
export function doesLogContainText(text: string): boolean { | ||
const data = fs.readFileSync(SSR_LOG_PATH).toString(); | ||
return data.includes(text); | ||
} |
Oops, something went wrong.